alculating elapsed time using clock() returns invalid values on an AMD64 box

Rick Stevens rstevens at vitalstream.com
Mon Oct 4 20:28:30 UTC 2004


Thiago Guzella wrote:
> Hi there folks,
> 
> I have got an AMD64 system running fedora core 2. It's an Athlon64
> 3500+, on an Asus A8V Deluxe.
> I have found something quite intriguing, by running the following code:
> 
> ------------------
> 
> ...
> clock_t start, end;
> 
> start = clock();
> 
> // Do some work
> 
> end = clock();
> 
> elapsed_time = (double) (end - start) / (double) CLOCKS_PER_SEC;
> 
> ------------------
> 
> The calculated time for an specific program i've written is 32.83
> seconds, but the time i have calculated by looking at a digital watch
> before and after program execution was 125 seconds.
> My question is: i am doing something wrong?? Has the clock() way of
> measuring elapsed time been deprecated? If so, how should I do it?

If you carefully read the man page for clock(), you'd find:

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

Note that's _processor_ time, not elapsed time.  For elapsed time, use
"time(2)" (return current time in seconds since the epoch of 00:00:00
UTC, January 1, 1970).
----------------------------------------------------------------------
- Rick Stevens, Senior Systems Engineer     rstevens at vitalstream.com -
- VitalStream, Inc.                       http://www.vitalstream.com -
-                                                                    -
-           Blech!  ACKth!  Ooop!  -- Bill the Cat (Outland)         -
----------------------------------------------------------------------




More information about the fedora-list mailing list