Main menu:

Site search


August 2019
« Oct    




Layer Backed Views and improving drawing speed

A few evenings ago I watched the “Session 401 - Leveraging Cocoa’s Layer-Backed Views” from WWDC 2008 again. Watching it, I concluded that just adding this line of code for every NSView could give a nice speed boost in drawing the content of the NSView.

[self setWantsLayer:YES];

To find out, I wrote a simple test application with 2 NSView’s overlaying each other. The view in the back would show a random NSBezierPath while the view on top would simply move a cursor line from left to right (very similar to what you would find in an audio editing application).


A simple checkbox allows to switch on and off the layers for the NSView. A simple CPU load measurement is part of the application (simply the ratio between the cpu time spend in the application and the real time itself).

To let the CPU sweat, the cursor line is updated every 10 milliseconds. As a result the underlaying NSView representing the ‘wavefile’ is forced to be redrawn also. Since I don’t take care of the rectangle which needs to be updated, the full view is simply redrawn which puts a huge burden on the CPU.

On my MBP, dual core 2.5 GHz with a GeForce 8600M GT GPU this results in an average of 30% CPU load. When I now switch on the layer for every view, this is halved to 15%. This seems a nice speed boost for just one line of code to me.

Now the caveat. This only seems to work well on Mac’s which have a real GPU. Weird enough, testing the same application on a Mac Mini (dual Core 2Ghz but using the GMA950 Intel integrated graphics chip), the CPU load on the Mac Mini was only 10% without the layer, rising to 15% when I switched the layer on. So your mileage may vary…

And here is the small test project:

Write a comment

You need to login to post comments!