Timing macros in C/C++

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.

Back to top

7 Comments

  1. Julien Amsellem
    Posted August 20, 2010 at 1:17 am | Permalink

    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 :

    #define ENABLE_TUNNING
     
    #ifdef ENABLE_TUNNING
    	class LogTime
    	{
    	public:
    		LogTime(const std::string& tag)
    		{
    			_tag = tag;
    			_t = clock();
    		}
    		~LogTime()
    		{
    			printf("%s : %.2fms\n", _tag.c_str(), (float)(clock() - _t)/(float)CLOCKS_PER_SEC * 1000);
    		}
    	private:
    		std::string _tag;
    		clock_t _t;
    	};
     
    #define LOG_TIME(tag) LogTime __lt__ = LogTime(tag);
    #else
    	#define LOG_TIME(tag)
    #endif

    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.

    • Posted August 20, 2010 at 6:49 am | Permalink

      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!

  2. Franco
    Posted August 24, 2010 at 1:55 am | Permalink

    In your example which kind of variable is myTimer?

    • Posted August 24, 2010 at 6:00 am | Permalink

      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.

  3. ferros
    Posted March 20, 2011 at 12:03 am | Permalink

    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.

    • ferros
      Posted March 29, 2011 at 8:07 pm | Permalink

      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..

  4. Mohand
    Posted August 20, 2011 at 9:40 pm | Permalink

    very useful & helpful ! thanks utkarshi :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">