0

Whenever I call:

std::chrono::high_resolution_clock::now().time_since_epoch().count();

The assembly instructions for it are:

std::chrono::high_resolution_clock::now().time_since_epoch().count();
00007FF7D9E11840  call        qword ptr [__imp__Query_perf_frequency (07FF7D9E14090h)]  
00007FF7D9E11846  call        qword ptr [__imp__Query_perf_counter (07FF7D9E140A0h)] 

I've used the Windows API clock before and I thought the right way was to query the frequency once.

On Microsoft documentation it says:

QueryPerformanceFrequency Retrieves the frequency of the performance counter. The frequency of the performance counter is fixed at system boot and is consistent across all processors. Therefore, the frequency need only be queried upon application initialization, and the result can be cached.

This was in a loop so I think the call to QueryPerformanceFrequency is done repeatedly. This was building in Release mode and with /O2 optimization.

Also, if I build in Debug mode it makes the following assembly:

std::chrono::high_resolution_clock::now().time_since_epoch().count();
00007FF774FC9D19  lea         rcx,[rbp+398h]  
00007FF774FC9D20  call        std::chrono::steady_clock::now (07FF774FB1226h)  
00007FF774FC9D25  lea         rdx,[rbp+3B8h]  
00007FF774FC9D2C  mov         rcx,rax  
00007FF774FC9D2F  call        std::chrono::time_point<std::chrono::steady_clock,std::chrono::duration<__int64,std::ratio<1,1000000000> > >::time_since_epoch (07FF774FB143Dh)  
00007FF774FC9D34  mov         rcx,rax  
00007FF774FC9D37  call        std::chrono::duration<__int64,std::ratio<1,1000000000> >::count (07FF774FB1361h) 

I don't understand assembly, and I don't know why in the Release mode there are calls to the Windows API and in the Debug mode there is no mention of it. Also, I am on Visual Studio.

Thanks.

  • But there is only one call to QueryPerformanceFrequency, where is the second one? – Rakete1111 Dec 27 '16 at 15:05
  • This looks more a compiler specific issue rather than a C++ standard one. I suggest you to edit your flags and maybe specify what compiler and version you are using (Visual Studio IDE may use different compilers). Also, because I assume the c++ library provided by Visual C++ is produced by Microsoft, they may know the best practices to use QueryPerformanceFrequency(). – roalz Dec 27 '16 at 15:05
  • @Rakete It's in a loop, I understand it calls it each time in the loop. – Zebrafish Dec 27 '16 at 15:06
  • @TitoneMaurice Oh, now I get it :) Thanks. – Rakete1111 Dec 27 '16 at 15:07
  • You say: "and I thought the right way was to query the frequency once", this is not exactly what's stated in the MSDN page for QueryPerformanceFrequency: "the frequency need only be queried upon application initialization, and the result can be cached". "CAN be cached" does not imply it MUST be. Anyway, I may agree with you that it can be a non-optimal implementation, you may notify this point to the Microsoft Visual C++ team: msdn.microsoft.com/it-it/library/mt748084.aspx – roalz Dec 27 '16 at 15:13
1

The optimizer of VS doesn't seem to put the call of QueryPerformanceFrequency outside of the loop. It doesn't recognize that the output is always the same on every iteration after the first one, and so it can't optimize it, which any sane optimizer would do :)

Probably a missing feature or something rather than a bug I think, as I would say that VS optimizes the call to foo here outside of the loop (I don't have access to VS at the moment, so I can't test):

int value = 0;
void foo() { value = 2; }

for (int i = 0; i < 10; ++i) {
    foo();
    std::cout << i * value << '\n';
}

The reason why there is no call to the QueryPerformance* functions is that in Debug, the optimizer isn't allowed to optimize. The optimizer sees that a call to the native Windows API is faster than the call to the standard library implementation, and so it replaces the respective calls.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.