PDA

View Full Version : timefunctions in c++



H.Stony
04-09-2005, 02:23 AM
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

int main ()
{
int n;
printf ("Starting countdown...\n");
for (n=10; n>0; n--)
{
printf ("%d\n",n);
wait (1);
}
printf ("End!!!\n");
return 0;
} but i have to use float values. another possiblity would be time() but also this function only returns int values. i have to use this code on mac,linux and win. any alternatives? how would you do this?

powerpad
04-09-2005, 03:43 AM
for linux use the
asm and do rdtsc - read time stamp counter

for windows look for

queryperformancecounter / queryperformancefrequency

since macos is with in some way linux maybe the linux thing will work

ZbuffeR
04-09-2005, 04:29 AM
the clock function seem not too bad and portable :
http://www.cplusplus.com/ref/ctime/clock.html

H.Stony
04-10-2005, 08:52 AM
the second reply is my current function. are there other possiblities?

(sorry i know it is not opengl specific)

thanks

T101
04-10-2005, 09:08 AM
Maybe something along the lines of:


#include <sys/time.h>

struct timeval starttime,endtime;
float elapsedseconds;

gettimeofday(&amp;starttime,NULL);

// do stuff (rendering and the like)

gettimeofday(&amp;endtime,NULL);
elapsedseconds=(float)((endtime.tv_sec-starttime.tv_sec)%86400)+(float)(endtime.tv_usec-starttime.tv_usec)/1000000.0;It's not super-precise (the actual precision is milliseconds if I'm not mistaken), but it's elapsed time, not CPU time.

H.Stony
04-10-2005, 09:30 AM
sounds good. but does this work on windows?

H.Stony
04-10-2005, 09:39 AM
yes. thanks