time.h & clock() on RH9

Started by
7 comments, last by Shannon Barber 20 years, 8 months ago
Has anyone ever called clock() from the C header time.h using the Redhat9 distro (I''m all ''up2date'')? I get 0 every time I call it, which makes it hard to build a functional watch dog.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
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."
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.
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]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
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.
My current observations are that Linux does not count time sleeping but Windows does.

//Win32cout << 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]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
boost::timer uses clock and the author seems to think it measures elapsed time.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
bug report!

This topic is closed to new replies.

Advertisement