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 
2
  • what is making you "suspicious"? Commented Jan 31, 2010 at 21:08
  • 1
    your code executes too fast for the precision of time.h to catch it -- use a solution granting more precision (see my answer) Commented Jan 31, 2010 at 21:13

6 Answers 6

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 )); 
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

How i can convert it to milliseconds?
@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.
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!
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.
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.

Comments

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; 

Comments

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 

Comments

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 ; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.