A sufficiently accurate timing method for testing algorithm efficiency

xiaoxiao2021-03-05  32

In the experiment, it is often necessary to test the efficiency of the algorithm. At this time, it is necessary to use the precise timing method. Common methods such as clock (), etc., because these algorithms do have time is too small, generally milliseconds Level, even lower, it turned around online a few days, finally found a way, as seen from MSDN Online:

#include

Large_integer start, end; large_integer freq;

SetthreadAffinityMask (GetCurrentThread (), 1); QueryperFormanceFrequency (& FREQ); QueryperFormanceCounter (& Start);

// do something

QueryperFormanceCounter (& End);

Cout << (end.quadpart - start.quadpart) / (double) freq.quadpart << "Seconds" << Endl;

The above code uses some timing functions implemented in Kernel32.dll (which is part of Microsoft Windows). These functions and the data type they use are defined in Windows.h. Simply put, there is a very fine clock in Windows, QueryperFormanceCounter can get the number of ticks running in the current clock, and QueryperFormanceFrequency can get a clock frequency. In this example, the start-up time and the clock ticks at the end of the start and the end are saved in this example, and then the difference between the start time and the end time is obtained, the result is the run time in seconds. SetThreadAffinityMask is used to set the priority of the current thread, set to high priority to reduce interference. Large_integer here is defined in Windows.h, just declare this header file.

I have tested myself, it is really precise, but I started a few running or stable, and it can be stabilized after running more.

转载请注明原文地址:https://www.9cbs.com/read-37921.html

New Post(0)