Re: Fastest way to push strings to the screen in an NSView?
Re: Fastest way to push strings to the screen in an NSView?
- Subject: Re: Fastest way to push strings to the screen in an NSView?
- From: Ricky Sharp <email@hidden>
- Date: Wed, 17 Nov 2004 20:25:07 -0600
On Nov 17, 2004, at 8:08 PM, Scott Ellsworth wrote:
Roughly half the time my program takes is spent drawing attributed
strings to the screen, according to Shark. This surprises me, as
there are no more than 1200 on the screen at once, though they are
drawn at a variety of locations. The attributes are not complicated,
either.
I build my attribute dict only once in the program execution with:
hexFontSize=12.0f;
attributesForRegion = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont userFontOfSize:hexFontSize],
NSFontAttributeName,
nil];
Then I call the following method about 1.2k times, interleaved with
pushing a few graphics to the screen as well:
- (void) drawString:(NSString *)string inRegion:(Region) theRegion{
if (string != nil){
NSDictionary * attributesForRegion=[self
attributesForRegion:theRegion];
NSAttributedString * attributedString=[[NSAttributedString alloc]
initWithString:string attributes:attributesForRegion];
NSSize size=[attributedString size];
NSPoint basePoint = [self pointForRegion:theRegion];
NSRect r=NSMakeRect(basePoint.x-size.width/2, basePoint.y,
size.width, size.height);
[attributedString drawInRect: r];
[attributedString release];
}
}
attributesForRegion always returns the above dictionary of attributes,
and pointForRegion is a fast integer calculation.
drawAtPoint:withAttributes: seemed about the same speed, and adds the
complexity of trying to justify the result.
This is on Panther 10.3.6, running on my AlBook/1.5GHz.
NB - documentation refs, google URLs, and other pointers to info are
fine - I tried a number of searches first, but did not find anything
obvious.
Definitely check out the Worm example:
/Developer/Examples/AppKit/Worm
Specifically, the EventBetterWormView creates and caches an instance of
NSTextStorage, NSLayoutManager and NSTextContainer.
It's my understanding that when you use NS[Attributed]String's drawing,
it has the overhead of creating these three instances each time.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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