C++11 clocks: g++ steady_clock::is_steady == false?
steady_clock
is supported for GCC 4.7 (as shown by the docs for the 4.7 release: http://gcc.gnu.org/onlinedocs/gcc-4.7.2/libstdc++/manual/manual/status.html#status.iso.2011) and steady_clock::is_steady
is true but only if you build GCC with --enable-libstdcxx-time=rt
See https://stackoverflow.com/a/12961816/981959 for details of that configuration option.
For GCC 4.9 it will be enabled automatically if your OS and C library supports POSIX monotonic clocks for clock_gettime
(which is true for GNU/Linux with glibc 2.17 or later and for Solaris 10, IIRC)
Here are the results with GCC 4.8 configured with --enable-libstdcxx-time=rt
on an AMD Phenom II X4 905e, 2.5GHz but I think it's throttled to 800MHz right now, running Linux 3.6.11, glibc 2.15
$ ./hrc
clock::period: 0.001000 us.
clock::is_steady: no
Calling clock::now() 1000000 times...
That took 0.069646 seconds. That's 69.645928 ns/iteration.
$ ./sys
clock::period: 0.001000 us.
clock::is_steady: no
Calling clock::now() 1000000 times...
That took 0.062535 seconds. That's 62.534986 ns/iteration.
$ ./sc
clock::period: 0.001000 us.
clock::is_steady: yes
Calling clock::now() 1000000 times...
That took 0.065684 seconds. That's 65.683730 ns/iteration.
And with GCC 4.7 without --enable-libstdcxx-time
(so the same results for all three clock types) on ARMv7 Exynos5 running Linux 3.4.0, glibc 2.16
clock::period: 1.000000 us.
clock::is_steady: no
Calling clock::now() 1000000 times...
That took 1.089904 seconds. That's 1089.904000 ns/iteration.
If you can run the simple program on other platforms/compilers, I would be very interested to know the results.
Mac OS X 10.8, clang++ / libc++, -O3, 2.8 GHz Core i5:
High resolution clock
clock::period: 0.001000 us.
clock::is_steady: yes
Calling clock::now() 1000000 times...
That took 0.021833 seconds. That's 21.832827 ns/iteration.
System clock
clock::period: 1.000000 us.
clock::is_steady: no
Calling clock::now() 1000000 times...
That took 0.041930 seconds. That's 41.930000 ns/iteration.
Steady clock
clock::period: 0.001000 us.
clock::is_steady: yes
Calling clock::now() 1000000 times...
That took 0.021478 seconds. That's 21.477953 ns/iteration.
steady_clock
and system_clock
are required to be distinct types. steady_clock::is_steady
is required to be true
. high_resolution_clock
may be a distinct type or an alias of steady_clock
or system_clock
. system_clock::rep
must be a signed type.
According to GNU's site, GNU libstdc++ doesn't support steady_clock
yet. That's why steady_clock::is_steady
is false.
Here is the relevant section of the support checklist:
20.11.7.1 Class system_clock Y
20.11.7.2 Class steady_clock N Support old monotonic_clock spec instead
20.11.7.3 Class high_resolution_clock Y