I'm using the function clock() to measure the run time of a program so that
I can compare among several different algorithms. My code looks like:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
int main() {
clock_t start, stop;
double t = 0.0;
/* Start timer */
assert((start = clock())!=-1);
/* Do lotsa fancy calculations */
/* Stop timer */
stop = clock();
t = (double) (stop-start)/CLOCKS_PER_SEC;
printf("Run time: %f\n", t);
return(0);
} /* main */
The question is, does this give me the "real life" time that passes while
the process is excuting, or just the processor time actully used by this
process. Put another way, if I run the exact same code when the machine is
"idle" and again when the processor is being shared by a bunch of other
processes, will the above give me *roughly* the same results or a
significantly longer time for the latter case?
Thanx all!
--
Charles M. Reinke
Georgia Institute of Technology
School of Electrical and Computer Engineering
(404) 385-2579 6 61793
Charles M. Reinke <cm******@ece.gatech.edu> wrote: I'm using the function clock() to measure the run time of a program so that I can compare among several different algorithms. My code looks like:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <time.h>
int main() { clock_t start, stop; double t = 0.0;
/* Start timer */ assert((start = clock())!=-1);
/* Do lotsa fancy calculations */
/* Stop timer */ stop = clock(); t = (double) (stop-start)/CLOCKS_PER_SEC;
printf("Run time: %f\n", t);
return(0); } /* main */
The question is, does this give me the "real life" time that passes while the process is excuting, or just the processor time actully used by this process. Put another way, if I run the exact same code when the machine is "idle" and again when the processor is being shared by a bunch of other processes, will the above give me *roughly* the same results or a significantly longer time for the latter case?
clock() is supposed the CPU time, not the wallclock time, so it should
not matter (too much) if the machine is under load or not. Here are the
relevant sentences from the C89 standard:
The clock function returns the implementation's best approximation
to the processor time used by the program since the beginning of an
implementation-defined era related only to the program invocation. To
determine the time in seconds, the value returned by the clock
function should be divided by the value of the macro CLK_TCK. If the
processor time used is not available or its value cannot be
represented, the function returns the value (clock_t)-1.
Please note that here you're asked to divide by CLK_TCK, not
CLOCKS_PER_SEC. In the C99 standard it tells you instead to divide by
CLOCKS_PER_SEC and if it is defined I would prefer it over CLK_TCK,
it also being required by the (newer) POSIX standard.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
>I'm using the function clock() to measure the run time of a program so that I can compare among several different algorithms. My code looks like:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <time.h>
int main() { clock_t start, stop; double t = 0.0;
/* Start timer */ assert((start = clock())!=-1);
/* Do lotsa fancy calculations */
/* Stop timer */ stop = clock(); t = (double) (stop-start)/CLOCKS_PER_SEC;
printf("Run time: %f\n", t);
return(0); } /* main */
The question is, does this give me the "real life" time that passes while the process is excuting, or just the processor time actully used by this process.
clock() is supposed to give an approximation to processor time used.
On a uni-tasking machine (e.g. MS-DOS), it may give something more
like wall clock time. On a multi-processor machine it may run
faster than real time. In that sense, clock() is like an employee
time clock (or the game clock in basketball, football, or whatever):
it runs only some of the time, is likely to only run 8 hours on a
weekday, and if you've got multiple employees, they may rack up
more than 24 hours of work in 1 day.
Put another way, if I run the exact same code when the machine is "idle" and again when the processor is being shared by a bunch of other processes, will the above give me *roughly* the same results or a significantly longer time for the latter case?
Generally, I'd say yes, assuming the OS makes an attempt to measure
actual CPU time (UNIX does, and I think recent versions of Windows
do also, assuming the C implementation uses the features), you get
roughly the same results, subject to things like task switching
messing up the cache so a run with a lot of task switches has a lot
more cache misses. There is also the issue of what process the
task switch itself gets "charged" to.
Gordon L. Burditt
"Gordon Burditt" <go***********@burditt.org> wrote in message
news:11************@corp.supernews.com... clock() is supposed to give an approximation to processor time used. On a uni-tasking machine (e.g. MS-DOS), it may give something more like wall clock time. On a multi-processor machine it may run faster than real time. In that sense, clock() is like an employee time clock (or the game clock in basketball, football, or whatever): it runs only some of the time, is likely to only run 8 hours on a weekday, and if you've got multiple employees, they may rack up more than 24 hours of work in 1 day.
Generally, I'd say yes, assuming the OS makes an attempt to measure actual CPU time (UNIX does, and I think recent versions of Windows do also, assuming the C implementation uses the features), you get roughly the same results, subject to things like task switching messing up the cache so a run with a lot of task switches has a lot more cache misses. There is also the issue of what process the task switch itself gets "charged" to.
Gordon L. Burditt
Your explanation was very helpful. I'm using Linux on a dual-processor
machine, so I think I should be OK.
-Charles
<Je***********@physik.fu-berlin.de> wrote in message
news:3f************@uni-berlin.de...
case? clock() is supposed the CPU time, not the wallclock time, so it should not matter (too much) if the machine is under load or not. Here are the relevant sentences from the C89 standard:
The clock function returns the implementation's best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation. To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLK_TCK. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1.
Please note that here you're asked to divide by CLK_TCK, not CLOCKS_PER_SEC. In the C99 standard it tells you instead to divide by CLOCKS_PER_SEC and if it is defined I would prefer it over CLK_TCK, it also being required by the (newer) POSIX standard.
Regards, Jens
Thanx, this is exactly what I was looking for. BTW, I tried CLK_TCK in
place of CLOCKS_PER_SEC and got something that looked more like microseconds
rather than sec. I think I'll stick with CLOCKS_PER_SEC, since I don't
really need that much precision for processes that take 28+ min. (in
parallel over 4 processors) to run. :^)
-Charles
"Charles M. Reinke" <cm******@ece.gatech.edu> writes: <Je***********@physik.fu-berlin.de> wrote in message news:3f************@uni-berlin.de... case? clock() is supposed the CPU time, not the wallclock time, so it should not matter (too much) if the machine is under load or not. Here are the relevant sentences from the C89 standard:
The clock function returns the implementation's best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation. To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLK_TCK. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1.
Please note that here you're asked to divide by CLK_TCK, not CLOCKS_PER_SEC. In the C99 standard it tells you instead to divide by CLOCKS_PER_SEC and if it is defined I would prefer it over CLK_TCK, it also being required by the (newer) POSIX standard.
Regards, Jens
Thanx, this is exactly what I was looking for. BTW, I tried CLK_TCK in place of CLOCKS_PER_SEC and got something that looked more like microseconds rather than sec. I think I'll stick with CLOCKS_PER_SEC, since I don't really need that much precision for processes that take 28+ min. (in parallel over 4 processors) to run. :^)
Watch out for overflow. If clock_t is a 32-bit signed integer type
and CLOCKS_PER_SEC is 1000000 (1e6), then it will overflow in less
than 36 minutes; assuming signed integer overflow wraps around, you
can get repeated results in less than 72 minutes.
This program will show the characteristics of the clock_t type.
#include <time.h>
#include <stdio.h>
#include <limits.h>
int main(void)
{
int bits = sizeof(clock_t) * CHAR_BIT;
if ((clock_t)1 / 2 > 0) {
printf("clock_t is a %d-bit floating-point type\n", bits);
}
else {
printf("clock_t is a %d-bit %s integer type\n",
bits, (clock_t)-1 < 0 ? "signed" : "unsigned");
}
printf("CLOCKS_PER_SEC = %ld\n", (long)CLOCKS_PER_SEC);
return 0;
}
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... Watch out for overflow. If clock_t is a 32-bit signed integer type and CLOCKS_PER_SEC is 1000000 (1e6), then it will overflow in less than 36 minutes; assuming signed integer overflow wraps around, you can get repeated results in less than 72 minutes.
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kst> San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
Thanx for the warning. I've already experienced overflow when running on
only 1 processor, but it gave a negative number so knew what was going on
immediately. I didn't know how many times it had wrapped around though (and
it wasn't worth the effort to code around the problem), so I just threw
those results out.
<OT>
BTW, I noticed you're at SDSC--I'll be at ORNL this summer to test the 3D
parallel version of our code on their machines (current version is 2D).
It'll be *fun* to get off the beowulf cluster and work with a "real"
supercomputer for a while.
</OT>
-Charles This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: peterbe |
last post by:
What's the difference between time.clock() and time.time()
(and please don't say clock() is the CPU clock and time() is the actual
time because that doesn't help me at all :)
I'm trying to...
|
by: bluekite2000 |
last post by:
I ran the following code for a square matrix whose size ranges from 1x1
to 1000x1000
start=clock();
//sequential access time measurement
for (i=0;i<Msize;i++)
for(j=0;j<Nsize;j++)
{...
|
by: Dave H |
last post by:
I have a generic class similar to below that is responsible for returning
classes as Interfaces, where IBook and ICard are known interfaces.
public sealed class StoreManager {
private...
|
by: Steve |
last post by:
I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in
my VC++ 6.0 projects to measure correct time spans, for example for timeouts,
how much time takes to execute certain...
|
by: AndersBj |
last post by:
I have a web application (asp.net) that uses Forms authentication to
authenticate users against an Active Directory. The problem I have is that
most of the time this works great, but every once in...
|
by: saurabh.ss |
last post by:
Hi
I want to measure the time required to execute a piece of code.
For example, I want to know the time required to execute a given
function. How do I do it within C++? I know I got to use a...
|
by: tommyjoe |
last post by:
I am stuck. I can't get the program to show the tenths of a second correct. Any help would be appreciated! Here is the code:
Option Compare Database
Option Explicit
Private Sub Form_Timer()...
|
by: anthonypolidori |
last post by:
i need to measure time in c# please tell me how to do it
how do i print statements
i need to create fault s
this is console writing
|
by: reginaldmerritt |
last post by:
I'm in the process of planning out a database that can organise our staff bookings. Basically having the ability to link staff to certain clients at chosen periods of time.
For example...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |