Timer function to provide time in nano seconds using C++

前端 未结 16 2265
野趣味
野趣味 2020-11-22 06:02

I wish to calculate the time it took for an API to return a value. The time taken for such an action is in the space of nano seconds. As the API is a C++ class/function, I a

16条回答
  •  时光取名叫无心
    2020-11-22 06:23

    You can use Embedded Profiler (free for Windows and Linux) which has an interface to a multiplatform timer (in a processor cycle count) and can give you a number of cycles per seconds:

    EProfilerTimer timer;
    timer.Start();
    
    ... // Your code here
    
    const uint64_t number_of_elapsed_cycles = timer.Stop();
    const uint64_t nano_seconds_elapsed =
        mumber_of_elapsed_cycles / (double) timer.GetCyclesPerSecond() * 1000000000;
    

    Recalculation of cycle count to time is possibly a dangerous operation with modern processors where CPU frequency can be changed dynamically. Therefore to be sure that converted times are correct, it is necessary to fix processor frequency before profiling.

提交回复
热议问题