Often you need to calculate the time it takes to execute a certain set of operations. And if you’re trying to optimize your code, you’ll need this even more. So here’s a set of usefulĀ macros that can help you do that (without any plumbing).
The Timing Macros
These macros use the OpenCV functions cvGetTickCount() and cvGetTickFrequency(). So they’ll work only for your computer vision projects. These macros should work on all platforms.
// Record the execution time of some code, in milliseconds. #define DECLARE_TIMING(s) int64 timeStart_##s; double timeDiff_##s; double timeTally_##s = 0; int countTally_##s = 0 #define START_TIMING(s) timeStart_##s = cvGetTickCount() #define STOP_TIMING(s) timeDiff_##s = (double)(cvGetTickCount() - timeStart_##s); timeTally_##s += timeDiff_##s; countTally_##s++ #define GET_TIMING(s) (double)(timeDiff_##s / (cvGetTickFrequency()*1000.0)) #define GET_AVERAGE_TIMING(s) (double)(countTally_##s ? timeTally_##s/ ((double)countTally_##s * cvGetTickFrequency()*1000.0) : 0) #define CLEAR_AVERAGE_TIMING(s) timeTally_##s = 0; countTally_##s = 0
Using the macros
Using these timing macros is really easy! Before doing anything, you must declare a timer. You must supply a name for the timer. Based on the name, new variables are created (that hold timing information). Then you can start and stop the timer. If you’re using the timer for multiple operations done in an iteration, you can check the average time.
Here’s some code to get your started:
// Example: DECLARE_TIMING(myTimer); while (something) { START_TIMING(myTimer); printf("Hello!\n"); STOP_TIMING(myTimer); } printf("Execution time: %f ms.\n", GET_TIMING(myTimer) ); printf("Average time: %f ms per iteration.\n", GET_AVERAGE_TIMING(myTimer) );
Pretty simple.
Summary
You now have some really great macros for timing your programs. If you update them, leave a comment! They just might help someone ![]()
The author is Shervin Emami. He has a Masters degree in Robotics Engineering plus 13 years of building electronics, robots and computer programming in C/C++, Java, Assembly and Basic. He was born in Iran, grew up in Australia and worked on robots and computer vision technologies in USA, Abu Dhabi and Philippines.


7 Comments
Hi, your website is really interesting, I work on robotics too, and sometimes on vision algorithm (using OpenCV). I have my own function/macro to instrument my code and I thought it could be interesting to share it here :
Just copy this piece of code in a header file, and at the beginning of the method/function you want to instrument write this:
LOG_TIME(“some tag”)
You should see in the console the time the whole function takes to execute. You also could call the macro inside brackets {} if you want to measure time for a specific piece of code.
Hope this can help, any comment on this is welcome.
Hi Julien! Interesting code.. It does not depend on OpenCV, so that’s great! The only thing lacking, in my opinion, is the ability to time specific portions in a function (looking at the code I’m guessing it prints the time when a function ends). So we can probably merge your code and Shervin’s code and create a powerful timing class/macro!
In your example which kind of variable is myTimer?
You don’t need to declare a variable. The macros take care of that at compile time. Just write some variable name in place of myTimer and it should work.
Hello
What should i modified if i wish to set the timer to execute a command after a certain period, for example may be 5 seconds.
ehh may i know how to do the code for the above problem? Sorry if this question sound silly for you but i don’t know how to do, i just new in programming..
very useful & helpful ! thanks utkarshi