Easy Performance Analysis

This solution presents a way of using cyTimer code release to easily analyze the performance of your code (Note that cyTimer requires Windows operating system). Here, we will use a new class, TimerList, which keeps a number of timers and prints the result of all timers with a single function call. Using TimerList class makes it very simple to use many timers and measure the performance of each part of your code and find the bottleneck.

You will first need to download timerlist.h, which includes the implementation of TimerList class. Then, we will edit this file according to your code.

Editing timerlist.h

Let's assume that your code has two major parts and you would like to know their performances. The first part has, say, 3 sub-parts and the second part has 2 sub-parts. In this case, we can use 7 timers, 2 of which will measure each part as a whole, and the other 5 (3+2) will measure each individual sub-part. In the beginning of timerlist.h file TimerType and TimerTypeNames are defined as the following:

// ID of each timer and the number of timers
enum TimerType
{
   TT_PART1=0,
      TT_PART1_SUB1,
      TT_PART1_SUB2,
      TT_PART1_SUB3,
   TT_PART2,
      TT_PART2_SUB1,
      TT_PART2_SUB2,
   TIMER_TYPE_COUNT   // this keeps the number of timers (DO NOT REMOVE)
};
// The name string of each timer
static char TimerTypeNames[][255] = {
   "Part 1                ",
   "  Part 1 algorithm 1  ",
   "  Part 1 algorithm 2  ",
   "  Part 1 algorithm 3  ",
   "Part 2                ",
   "  Part 2 algorithm 1  ",
   "  Part 2 algorithm 2  ",
};

All you need to do is to edit TimerType and TimerTypeNames, such that for each item of TimerType there should be a string in TimerTypeNames, and TIMER_TYPE_COUNT should be at the end of TimerType list.

Using timerlist.h

We first need to include the following line in one of the source files, which defines a global TIMER variable that we can access anywhere in the code.

TimerList TIMER;

To measure the time, we access each timer separately and use Start() and Stop() methods. The following code shows how this can be done.

TIMER[ TT_PART1 ].Start();
// First part starts
TIMER[ TT_PART1_SUB1 ].Start();
// The first sub-part in part 1 code will be here
TIMER[ TT_PART1_SUB1 ].Stop();
TIMER[ TT_PART1_SUB2 ].Start();
// The second sub-part in part 1 code will be here
TIMER[ TT_PART1_SUB2 ].Stop();
TIMER[ TT_PART1_SUB3 ].Start();
// The final sub-part in part 1 code will be here
TIMER[ TT_PART1_SUB3 ].Stop();
// Some more stuff in part 1
TIMER[ TT_PART1 ].Stop();

The above code can be executed as many times as we like. TIMER keeps the statistics for the last 128 measurements, and max/min timings. To print the results of the measured part in the code above, all we need to do is to call the PrintResults method.

TIMER.PrintResults( TT_PART1, TT_PART2 );

This will print the results of part 1 to the stdout.