4
I would like to know how best to measure the running time of a programme in C++. I found several solutions on the internet, each with an approach. Suggestions?
4
I would like to know how best to measure the running time of a programme in C++. I found several solutions on the internet, each with an approach. Suggestions?
1
For Windows specifically, the best way (in the sense of having higher resolution) is to use the
QueryPerformanceCounter
.
An example of usage (use a recent compiler):
#include <windows.h>
#include <iostream>
#include <cstdint>
class Watch
{
public:
Watch()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
{
std::cout << "QueryPerformanceFrequency failed!\n";
return;
}
mPCFreq = static_cast<double>(li.QuadPart)/1000.0;
QueryPerformanceCounter(&li);
mCounterStart = li.QuadPart;
}
// Retorna o tempo em milisegundos desde que o
// objeto Watch foi criado.
double getCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return static_cast<double>(li.QuadPart - mCounterStart)/mPCFreq;
}
private:
uint64_t mCounterStart;
double mPCFreq;
};
int main()
{
Watch counter;
Sleep(1000);
std::cout << counter.getCounter() << std::endl;
return 0;
}
In this case, the code returns the time in milliseconds between the creation of the object Watch
and the call counter.getCounter()
.
Another more portable solution is to use the library boost and use boost::posix_time::time_duration
to measure the time on the scale you want (milliseconds, microseconds, seconds).
0
C++11 provides high_resolution_clock, is the ideal solution but microsoft still does not provide a correct implementation that uses Queryperformancecounter. This is considered a bug, and it looks like it will be solved in the next version of visual studio. So at the moment best use Queryperformancecounter directly or a library whose implementation.
Using Mingw’s Queryperformancecounter can solve this problem?
@lfelix Mingw’s Queryperformancecounter should be the same as Windows native.
0
The text(Optimizing software in C++
- recommend read) shows how we can achieve this.
Translated:
Time measures may require very high resolution if the time intervals are short. In Windows, you can use the functions
GetTickCount
orQueryPerformanceCounter
for millisecond resolution. The much higher resolution can be obtained with the time stamp counter on the CPU, which counts with the clock frequency of the CPU.
There’s a problem that the clock frequency may vary dynamically and measurements are unstable due to interruptions and task switches.
It is interesting to also look at the class StopWatch
which is based on two API’s, to QueryPerformanceFrequency
and QueryPerformanceCounter
.
Browser other questions tagged c++ windows
You are not signed in. Login or sign up in order to post.
Some recommendations: 1. Whenever possible (that is, if the compiler supports), one should use the types in
stdint.h
, asint64
, instead of__int64
. 2. Avoid using global variables for this type of thing. DoCounterStart
a local variable and pass it to themain
with areturn
and to theGetCounter
as argument would be better.– luiscubal
@luiscubal I made some changes to the code. It’s a different approach. What do you think?
– Lucas Lima
Much better, although the
cout
in the constructor is not ideal (an exception would be preferable). By the way, in case of an error, it is possible to get a more detailed message withGetLastError
andFormatMessage
withFORMAT_MESSAGE_FROM_SYSTEM
.– luiscubal
@luiscubal, according to the documentation of functions, Later versions of XP will not cause error when invoking this API function. The code is OK then.
– user6635
@lfelix O
QueryPerformanceCounter
is called once only in the constructor. The other, which you must have confused, is theQueryPerformanceFrequency
, that gets the frequency.– Lucas Lima
That’s right, I ended up editing the comment. I understood the code, solved my problem. Just one piece of information, this value returned is on what time scale, milliseconds? I haven’t implemented it yet...
– user6635
Yes. In milliseconds.
– Lucas Lima