How to time a function in milliseconds without boost::timer

Aly picture Aly · Feb 26, 2013 · Viewed 73.7k times · Source

I am using boost 1.46 which does not include boost::timer, What other way can I time my functions.

I am currently doing this:

time_t now = time(0);
<some stuff>
time_t after = time(0);

cout << after - now << endl; 

but it just gives the answer in seconds, so if the function takes < 1s it displays 0.

Thanks

Answer

Quentin Perez picture Quentin Perez · Feb 26, 2013

In linux or Windows:

#include <ctime>
#include <iostream>

int
main(int, const char**)
{
     std::clock_t    start;

     start = std::clock();
     // your test
     std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
     return 0;
}

Good Luck ;)