Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Performance
Performance is important to all graphics programs, whether they are based on QuickDraw or Quartz. When you rewrite your application to use only Quartz, you’ll want to pay particular attention to performance issues. Although Quartz optimizes its operations “under the hood,” there are coding practices you can adopt to ensure that your code works in concert with Quartz optimization strategies.
As you develop your application, you can analyze its performance using the debugging tools (Shark, Quartz Debug, Sampler, and so on) provided with Mac OS X. In particular, Quartz Debug is useful for identifying issues related to drawing performance.
Adopting Good Coding Practices
Part of adopting good coding practices is to understand how Quartz works. Your code may be performing some task that either isn’t necessary or is working at cross-purposes with Quartz.
Consider following these guidelines:
Don’t overdraw. Mac OS X v10.4 introduces a coalesced update feature. Quartz draws at a set rate (1/60 sec.) for optimal results. Don’t try to draw faster by flushing or synchronizing. In fact, Quartz enforces deferred updating to prevent you from drawing too fast.
Make your code cache-friendly. If you keep any Quartz object that you reuse, such as images, layers, colors,and patterns, Quartz notices and caches that object. Cached objects are drawn faster than those that aren’t. Make sure you are not creating, disposing, and recreating the same thing over and over again.
Use CGLayers for offscreen rendering. They are a better choice from a performance standpoint than bitmap graphics contexts.
Be kind to the window server by not overflushing. It’s important to understand the difference between the function
CGContextFlush
and the functionCGContextSynchronize
. The functionCGContextFlush
performs the flush immediately by calling into the window server. (It is not equivalent toCGContextSynchronize
+QDFlushPortBuffer
.)The purpose of
CGContextSynchronize
is to delay flushing. Synchronizing allows you to draw to the window backing store multiple times using multiple contexts. Given that each flushing operation is synchronized with the beam, you want to minimize the number of flushes (which is what calling the functionCGContextSynchronize
achieves).
Relevant Resources
For more information, see:
Drawing Performance Guidelines, which describes some basic ways to improve drawing performance in your code, contains specific tips for Carbon and Cocoa, describes how to measure performance, and discusses other issues, such as flushing.
Quartz Performance: A Look Under the Hood, in Quartz 2D Programming Guide.
CGLayer Reference, which provides a complete reference to the functions that create and manage CGLayer objects.
Copyright © 2004, 2006 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2006-09-05