Here was the problem: we were working on an OpenGL iPhone game and for some reason the frame rates were unusually low - especially for what little action was happening on the screen at the time. My first plan of attack was to begin commenting out chunks of the system in an attempt to see what module was causing the problem. I removed the physics, the logic updates, and some of the things being rendered. Nothing I did, however, seemed to create a noticeable improvement in performance. It was now time to look a little deeper into the system and let Shark figure out what was wrong.
Shark is a performance profiling tool shipped with every version of Mac OS X 10.3 or newer. It comes as part of the Xcode Tools. This tutorial will introduce you to Shark, how to have it profile an iPhone application, and how to interpret the results.
The very first thing you're going to need to do it get a debug version of your app onto your phone. If you're to the point of wanting to use Shark, I'm going to assume you already know how to do this. To make the recording and analysis a little quicker, we're going to target your specific application, which means you'll want to go ahead launch it before you launch Shark. You'll also want to run your application outside of the Xcode debugger - just launch it directly using the phone.
Up next, launch Shark. It should installed here:
In order to connect Shark to an iPhone, you'll need to change the sampling mode to Network/iPhone Profiling. You do this through Shark's Sampling menu.
At this point, your iPhone should be connected to the computer and your app should be running. When your phone appears in the list, simply check the box next to it. Shark should now look something like the image below.
Shark has several different configurations, however Time Profile is the one we'll be looking at today. This mode will tell us how much processing time is spent in what functions. Under Target, choose the app you'd like to profile. The current name of our app is "drop n lock", so that's what I have chosen. When everything is ready to go, just hit "Start".
Shark doesn't let you view the performance information real-time. Simply do the actions you'd like performance information for and when you're done, hit "Stop". At this point, Shark is going to spend a long time analyzing the sample and then will create a session file that we can review.
When the report is complete, it will display automatically.
There's lots of ways to view session information, however Tree (Top-Down) is the one I like the best. Looking at this report, we can see that the function responsible for rendering text is taking up 16% of the processing time. When I was first hunting for the problem, text rendering didn't even occur to me as a potential problem. And sure enough, when I disabled the text, my frame rate doubled. Obviously we'll need to find a more optimized way to display text.
There you have it. Using Shark is a great way to find and eliminate performance bottlenecks in your iPhone applications. It took me no time at all to pin-point exactly where in our code a potential problem was. Unfortunately it can't fix the code for you - that you'll have to do on your own.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.