Archived

This topic is now archived and is closed to further replies.

Shannon Barber

time.h & clock() on RH9

Recommended Posts

DESCRIPTION
The clock() function returns an approximation of processor
time used by the program.

RETURN VALUE
The value returned is the CPU time used so far as a
clock_t; to get the number of seconds used, divide by
CLOCKS_PER_SEC. If the processor time used is not avail�
able or its value cannot be represented, the function
returns the value (clock_t)-1.

CONFORMING TO
ANSI C. POSIX requires that CLOCKS_PER_SEC equals 1000000
independent of the actual resolution.
------------

I''m not exactly sure clock() is what you''re looking for. That''s CPU time up there. As in, "how much time the CPU has been executing instruction from this program."

Share this post


Link to post
Share on other sites
Here''s a little demo that produces values for me:


#include <time.h>
#include <stdio.h>


int main()
{
long int i;
clock_t start;
clock_t end;
double duration;

i = 1000000000;
start = clock();

while (i--);

end = clock();

duration = (double)(end - start)/CLOCKS_PER_SEC;

printf("Duration is: %f \n", duration );
return 0;
}


You may want to look at gettimeofday() for a better resolution.



-------
Andrew
PlaneShift - A MMORPG in development.

Share this post


Link to post
Share on other sites
I'm looking for something with high resolution (the bit about posix CLOCKS_PER_SEC being 1000000 caught my eye on clock)

If you interpret it as only time spent executing this process, then clock is broken on Win32.

I'll try gettimeofday, thanks!

[edited by - Magmai Kai Holmlor on August 6, 2003 3:37:56 PM]

Share this post


Link to post
Share on other sites
You may want to look into the times() function (man 2 times), which at least on Linux gives a useful, high-res return val. Not sure how portable it is, tho; I''ve never had a need for timers in my cross-platform stuff.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
If you interpret it as only time spent executing this process, then clock is broken on Win32.


Are you sure? I wouldn''t be surprised, but it is allowed to have any initial value it wants, not just 0. So maybe its just that windows starts it off with some "random" value.

Share this post


Link to post
Share on other sites
My current observations are that Linux does not count time sleeping but Windows does.


//Win32
cout << CLOCKS_PER_SEC << " Hz" << endl;
cout << clock() << endl;
Sleep(1000);
cout << clock() << endl;

will output something like:
1000 Hz
93
1125


cout << CLOCKS_PER_SEC << " Hz" << endl;
cout << clock() << endl;
usleep(1000 * 1000);
cout << clock() << endl;

will output:
1000000 Hz
0
0


[edited by - Magmai Kai Holmlor on August 6, 2003 6:30:36 PM]

Share this post


Link to post
Share on other sites