views:

284

answers:

3

Hi,

I'd like to measure the performance of code on the iPhone that runs only once, so Instrument's CPU sampler tool is of limited use, because it needs many iterations to collect enough samples.

Is there a tool that I can use that times each function on each invocation? That does call tracing instead of statistical sampling?

Regards, Jochen

A: 

It could be done trough interposing, here is an article presenting how can it be done for tracing messages, you could perhaps adapt it at timing their execution.

luvieere
+3  A: 

The question "Are there non-sampling time-profiling tools for iPhone apps?" is similar to what you're asking. In my response there, I point out DTrace, which can do function-call-based profiling, but unfortunately only works in the simulator, not on the actual device. You may also be able to gather data using Shark, with a small enough sampling interval.

Finally, code like the following can be used to do timing of code execution within your application:

CFAbsoluteTime elapsedTime, startTime = CFAbsoluteTimeGetCurrent();

// Your code here

elapsedTime = CFAbsoluteTimeGetCurrent() - startTime;
NSLog(@"Elapsed time in seconds: %f", elapsedTime);
Brad Larson
Thanks for the answer. I'm doing that now, but it would be nice to have a tool...
Jochen
+2  A: 

If you have some large block of code that performs unacceptably the one time it is called, the odds are pretty good that it either has some time-consuming loops in it or you have some calls to inefficient lower-level functions. Looping control structures ought to be easy to pick up by inspection, and locating inefficient lower-level functions is generally pretty easy with hand-timing CGAbsoluteTimeGetCurrent() and a "binary debugging search" (is the bottleneck in the first half of the block or the second? First quarter or second quarter? etc.)

If you can't find a hotspot with that type of search, that's a pretty good indication that you're doing okay, performance-wise, and greater performance is going to require some kind of re-think of your approach.

I don't mean to be facetious, but are you sure you care? If the code only executes once, it's relative performance may be a matter of curiosity more than something of value to the end user.

Larry OBrien
I'm tracing startup code that by nature runs only once when the application starts. However, to the user, it does matter if startup takes 2 instead of 10 seconds.
Jochen