Measurement time in C ++ with high accuracy

How can I measure something in C ++ as accurately as in C #?

This is my C # code

var elapsedMilliseconds = (double)(sw.ElapsedTicks * 1000L) / Stopwatch.Frequency; 

I am using visual studio 2010.

+4
source share
5 answers

The Stopwatch class in C # is based on these two Win32 API calls that you can call from C / C ++:

Call the first function and divide it into the second function to get the value in seconds.

Example:

 LARGE_INTEGER freq, start, end; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&start); // do some long operation here Sleep(5000); QueryPerformanceCounter(&end); // subtract before dividing to improve precision double durationInSeconds = static_cast<double>(end.QuadPart - start.QuadPart) / freq.QuadPart; 

Please note that the following comment in the documentation is real and should be considered. I personally observed this behavior on the VirtualBox virtual machine. Differences in tens of milliseconds can exist between different processors, which leads to unexpected results, such as negative duration and duration exceeding expected:

On a multiprocessor computer, it should not matter which processor is called. However, you may get different results on different processors due to errors in the basic input / output system (BIOS) or hardware abstraction layer (HAL). To indicate the affinity of a processor for a thread, use the SetThreadAffinityMask function.

You may be interested in this for more: System.Diagnostics.Stopwatch returns negative numbers in Elapsed properties ...

Note that the Stopwatch class returns to GetTickCount if the above two APIs are not available or return failure codes. Most likely, it is just compatible with Windows 9x; I did not encounter any problems with these APIs on modern computers. However, GetTickCount will not have accurate accuracy.

+14
source

check this: How to get system time in C ++?

You can also use GetTickCount, which: Gets the number of milliseconds that have passed since the system started up to 49.7 days.

0
source

Use the functions of the QueryPerformanceFrequency and QueryPerformanceCounter API.

 LARGE_INTEGER freq; ::QueryPerformanceFrequency(&freq); LARGE_INTEGER start, end; ::QueryPerformanceCounter(&start); // do some work ::QueryPerformanceCounter(&end); double const t = double(end.QuadPart - start.QuadPart) / double(freq.QuadPart); 

To increase accuracy, you can subtract the time required to call the QueryPerformanceCounter from the result.

0
source

The Stopwatch class is a QueryPerformanceCounter wrapper:

 uint64_t startTime, endTime; uint64_t frequency; QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); QueryPerformanceCounter((LARGE_INTEGER*)&startTime); //DoStuff QueryPerformanceCounter((LARGE_INTEGER*)&endTime); double seconds = (endTime-startTime)/(double) frequency; 
0
source

I needed a more specific dimension and find it. Thanks for the other answers. They are all good. with this answer, I can easily switch to Microseconds if necessary.

Of course, I agree with the answer of James Johnston as the right one due to the great explanation and feedback from people.

thanks all

 int main(int argc, char ** argv) { unsigned long long nFreq = GetPerformanceTicksInSecond(); unsigned long long nBefore = GetPerformanceTicks(); timer start1 = timer::start(); CallSomeFunction(); unsigned long long nAfter = GetPerformanceTicks(); const unsigned long long nDiff = nAfter - nBefore; const unsigned long long nMicroseconds = GetTickMicroseconds(nDiff,nFreq); cout << "CallSomeFunction() took " << nMicroseconds << " " << time << endl; return 0; } unsigned long long GetPerformanceTicks() { LARGE_INTEGER nValue; ::QueryPerformanceCounter(&nValue); return nValue.QuadPart; } unsigned long long GetPerformanceTicksInSecond() { LARGE_INTEGER nFreq; ::QueryPerformanceFrequency(&nFreq); return nFreq.QuadPart; } double GetTickSeconds(unsigned long long nTicks,unsigned long long nFreq) { return static_cast<double>(nTicks) / static_cast<double>(nFreq); } unsigned long long GetTickMilliseconds(unsigned long long nTicks,unsigned long long nFreq) { unsigned long long nTicksInMillisecond = nFreq / 1000; return nTicks / nTicksInMillisecond; } unsigned long long GetTickMicroseconds(unsigned long long nTicks,unsigned long long nFreq) { unsigned long long nTicksInMicrosecond = nFreq / 1000000; return nTicks / nTicksInMicrosecond; } 
0
source

All Articles