active: Feb 2006 to Mar 2008 download: sf.net/projects/quickprof api: link

QuickProf

Simple C++ Performance Profiling

QuickProf is a simple C++ runtime performance measurement tool contained in a single header file. It provides a rough performance estimate with minimal effort. Profiling code is easy:

PROFILER.beginBlock("graphics");
draw();
PROFILER.endBlock("graphics");

This simply measures the real elapsed time spent in the wrapped code block. Later, timing results can be printed, plotted, etc. This kind of feedback can be invaluable to developers who want to optimize performance where it matters.

In general, to use QuickProf you need to:

  1. include the header file
  2. initialize the profiler
  3. wrap code blocks with begin/end calls
  4. get timing results in some form.

Below are some examples and tips. For all available functions, see the Profiler class in the API documentation.

Also see QuickTest (unit testing) and QuickMP (loop parallelization).

Example Results

QuickProf can give a simple timing summary string or generate a detailed timing file for plotting performance changes over time. For example, in one project I used QuickProf to profile the three major components of my code (inference, propagation, and adaptation) and print an overall summary at the end:

adaptation: 23.2914 %
inference: 11.5562 %
propagation: 0.200819 %

The same run also generated a timing data file, which I then plotted (with the Python module Matplotlib):

Profile results

Example 1: Simple Timing Summary

This code shows how to get a simple overall timing summary at the end of execution:

#include <quickprof/quickprof.h>

// For simple cases, just init with no arguments.
PROFILER.init();

PROFILER.beginBlock("setup");
setupFunc();
PROFILER.beginBlock("setup");

while(!quit)
{
  ...
  PROFILER.beginBlock("physics");
  simulate();
  PROFILER.endBlock("physics");
  ...
  PROFILER.beginBlock("graphics");
  draw();
  PROFILER.endBlock("graphics");
  ...
}

// This returns timing results for each block. It can format the
// data as a percentage or the actual elapsed time.
std::cout << PROFILER.getSummary(quickprof::PERCENT) << std::endl;

Example 2: Data File Output

This example shows how to save detailed timing data to an output file (e.g. for plotting). This assumes that your code runs in a cyclic manner.

#include <quickprof/quickprof.h>

// Initialize the profiler with smoothness value (used for moving
// averages), output filename, print period (print to file every
// nth cycle), and time format.
PROFILER.init(10, "timing.dat", 8, quickprof::MILLISECONDS);

while(!quit)
{
  ...
  PROFILER.beginBlock("physics");
  simulate();
  PROFILER.endBlock("physics");
  ...
  PROFILER.beginBlock("graphics");
  draw();
  PROFILER.endBlock("graphics");
  ...

  // Defines the end of a cycle in the code. This is needed for
  // file output and smooth averaging.
  PROFILER.endCycle();
}

// This is always useful for a final rough estimate.
std::cout << PROFILER.getSummary() << std::endl;

Example 3: Real Time Display

This example shows how to display timing data in real time as the application is running. This is great for things like interactive 3D applications. Again, this assumes that your code runs in a cyclic manner.

#include <quickprof/quickprof.h>

// Initialize the profiler with the smoothness value only. (The
// other parameters are only needed for file output.)
PROFILER.init(10);

while(!quit)
{
  ...
  PROFILER.beginBlock("collision");
  collide();
  PROFILER.endBlock("collision");
  ...
  PROFILER.beginBlock("physics");
  simulate();
  PROFILER.endBlock("physics");
  ...
  PROFILER.beginBlock("graphics");
  draw();
  PROFILER.endBlock("graphics");
  ...

  // Defines the end of a cycle in the code. This is needed for
  // file output and smooth averaging.
  PROFILER.endCycle();

  // Send the current timing data to some function that displays
  // it in real time as the application is running.
  double tc,tp,tg;
  tc = PROFILER.getAvgDuration("collision", quickprof::PERCENT);
  tp = PROFILER.getAvgDuration("physics", quickprof::PERCENT);
  tg = PROFILER.getAvgDuration("graphics", quickprof::PERCENT);
  displayTimingData("collision", tc);
  displayTimingData("physics", tp);
  displayTimingData("graphics", tg);
}

// This is always useful for a rough estimate at the end.
std::cout << PROFILER.getSummary() << std::endl;

Usage Tips