Time difference measured by clock and steady-state time

I tried to measure the time taken to execute a specific function in my code. I originally used the function clock()as shown 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 read about the library chronoin C++11and tried to measure the same with std::chrono::steady_clock, as shown 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 fragment (using clock) was 89.53 ms, and the measurement steady_clockwas 1140 ms.

Why is there such a big time difference measured by both watches?

+4
source share
1 answer

clock , steady_clock . , , do_something() (, - ).

Daniel H , , do_something() . , , , ..

+5

Source: https://habr.com/ru/post/1679079/


All Articles