views:

595

answers:

2

I have tried Instruments and Shark to profile an iPhone app, but both use a data sampling approach, by regularly taking screenshots of thread stacks.

I would prefer to have a full coverage profiling tool, which would record every single function call and the time spent in functions and their subroutines. This seems intuitively better that getting samples. Something like AQtime on Windows would be great.

So my questions are:

  1. Can sample-based profiling be trusted and be as useful as function-call-based profiling?
  2. Can Instruments or Shark do this type of profiling?
  3. Are there other tools available, which would be closer to what I want?
+1  A: 

Yes, stack sampling can be counterintuitive, and yes it can be trusted. Look at this link, and the first comment.

It's not about asking "How long did this take?"

It's about asking (in a doubtful voice) "Was that nanosecond necessary?"

Mike Dunlavey
+3  A: 

I've found that the time profiling used by Shark is very accurate for determining what your bottlenecks are in your code. You can adjust the sampling interval to be more fine-grained by showing the Mini Config editor using Config | Show Mini Config Editor and lowering the sample time.

Instruments in Xcode 3.2 also now has a nice Time Profiler instrument, although that's Mac-only. I've found that Instruments works well for profiling, but it can drop samples if the system is under heavy load. Generally, I start with Instruments, given how easy it is to use, then move on to Shark if I need a more detailed view of what's going on.

If you really want to do function-call-based profiling, I'd look at DTrace. I've written a couple of articles about tuning Cocoa applications using DTrace here and here. The latter one even shows an example of tuning the startup time of an iPhone application using a custom DTrace script.

Unfortunately, DTrace currently does not run on the iPhone itself, but you can still gather a lot of interesting information using it by running your application in the simulator. While the exact timing information will be nowhere near what it is on the device, knowing exactly what methods are executed how many times and in what order can give some clues as to where to optimize. I use DTrace to provide a different perspective on information gathered by Shark and Instruments, and to answer specific questions about my application.

Brad Larson