2

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

Jochen
  • 7,270
  • 5
  • 24
  • 32
  • Does CPU sampler tool require multiple repeated instances? Meaning it triggers the code over and over to benchmark? Or does it just listen and then measure on execution? Because unless you have 500 iPhones, you'll probably just want to uninstall the code to retest anyway, right? – Anthony Oct 02 '09 at 07:35
  • Thank you all for your suggestions providing different ways to measure execution time -- I haven't accepted any one of them because no tool is mentioned (as asked), and people therefor should pick the solution best for them. – Jochen Dec 06 '12 at 12:22

4 Answers4

8

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);
Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
2

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
  • 8,484
  • 1
  • 41
  • 75
  • 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 Jan 19 '10 at 15:19
0

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
  • 37,065
  • 18
  • 127
  • 179
0

The Pulse engineering team recently published an article on how to measure the performance of single lines (or more) of Objective-c code: http://eng.pulse.me/line-by-line-speed-analysis-for-ios-apps/

Robin
  • 8,197
  • 11
  • 45
  • 74