timing in C++

K.I.L.E.R

Retarded moron
Veteran
I made my own timer but it uses 4 lines of code and it is innacurate.
Isn't there a simple way of doing it? I am using equations and the like do simulate time.
 
I'm not entirely sure what you are asking, but perhaps this is what you want:

man time
Code:
time(2)                   System Calls                    time(2)

NAME
     time - get time

SYNOPSIS
     #include <sys/types.h>
     #include <time.h>

     time_t time(time_t *tloc);

MT-LEVEL
     Async-Signal-Safe

DESCRIPTION
     time() returns the value of time in seconds  since  00:00:00
     UTC, January 1, 1970.

     If tloc is non-zero, the return value is also stored in  the
     location to which tloc points.

RETURN VALUES
     Upon successful completion,  time()  returns  the  value  of
     time.   Otherwise,  a  value  of  (time_t)-1 is returned and
     errno is set to indicate the error.

SEE ALSO
     stime(2), ctime(3C)

NOTES
     time() fails and its actions are undefined if tloc points to
     an illegal address.
 
Code:
void main()
{
	double a, b, c, d, e;
	const pi = (float) 3.141592654;
	int count=0;
	
	time_t time;


	a = 6565.265412121;
	b = 45646.1584;
	c = 23545.222656564161;
	d = 2.66464645563;
	e = 3.333333355535;

	while( ! kbhit() )
	{
		a = pow(((b*c)/d),(e/2));

		count++;
	}

	time = time/3600;

	cout << "Time(seconds):" << time << endl;
	cout << "Score:" << time/a << endl;

	system("pause");
}
 
Code:
time_t begin_time = time(0);

do_something();

time_t end_time = time(0);

#ifdef HAVE_DIFFTIME
time_t seconds = (time_t) difftime(end_time, begin_time);
#else
time_t seconds = (time_t) end_time - begin_time;
#endif
 
Tried both sets of code and neither is what I am after.

Thanks for the help though.

I am after something that will give me seconds purely from a designated point (in other words where/when I want the counting down/up to begin).
 
DWORD t = GetTickCount(), time;

...do stuff..

time = GetTickCount() - t; // milliseconds

Don't know how accurate that is for your purposes, but surely a second is a huge interval for something like timing an inner loop on today's cpus.

or something similar using:

QueryPerformanceCounter
The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter, if one exists.

BOOL QueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount ); // address of current counter value

Parameters
lpPerformanceCount

Points to a variable that the function sets, in counts, to the current performance-counter value. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.

Return Values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the installed hardware does not support a high-resolution performance counter, the return value is zero.

==========================

QueryPerformanceFrequency
The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists.

BOOL QueryPerformanceFrequency( LARGE_INTEGER *lpFrequency ); // address of current frequency

Parameters
lpFrequency

Points to a variable that the function sets, in counts per second, to the current performance-counter frequency. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.

Return Values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the installed hardware does not support a high-resolution performance counter, the return value is zero.
 
Otherwise, perhaps a realtime priority thread doing a Wait() with an appropriate time interval would be best.

Just put a check for the thread being signaled at the top of the loop along with your check for the keyboard.
 
K.I.L.E.R said:
Er... sorry. I will have to play with it a little longer to get hte desired effect then.

You merely have to do the subtraction of the ending-time from the begining time to arrive at number of seconds the event took.

If you still don't have an idea, post up your code again and we'll inline our changes.

NOTE: Always be sure you do something with the actual calculation values, otherwise the compiler is free to optimize it away. This will lead to showing itself as no time passing at all.

Code:
#ifndef NULL
#define NULL 0
#endif

void main() 
{ 
   double a, b, c, d, e; 
   const pi = (float) 3.141592654; 
   int count=0; 
    
   time_t begin_time = time(NULL); 
   a = 6565.265412121; 
   b = 45646.1584; 
   c = 23545.222656564161; 
   d = 2.66464645563; 
   e = 3.333333355535; 
   while( ! kbhit() ) 
   { 
      a = pow(((b*c)/d),(e/2)); 
      count++; 
   } 
   time_t end_time = time(NULL);

   long duration = difftime(end_time, begin_time);

   cout << "Time(seconds):" << duration << endl; 
   cout << "Score:" << duration/a << endl; 
   system("pause"); 
}
 
Back
Top