alculating elapsed time using clock() returns invalid values on an AMD64 box
linxt at comcast.net
Mon Oct 4 21:45:32 UTC 2004
On Monday 04 October 2004 13:18, 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?
> uname -a returns: Linux Guzella 2.6.5-1.358 #1 Sat May 8 09:01:26 EDT
> 2004 x86_64 x86_64 x86_64 GNU/Linux
> gcc specs are: Reading specs from
> Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
> --infodir=/usr/share/info --enable-shared --enable-threads=posix
> --disable-checking --disable-libunwind-exceptions --with-system-zlib
> --enable-__cxa_atexit --host=x86_64-redhat-linux
> Thread model: posix
> gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
> I'm running the 2.6.5 kernel, as the 2.6.8 won't boot on this
> machine (i will be addressing this other issue later...)
> Thiago Guzella
alculating? I assume you mean calculating.
Anyway, clock() return an approximation amount of time the CPU is used. Take
into account that your program is not the only thing running on the CPU
during your "digital watch" elapsed time. Any multitasking operating system
has system programs running "concurrently" (actually running in small time
slots on a rotational basis).
Try "man time()" for what you're trying to do.
registered linux user #263467
"Our enemies are innovative and resourceful - and so are we,"
"They never stop thinking about new ways to harm our country and our
people - and neither do we." - George W. Bush, Aug 2004
More information about the users