tags:

views:

62

answers:

2

Shark complains about a big performance hit with this line, which takes like 80% of CPU time. I have a counter that is updated very frequently and performance seriously sucks.

It's an custom UILabel subclass with -drawRect: implemented. Every time the counter value changes, this is used to draw the new text:

[self.text drawInRect:textRect withFont:correctedFont lineBreakMode:self.lineBreakMode alignment:self.textAlignment];

When I comment this line out, performance rocks. Its smooth and fast. So Shark isn't wrong about this. But what could I do to improve this? Maybe go a level deeper? Does that make any sense?

Probably drawing text is really so incredible heavy...?

+2  A: 

There's no reason the drawing of a single label should cause such a massive performance hit. If you're updating it more than 30-60 times per second, though, the system may have trouble keeping up. In that case, you could use an NSTimer to only perform the drawing at fixed intervals. There's no doubt that drawing text is expensive, but you've pretty much found the optimal way of doing the drawing itself, unless the label is only a single line, in which case you can use the slightly cheaper drawAtPoint:withAttributes:

warrenm
hah warren, nice to see you here, Daniel Soffer here
Daniel
Hey dude. Too bad there's no PM feature here. Hit me up on facebook and let me know what you're doing these days.
warrenm
+1  A: 

Underneath, the text is being drawn with Quartz2D. You might see some improvement if you use it directly.

mustISignUp
but maybe Apple did the exact same thing?
dontWatchMyProfile
But Apple don't know exactly what you want to do, do they? So unfortunately they can't optimise their code to your specific case. Eg drawing a simple text string supports left-to-right drawing, right-to-left drawing, glyph substitution, font substitution, kerning tables, full Unicode, word-hyphenation, optimal line breaking and text wrapping, and much more. After all that it will use quartz to draw the appropriate glyphs at appropriate positions. If you know in advance that you need to render 4 glyphs at fixed positions from a subset of glyphs (0-9) then you know more than apple ever could.
mustISignUp