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: Robbie Haertel <email@hidden>
- Date: Wed, 17 Nov 2004 19:24:49 -0700
>From the, whatever you call those extra information pages on classes
(http://developer.apple.com/documentation/Cocoa/Conceptual/AttributedStrings/index.html#//apple_ref/doc/uid/10000036i)
The drawAtPoint: and drawInRect: methods are designed for drawing
small amounts of text or text that must be drawn rarely. They create
and dispose of various supporting text objects every time you call
them. To draw strings repeatedly, it is more efficient to use
NSLayoutManager, as described in "Drawing Strings".
I think that 1000-2000 is too much for these methods to handle so look
into NSLayoutManager if it is worth the speed up.
Robbie
On Wed, 17 Nov 2004 18:08:10 -0800, Scott Ellsworth <email@hidden> wrote:
> Hi, all.
>
> 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.
>
> Scott
>
> _______________________________________________
> 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
>
_______________________________________________
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