I was trying to measure the time taken to execute a specific function in my code. Initially I used the clock()
function as below
clock_t start = clock();
do_something();
clock_t end = clock();
printf("Time taken: %f ms\n", ((double) end - start)*1000/CLOCKS_PER_SEC);
Later I was reading about the chrono
library in C++11
and tried to measure the same with a std::chrono::steady_clock
as below
using namespace std::chrono;
auto start = steady_clock::now();
do_something();
auto end = steady_clock::now();
printf("Time taken: %lld ms\n", duration_cast<milliseconds>(end - start).count());
The time measured by the first code snippet (using clock
) was 89.53 ms
and that measured by steady_clock
was 1140 ms
.
Why is there such a big difference in time measured by both the clocks?