views:

668

answers:

3

I feel my app has bad performance, and want to figure out which parts in code are evil. Is there a good tutorial somewhere on how to find these?

+2  A: 

Do some profiling using the "Instruments" app found in the same directory as XCode.

You can run you program with Instruments by using "Start with Perfomance Tool" in XCodes "Run" menu.

mrueg
A: 

To find performance bottlenecks (I'm assuming you have the source code and you aren't using a precompiled app), you want to use a profiler. Basically, a profiler allows you to inspect how much time another program is taking in certain areas of the code and you can make decisions of whether that makes sense or not.

If you're developing in Java, there are a number of profilers out there. NetBeans has a profiler built into the IDE. Eclipse comes with a number of profilers that can be plugged in (some free, some not). And, of course, there are standalone profilers.

Unfortunately, Visual Studio does not come with a profiler unless you have the Team Server edition. Of course, there are other companies that developer profilers for .NET, but I typically like my tools all within the same IDE (personal preference).

Other languages have their own profilers, so you'll have to look up those on a per-profile basis.

With All That Said - Before you start spending a lot of time profiling your code, the first thing you need to do is make sure that your algorithms are strong. Many, many people spend time "optimizing" parts of their code without ever really thinking about whether or not the algorithms they are employing are the best they can be. For example: have you chosen the correct Collections for the job, are your algorithms O(n^2), O(nlog(n)), or O(n)? Once you have a solid design and solid algorithms, then you want to spend more time with profiling.

JasCav
> Before you start spending a lot of time profiling your codeI'm going to have to disagree with this statement. Time is better spent at finding the bottleneck and improving its performance rather than a generic look through the code base.
Ushox
If you have an existing code-base, sure. But, if you're writing new code, it's far better to think about design and algorithms up front rather than just write "whatever" and then have to go back and refactor later. (Basically, I was encouraging him to use his brain.)That's where I was coming from.
JasCav
Why do you give a list of profiling tools for every language except Objective-C? "If you're using Java?" On the iPhone? And I disagree that you should think about algorithms up front. It's a waste of time, because 90% of the things you think will be slow end up being fast. The other 10% show up as performance issues, and you can track them down and fix them.
Chris Garrett
@Chris - When he wrote the question the first time, he did not specify that he was developing for an iPhone, so my response applied. It obviously does not apply directly to his question now.Second, I was not referring to "fix the things that you think was wrong." I'm talking about up-front design. For example, if you need to store things in a container in your application, spend some time thinking about the best container to use. (I'm not talking about hunting and pecking for "slow areas" of an application which is obviously a waste of time.)
JasCav
+6  A: 

Instruments is your friend in this regard, and has many excellent profiling tools for finding memory leaks, how much memory you're using, detailed tracing of what code is running when, and so on. You can find the Instruments User Guide here.

However, you should also look into a little tool called Shark, installed with the developer tools. Personally, I find Shark more useful than Instruments for profiling my code and finding out what's taking up time when it matters. Read Optimizing your Application with Shark 4 for lots of great info on using Shark.

I've just spent a few days optimising my iPhone app, and Shark is a wonderful tool. Once you find out where you're slow, making it fast can be quite difficult, especially if you're drawing UIImages — expect to do a lot of caching of images into static variables!

Optimising can really reduce the readability of your code, so make sure you're careful where you do it and that it's absolutely necessary.

iKenndac
Nice hint with shark, but how to constrain it to the iPhone app? All I get is a chunky list of process crap?
HelloMoon
Launch the app in the Simulator, then change "Everything" to "Process" then choose the process from the process list - it'll have the same name as your app. If you want to Shark something on the device, choose Sampling -> Network/iPhone Profiling…, connect to your device and choose the process from the list.
iKenndac
There's no process to select from when networking to device. All I see is the device, activated. Processes is grayed out.
HelloMoon
Note that Shark support has been dropped from iOS 4 - Apple now say to use the Time Profiler in Instruments instead.
JosephH