问题是这样的:我们正处理一个 OpenGL 的 iPhone 游戏,不知什么原因桢率非常低 - 尤其有一些小的操作发生在屏幕上的时侯。我首先打算着手处理的是开始偿试成块注释掉系统部分,看看哪个模块导致的这个问题。我移除了物理系统、逻辑更新以及一些要被渲染的东西。然而,我什么也没做,好像就有显蓍的性能提升。现在该到时侯,深入点查看系统并让 Shark 找出问题出在哪儿了。
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 是一个性能描述工具,自从 Mac OS X 10.3 开始每一个版本都自带。它作为 Xcode 工具的一部分。本教程将向你介绍 Shark,如何使用它来描述一个 iPhone 应用,以及解释结果。
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:
/Developer/Applications/Performance Tools/Shark
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.