So accurate timing is important to me, and I was investigating the 3 types of clocks specified in C++ 11, namely system_clock
, steady_clock
, and high_resolution_clock
.
My initial concern was testing if there is any difference in call overhead to the different types of clocks, and to check the resolution of each type of clock.
Here is my sample program:
#include <chrono>
#include <cstdio>
using namespace std;
using namespace std::chrono;
int main(int argc, char **argv)
{
size_t N = 1e6;
if(2 == argc) {
sscanf(argv[1], "%zu", &N);
}
#if defined(hrc)
typedef high_resolution_clock clock;
#warning "High resolution clock"
#elif defined(sc)
typedef steady_clock clock;
#warning "Steady clock"
#elif defined(sys)
typedef system_clock clock;
#warning "System clock"
#endif
const double resolution = double(clock::period::num) / double(clock::period::den);
printf("clock::period: %lf us.\n", resolution*1e6);
printf("clock::is_steady: %s\n", clock::is_steady ? "yes" : "no");
printf("Calling clock::now() %zu times...\n", N);
// first, warm up
for(size_t i=0; i<100; ++i) {
time_point<clock> t = clock::now();
}
// loop N times
time_point<clock> start = clock::now();
for(size_t i=0; i<N; ++i) {
time_point<clock> t = clock::now();
}
time_point<clock> end = clock::now();
// display duration
duration<double> time_span = duration_cast<duration<double>>(end-start);
const double sec = time_span.count();
const double ns_it = sec*1e9/N;
printf("That took %lf seconds. That's %lf ns/iteration.\n", sec, ns_it);
return 0;
}
I compile it with
$ g++-4.7 -std=c++11 -Dhrc chrono.cpp -o hrc_chrono
chrono.cpp:15:2: warning: #warning "High resolution clock" [-Wcpp]
$ g++-4.7 -std=c++11 -Dsys chrono.cpp -o sys_chrono
chrono.cpp:15:2: warning: #warning "System clock" [-Wcpp]
$ g++-4.7 -std=c++11 -Dsc chrono.cpp -o sc_chrono
chrono.cpp:15:2: warning: #warning "Steady clock" [-Wcpp]
I compiled with G++ 4.7.2, and ran it on
- SUSE Linux, kernel v3.1.10, CPU i7
- Angstrom Linux embedded system, kernel v3.1.10, MCU Tegra 2 (ARM Cortex A9).
The first surprising thing was that the 3 types of clock are apparently synonyms. They all have the same period (1 micro sec), and the time/call is practically the same. What's the point of specifying 3 types of clocks if they are all the same? Is this just because the G++ implementation of chrono
isn't mature yet? Or maybe the 3.1.10 kernel only has one user-accessible clock?
The second surprise, and this is huge, is that steady_clock::is_steady == false. I'm fairly certain that by definition, that property should be true. What gives?? How can I work around it (ie, achieve a steady clock)?
If you can run the simple program on other platforms/compilers, I would be very interested to know the results. If anybody is wondering, it's about 25 ns/iteration on my Core i7, and 1000 ns/iteration on the Tegra 2.
-DXXX
flag. The last argument tog++
is just the executable's filename, which doesn't matter a lick. (although I have it reflect both which type of clock and the fact that the program is exercising the chrono library.) – Wrac