Fastest way to push strings to the screen in an NSView?
Fastest way to push strings to the screen in an NSView?
- Subject: Fastest way to push strings to the screen in an NSView?
- From: Scott Ellsworth <email@hidden>
- Date: Thu, 18 Nov 2004 23:33:28 -0800
Ok, now I am confused.
As an experiment, I ran the code with both the old drawInRect and the
new NSLayoutManager based code. If the new code is faster than the
old, then it should take up a smaller fraction of the total run time
than the old. Shark tells me pretty reliably that more time is spent
in the new is - 35.8% of runtime as compared with 12.9% for the code it
replaced. This seems counteintuitive.
(Three routines drawing text to the screen take up just over half the
time, according to shark, and this one was the easiest to refit and
test.)
The view is flipped, so the NSLayoutManager code should do pretty well.
I have pasted the complete text of the two methods below, on the chance
that it is something about how I am using the layout manager.
This code draws a few hundred four digit numbers like 0101 or 3240 in
the view. Only visible numbers are drawn.
According to shark:
Old code:
- (void) drawNumbersRect:(HexRedraw*) redraw{
NSMutableAttributedString * hexNumber = [[NSMutableAttributedString
alloc] initWithString:@"0000" attributes:hexDefaultAttributes];
int endCol=[redraw endCol];
int endRow=[redraw endRow];
[[NSColor blackColor] set];
for (int col=[redraw startCol]; col<=endCol; ++col){
for (int row=[redraw startRow]; row<=endRow; ++row){
NSRect hexRect=[self rectForCol: col Row: row];
if ([self needsToDrawRect:hexRect]){
NSAffineTransform * shifter = [NSAffineTransform transform];
[shifter translateXBy:hexRect.origin.x yBy:hexRect.origin.y];
[shifter concat];
// Draw number
NSString * hexNumString = [NSString
stringWithFormat:@"dd",col,row];
[hexNumber replaceCharactersInRange: NSMakeRange(0,4) withString:
hexNumString];
NSSize numberSize=[hexNumber size];
NSRect r=NSMakeRect(2.0*hexSz2-numberSize.width/2, 0,
numberSize.width, numberSize.height);
[hexNumber drawInRect: r];
[shifter invert];
[shifter concat];
}
}
}
[hexNumber release];
}
New code:
/**
* Paint the hex numbers
*/
- (void) drawNumbersLayout:(HexRedraw*) redraw{
NSTextStorage * textStorage = [[NSTextStorage alloc]
initWithString:@"1234"];
NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer * textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textContainer release];
[textStorage addLayoutManager:layoutManager];
[layoutManager release];
NSRange glyphRange = [layoutManager
glyphRangeForTextContainer:textContainer];
int endCol=[redraw endCol];
int endRow=[redraw endRow];
[[NSColor blackColor] set];
for (int col=[redraw startCol]; col<=endCol; ++col){
for (int row=[redraw startRow]; row<=endRow; ++row){
NSRect hexRect=[self rectForCol: col Row: row];
if ([self needsToDrawRect:hexRect]){
NSAffineTransform * shifter = [NSAffineTransform transform];
[shifter translateXBy:hexRect.origin.x yBy:hexRect.origin.y];
[shifter concat];
NSString * hexNumString = [NSString
stringWithFormat:@"dd",col,row];
[textStorage replaceCharactersInRange:glyphRange
withString:hexNumString];
NSSize numberSize=[textStorage size];
NSRect r=NSMakeRect(2.0*hexSz2-numberSize.width/2, 0,
numberSize.width, numberSize.height);
[layoutManager drawGlyphsForGlyphRange: glyphRange atPoint:
r.origin];
[shifter invert];
[shifter concat];
}
}
}
[textStorage release];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden