6

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 running 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
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156

7 Answers7

8

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 ));
Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
4

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.

Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
  • How i can convert it to milliseconds? – itsaboutcode Jan 31 '10 at 21:17
  • @itsaboutcode, unfortunately with such low-level performance functions, there's no sure way to convert it to millisecond, as the result is different depending on your processor speed. You could calculate a ticks/second value using Sleep before hand, and then do the conversion. – Kornel Kisielewicz Jan 31 '10 at 21:22
  • 2
    Beware if using RDTSC on multicore or multisocket systems! Each cpu core in a system has a seperate time-stamp counter and they might or might not be in sync. In general, there needs to be a driver that periodically synchronizes the time stamp counters across the cores. Unless you explicitly set the thread affinity of your process, the OS is free to move it from one core to another. If the TSCs of the cores are not in sync, you can get your start and stop times from different counters, and the difference can be drastically wrong -- even negative! – Die in Sente Jan 31 '10 at 21:24
  • 2
    Another problem with using RDTSC is you have to know *exactly* what your cpu core frequency is to convert it to seconds. But the cpu core clock is not a constant! It is being turned up or down by the power management. – Die in Sente Jan 31 '10 at 21:28
1

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

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;
shura
  • 694
  • 5
  • 9
0

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 ;
PTT
  • 526
  • 7
  • 27
0

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
Die in Sente
  • 9,546
  • 3
  • 35
  • 41
0

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.

rmn
  • 2,386
  • 1
  • 14
  • 21