I'm trying to get my code to measure time durations shorter than 1ms but just can't.
I've searched around but have not managed to understand how to do it. I've added various bits of code I found that I think are relevant.
template <class Clock>
void
display_precision()
{
typedef std::chrono::duration<double, std::nano> NS;
NS ns = typename Clock::duration(1);
std::cout << ns.count() << " ns\n";
}
int main()
{
display_precision<std::chrono::high_resolution_clock>();
display_precision<std::chrono::system_clock>();
display_precision<std::chrono::steady_clock>();
std::cout << std::chrono::high_resolution_clock::period::num
<< "/" << std::chrono::high_resolution_clock::period::den;
std::chrono::high_resolution_clock::time_point nowTime;
std::chrono::high_resolution_clock::time_point startTime;
startTime = std::chrono::high_resolution_clock::now();
int count = 0;
do
{
nowTime = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::duration diff = nowTime - startTime;
__int64 difference = std::chrono::duration_cast<std::chrono::microseconds>(diff).count();
printf("\n%i", difference);
count++;
if (std::chrono::duration_cast<std::chrono::seconds>(diff).count() > 2) { break; }
} while (1);
}
The out put I get looks like this:
100 ns
100 ns
100 ns
1 / 10000000
and:
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
3000300
As you can see, the difference
changes once a msec and remains constant in between although it loops 15 times or so in the meantime (and even though I reportedly have a 100ns precision).
Also, this doesn't seem to be a printf bottleneck because I tried loading a vector with the data and I get the same results.
[Windows 7 Professional, VS Community 2013]