Need to do some performance analysis on the cheap?

Add some debug code to your app to write out the raw numbers you’re interested in to a simple text file. Then you post-process the data file. How? It’s as simple as:

  FILE* perfdata = fopen("foo_perf.dat", "w");

  /* Simple header */
  fprintf("# Generated by myapp at %s", asctime(localtime(NULL)));
  fprintf("count rank score delta");

  for ( i = 0; ... )
  {
    /* something intensive here ... */

    fprintf( perfdata, "%u %d %f %f", count, gen->rank, gen->score, gen->delta );
  }
  fclose( fp );

Then you can use GNU R to plot the numbers over time, and quickly visualise what your code is doing.

> perf <- read.table("foo_perf.dat", header=TRUE)
> plot(perf)
> plot(perf$score ~ perf$delta)
> plot(perf$rank, type='l')

Enlightenment!

You can also write out a timestamp (the canonical float time since epoch will do fine) and use the powerful time series tools too. Or simply subtract the value of the first timestamp entry from the column (eg. attach perf ; tstamp <- tstamp - tstamp[1]) and you have a convenient “zero time” and your x-axis becomes a measure of milliseconds since the event. Why, there’s no end to the fun! :)

And R isn’t just a publication-quality plotting tool – it is a fully-blown programming language, with a massive array of statistical libraries. There’s an excellent stats book that uses R (or S Plus) and shows how to get the most out of it - Statistical Computing by Crawley.

Oh, and I can’t wait for X-Ray. Wow. I’ve used DTrace a few times, and it’s awesomely powerful; it just takes a fair effort to learn, and even longer to learn how to use it effectively. It looks like Apple’s sexy front-end will make this an incredibly useful tool.