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: Douglas Davidson <email@hidden>
- Date: Fri, 19 Nov 2004 13:44:30 -0800
On Nov 18, 2004, at 11:33 PM, Scott Ellsworth wrote:
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.
The problem here is that you aren't drawing the same string repeatedly,
you're drawing different strings every time. Remember, I said that
NSLayoutManager caches the information about the glyphs and layout for
the text, until the text changes. Well, if the text changes every time
you draw, that doesn't help much. It looks like the string drawing
routines are probably better for this particular case.
Take a look at the traces in Shark, and see whether most of the time in
your string drawing is being spent in things that start with "CG", or
whether most of it is being spent in things that have "layout" in your
name. If you see that most of it is being spent in CG routines, then
you're spending your time rendering glyphs, and optimizing the AppKit
portion isn't going to help much.
If what you really want to draw is always a four-digit number, you
might be able to cheat a little. Here are a couple of tricks:
First, a trick that works if there are only a limited number of short
strings that you are interested in (10000 in this case): Take a single
NSTextStorage, fill it with all the strings, separated by \n, and let
NSLayoutManager lay it out once. Then figure out what the glyph range
is corresponding to each of your strings; when you want to draw one of
them, just tell your layout manager to draw the corresponding glyph
range.
Second, a trick that can work if you are drawing only numbers. Many
fonts will have a fixed width for all of the digits 0-9, so that
numbers line up; even if your font doesn't, it's possible you may want
to force this state of affairs for your particular drawing. If you
create an NSLayoutManager and use it with the string "0123456789", you
can find out this fixed width and the identities of the glyphs for the
digits 0-9 in your font. Then drawing 100 four-digit numbers is just a
matter of drawing 400 individual digits, assuming you can use the fixed
width to calculate where each digit should be placed. This is one of
the cases that I mentioned where it might make sense to use Quartz
glyph drawing APIs directly. If you do it right you might even be able
to do it all in one call to CGContextShowGlyphsWithAdvances().
This second trick works only where the glyph generation and layout are
trivial. If you have non-digits in your strings, that usually is not
the case, which is why NSLayoutManager is needed.
Douglas Davidson
_______________________________________________
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