开发者

Calculating time by the C++ code

开发者 https://www.devze.com 2022-12-18 21:31 出处:网络
I know this question has been asked few times over SO but none of them is really helping me out, so asking again.

I know this question has been asked few times over SO but none of them is really helping me out, so asking again.

I am using windows xp and runn开发者_StackOverflowing visual studio c++ 2008.

All the code which i am looking is using time.h but i think may be its not working correctly here, because results are making me suspicious.

So this is what i want.

star time = get time some how (this is my question)

my code

end time = get time some how (this is my question)

time passed = start time - end time


Here is what I use to print time in milliseconds.

void StartTimer( _int64 *pt1 )
{
   QueryPerformanceCounter( (LARGE_INTEGER*)pt1 );
}

double StopTimer( _int64 t1 )
{
   _int64 t2, ldFreq;

   QueryPerformanceCounter( (LARGE_INTEGER*)&t2 );
   QueryPerformanceFrequency( (LARGE_INTEGER*)&ldFreq );
   return ((double)( t2 - t1 ) / (double)ldFreq) * 1000.0;
}

Use it like this:

    _int64 t1;

    StartTimer( &t1 );

    // do some stuff

    printf( "Time = %.3f\n", StopTimer( t1 ));


You need a high precision timer. In case of Visual Studio use QueryPerformanceCounter.

If the precision is still not enough, use compiler intristics:

#include <intrin.h>
#pragma intrinsic(__rdtsc)

unsigned __int64 ticks = __rdtsc();

See info on that intristic here.

Both solutions are Windows only, the latter is probably MSVC only. I can post a similar solution for GCC/Linux if needed.


If you're on Windows, the GetTickCount() function is a handy way to get a timer with more resolution than 1 second. The resolution of GetTickCount depends on your operating system, but it's probably somewhere between 10 ms and 16 ms.

Note that for quick operations, doing your code once won't be enough. Your code might run in like 0.02 ms, which means you'll get 0 from a counter such as GetTickCount. Your code may not execute for long enough for the timer to "tick" over to the next value. The solution is to run your code in a loop a million times or whatever, time the whole lot, then divide by a million.


Usually people do something like this to measure some small time interval:

t0 = getTime();

for(int i = 0; i<1000000; ++i) {
  your code
}

t1 = getTime();

timePassed = (t1-t0)/1000000;


Kornel's suggestion of using QueryPerformanceCounter is an excellent one.

If that doesn't work for you, and you don't mind another Windows-only solution, use the Windows "multimedia timers". Search the Visual Studio help files for "timeBeginPeriod", "timeEndPeriod", and "Using Multimedia Timers".

To use the multimedia timers, you need to include the header and link with winmm.lib:

#include <mmsystem.h>
#pragma comment( lib, "winmm" )     //  need this library for multimedia timers


This is working

#include <windows.h>

SYSTEMTIME startTime;
GetSystemTime(&startTime);
WORD startmillis = (startTime.wSecond * 1000) + startTime.wMilliseconds;

// Do some stuff

SYSTEMTIME endTime;
GetSystemTime(&endTime);
WORD endmillis = (endTime.wSecond * 1000) + endTime.wMilliseconds;

WORD millis = startmillis - endmillis ;


Here is a portable, self-contained, short implementation of exactly what you need: https://web.archive.org/web/20151123102016/http://efesx.com/2009/08/19/portable-measurement-of-execution-time/

If the resolution isnt fine enough, you should execute your test a couple of times.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号