Color Setting Performance
If you're looking to eek some extra performance out of your drawing code, take a look at how you're setting your fill and stroke colors. Setting the color using CGContextSetRGBFillColor()
or creating a new CGColorRef/NSColor object each time you set the color is quite expensive to do each time you draw. Depending on how many colors you use in your drawing, consider caching the CGColorRef objects and using CGContextSetFillColorWithColor()
or -[NSColor setFill]
instead.
A CGColorRef is 48 bytes for a RGB color. A NSColor is 32 bytes for a RGB color, plus 48 bytes for a CGColorRef. If you can afford the memory hit, you’ll see quite a bit of speed improvement from caching colors.
Below is the output from a small test app (source available here) that tests various color setting methods: (Each test is 1,000,000 iterations.)
CGContextSetRGBFillColor: 4.368422 seconds CGContextSetFillColorWithColor, static CGColorRef: 0.599552 seconds -[NSColor setFillColor], static NSColor: 0.832328 seconds -[NSColor setFillColor], dynamic NSColor: 5.847799 seconds
0 comments: