Performance is the measure of how efficiently your software uses resources such as the CPU, memory, and hard drive. Improving performance involves establishing a set of target goals and then measuring the efficiency of your software against those goals. Apple provides tools for measuring your software efficiency in several different ways. Using your measurements, you can then decide an appropriate course of action for modifying your code and meeting your goals.
There are many ways to improve software performance, but not all of them are appropriate for every situation. Diagnosing performance problems requires some detective work on your part, but Apple’s performance tools can help you in this endeavor. With these tools, you can gather information such as the following:
Where time is being spent in your code
Whether your application is leaking memory
How much memory your application consumes
Where your application is drawing to the screen needlessly
Where files are being accessed
Whether your application is polling the system
From this and other information, you can identify reasons why your application does not match your performance goals and proceed with fixing those problems.
To determine where your application might have performance issues, you should gather a set of baseline metrics first using the Instruments or Shark applications. Metrics help you identify potential problem spots in your code. You can also compare your set of baseline values against later measurements to verify that the changes you made had the desired effect.
Tuning your code is an iterative process. You gather metrics, you make changes, and you repeat the process until you achieve the desired level of efficiency. And there is no single metric that can tell you the “performance” of your application. What you are really looking for is data that shows how efficiently your code performs its tasks and how responsive your application is to the user. When it comes to efficiency, there are several factors you need to consider:
How much memory does your application use? More memory can mean increased paging, which can slow the system down.
Where are your application’s hot spots? Where is your application spending most of its time and what is it doing? Perhaps the chosen algorithms are not as efficient as they could be.
How long does it take your application to launch? Longer launch times mean a longer time when your application is unresponsive to the user.
Does your application do too much work on the main thread? If so, you might want to use GCD or operations to perform work on other threads.
Does your application draw to the screen unnecessarily? Refreshing the screen faster than the human eye can perceive wastes time and resources.
Performance Overview helps you get familiar with the fundamentals of performance analysis.
Instruments User Guide shows you how to gather performance metrics using the Instruments application.
Shark User Guide shows you how to gather performance metrics using the Shark application.
Code Size Performance Guidelines shows you how to reduce your application’s memory footprint.
Code Speed Performance Guidelines shows you how to find and remove hot spots in your code.
Drawing Performance Guidelines shows you how to make your drawing code more efficient.
Fractal Performance: Demonstrates how to find bottlenecks in memory allocation and time-critical sections using Shark.
SIMD Primer: Provides an example of how to use the vector unit hardware to increase performance.
The following examples show how to use OpenCL to leverage the GPU for general computations:
If you’re just starting a new programming project, your designs should take performance into account. If you’re improving the performance of an existing program, you want to find the bottlenecks in your code. If you’re looking for peak performance, you want to tune it for specific hardware.
Before you can build performance into your code, you need to understand what is meant by performance. There are many ways to measure performance, but not all of them may be relevant to your application. Knowing which performance metrics are important to you can heavily influence design decisions.
The document Performance Overview contains information related to designing your application with performance in mind.
Diagnosing performance problems requires real data and not guesses. Apple’s performance tools provide metrics that you can use to identify potential problems. For some specific problems, there are also ways to take advantage of multithreading and Velocity Engine to improve performance.
If your application performs operations that could run in parallel, use Grand Central Dispatch and operations to perform tasks. To learn how to use these technologies, read Concurrency Programming Guide.
If you are writing a Cocoa application, read Cocoa Performance Guidelines for tips on how to use the Cocoa frameworks more efficiently.
If the launch time of your application seems slow, your code may be doing too much work when it launches. To learn how to trim your launch-time code to the smallest working set, read Launch Time Performance Guidelines.
If your code seems to slow down as the working data set gets larger, your algorithms may not be tuned properly. To learn how to improve the efficiency of your algorithms, read Code Speed Performance Guidelines.
If your application performs complex math or image calculations, use the Accelerate framework to speed up operations using the available vector hardware. To learn more about this framework, read Taking Advantage of the Accelerate Framework.
Hardware optimization is one of the last steps for developers who need to maximize the performance of their applications. To optimize for specific hardware, you’ll learn how to use the CHUD tools and optimize for the target processor.
If your application is slow on specific hardware, use the CHUD tools to gather hardware-related metrics. You can find information about the CHUD tools in Performance Overview.
If your application needs to address more than 4 GB of memory, write your program for a 64-bit architecture. To learn more about writing 64-bit programs, read 64-Bit Transition Guide.
If want to maximize performance on the Power Mac G5, read Technical Note TN2087, PowerPC G5 Performance Primer and Technical Note TN2086, Tuning for the G5: A Practical Guide.
The Snow Leopard Reference Library holds plenty more resources that make your job easier. To narrow the list of resources, you can set filters to focus on specific resource types (such as guides or sample code) or on specific topics (such as graphics and imaging or multiprocessing).
Last updated: 2009-05-27