As known clock()
may show less than or greater than the value of the real time - both cases are shown in the following examples 1 and 2.
For high-precision measurements of the time in C++11 we can use:
std::chrono::high_resolution_clock::now();
- guarantee high-precisionstd::chrono::steady_clock::now();
- guarantee that measure real timeclock();
- guarantee high-precision, but measure CPU-cycles instead of timetime(&t_start);
- isn't high-precision, but measure real time
1- For example: http://ideone.com/SudWTM
#include <stdio.h>
#include <time.h>
#include <thread>
#include <iostream>
#include <chrono>
int main(void) {
std::cout << "sleep(3) took: \n\n";
clock_t c_start, c_end;
time_t t_start, t_end;
std::chrono::high_resolution_clock::time_point h_start, h_end;
std::chrono::steady_clock::time_point steady_start, steady_end;
time(&t_start); // less precise than clock() but always get the real actual time
c_start = clock(); // clock() get only CPU-time, it can be more than real or less - sleep(3); took 0.00 seconds
h_start = std::chrono::high_resolution_clock::now();
steady_start = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(3));
steady_end = std::chrono::steady_clock::now();
h_end = std::chrono::high_resolution_clock::now();
c_end = clock();
time(&t_end);
std::cout << "highres = " << std::chrono::duration<double>(h_end - h_start).count() << " s \n";
std::cout << "steady = " << std::chrono::duration<double>(steady_end - steady_start).count() << " s \n";
printf("clock() = %.2lf seconds \n", (c_end - c_start) / (double)CLOCKS_PER_SEC);
printf("time() = %.2lf seconds \n", difftime(t_end, t_start));
return 0;
}
Result on g++ (Debian 4.9.2-10) 4.9.2: clock() = 0.00 seconds
sleep(3) took:
highres = 3.00098 s
steady = 3.00098 s
clock() = 0.00 seconds
time() = 3.00 seconds
Result on C++ MSVS 2013 v120 (Windows 7x64):
sleep(3) took:
highres = 3.00017 s
steady = 3.00017 s
clock() = 3.00 seconds
time() = 3.00 seconds
2- Second example OpenMP or <thread>
: http://coliru.stacked-crooked.com/a/2922c85385d197e1
#include <stdio.h>
#include <time.h>
#include <thread>
#include <iostream>
#include <chrono>
#include <vector>
int main(void) {
std::cout << "for-loop took: \n\n";
clock_t c_start, c_end;
time_t t_start, t_end;
std::chrono::high_resolution_clock::time_point h_start, h_end;
std::chrono::steady_clock::time_point steady_start, steady_end;
time(&t_start); // less precise than clock() but always get the real actual time
c_start = clock(); // clock() get only CPU-time, it can be more than real or less - sleep(3); took 0.00 seconds
h_start = std::chrono::high_resolution_clock::now();
steady_start = std::chrono::steady_clock::now();
#pragma omp parallel num_threads(10)
{
for (volatile int i = 0; i < 200000000; ++i);
}
steady_end = std::chrono::steady_clock::now();
h_end = std::chrono::high_resolution_clock::now();
c_end = clock();
time(&t_end);
std::cout << "highres = " << std::chrono::duration<double>(h_end - h_start).count() << " s \n";
std::cout << "steady = " << std::chrono::duration<double>(steady_end - steady_start).count() << " s \n";
printf("clock() = %.2lf seconds \n", (c_end - c_start) / (double)CLOCKS_PER_SEC);
printf("time() = %.2lf seconds \n", difftime(t_end, t_start));
int b = getchar();
return 0;
}
Result on g++ (Debian 4.9.2-10) 4.9.2: clock() = 1.35 seconds
for-loop took:
highres = 0.213906 s
steady = 0.213905 s
clock() = 1.35 seconds
time() = 0.00 seconds
Result on C++ MSVS 2013 v120 (Windows 7x64):
for-loop took:
highres = 1.49109 s
steady = 1.49109 s
clock() = 1.49 seconds
time() = 2.00 seconds
Resume:
When thread sleeps then
clock()
on g++ 4.9.2 doesn't measure time unlike other functions.When we use multithreading by using OpenMP or by using
<thread>
(link), thenclock()
on g++ 4.9.2 measures CPU-cycles of all threads.
Also on Windows MSVS 2013 clock()
measures required real time in both cases, but this doesn't guarantee that clock()
measures the same on other platforms (on linux g++ is 0 for the sleep and x-fold for the multithreading).
Based on this, if std::chrono::high_resolution_clock::now();
measures required real time in both cases on both Windows MSVS 2013 and g++ 4.9.2, does this guarantee that it will measure real high resolution time on all other platforms and does whether it guarantee standard C++11/14?
std::chrono::high_resolution_clock
where it's using system-time so it only has a 1 ms resolution. This is fixed in MSVC 2015. – Chuck Walbourn Jul 7 '16 at 17:58