Is there a way to access the system tick count, or some other sort of timer, in C++. I'm very new to the language.
thanks,
jmiller
Printable View
Is there a way to access the system tick count, or some other sort of timer, in C++. I'm very new to the language.
thanks,
jmiller
A very easy timer class that I make use of is CPerfTimer.
It can be found at the following link:
http://www.codeproject.com/datetime/perftimer.asp
You can simply just do:
CPerfTimer X = 0; // X is now a timer object and is set to 0 seconds.
X.Start() // X will now begin to tick
while (X < 3.1415926535) {} // just pause until X is greater than or equal to PI.
X.Stop() // stop the timer X
X = 0 // reset it to 0 seconds.
Seems easy enough to me and it's VERY precise.
This class is internally making use of the multimedia hardware timer. This timer can be accessed through the QueryPerformanceCounter function.
If you don't need that precision, GetTickCount will give you the milliseconds since the start of windows and is, unlike the multimedia timer, available on all systems.
What library has the GetTickCount function in it?
Maybe that's the wrong question. GetTickCount is a API call, right? How do you make the call for the function?
Yep, API.
The prototype is simple:
DWORD GetTickCount(void);
DWORD is a typedef for unsigned long.
So you can do
ticks = GetTickCount();
You can also use the ultra-high-resolution timestamp counter built into Pentiums and up (I believe most modern AMD chips have the same).
It needs a bit of ASM though...search for RTDSC (or RDTSC, can never remember the order) on Google.
I would post my code but it's currently archived somewhere with no hope of getting it quickly :eek:
the clock() function accesses a high res timer, and works on any system - it's part of standard C/C++.
usage:
Code:#include <time.h>
clock_t start;
start=clock();
........................
printf("Elapsed time %u\n",clock()-start);
The clock() resolution may be highly hardware dependent and is not necessarily high. The clock() function has a resolution of CLOCKS_PER_SEC ticks per second. This constant is hardware and implementation-dependent.
The MS implementation of the CRT running under windows has a resolution of 1000 ticks/second, which is equal to the resolution of GetTickCount. The difference between the two is that GetTickCount retrieves the number of milliseconds since the start of windows whereas clock retrieves the number of ticks since the start of your app.
clock is standard instrumentation. ie., portable.
Correct. That's probably the main difference. I just wanted to point out that clock is not very high res.
For the HP box I do a fair amount of work on
Under windows it has the value 1000:Code:
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 1000000
#endif
It has other uses as well.Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sleep( clock_t wait );
void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;
/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );
/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
As I said, highly hardware and implementation dependent.