Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CPU time versus wall clock time

6 views
Skip to first unread message

Alex Vinokur

unread,
Oct 29, 2003, 11:27:48 AM10/29/03
to

"Scott Kelly" <sco...@softhome.net> wrote in message news:nflvpv873qlqrr2hl...@4ax.com...
> It might be off topic, but it's a burning question in my heart. :)
> What is CPU time, and how does it relate to wall clock time.
>
> I think it's a good question because it will provide me with a context
> in the future when I start messing with time based programs later.
>
> Thanks
>
> Scott Kelly

See
http://groups.google.com/groups?selm=bmpe5o%24ph16o%241%40ID-79865.news.uni-berlin.de
http://groups.google.com/groups?selm=bmr7ng%24q2eho%241%40ID-79865.news.uni-berlin.de


=====================================
Alex Vinokur
mailto:ale...@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================

Jerry Coffin

unread,
Oct 29, 2003, 11:15:57 PM10/29/03
to
In article <80m0qvopu4cijkube...@4ax.com>,
sco...@softhome.net says...

[ ... ]

> Considering I can't read C++ (yet), and yes, I'm a novice at C, that
> didn't help. It just showed me that clock time and cpu time can
> differ.

It's only vaguely related to C or C++ (or any programming language per
se) but the basic idea is pretty simple: clock time is often known as
wall time -- it's the period of time taken from starting a task to
finishing it.

CPU time is only the amount of time that the CPU itself is actually
working on your problem. It doesn't include things like waiting for
data to be read in from the disk, or to come across a network, or time
that the CPU is executing other tasks.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Alex Vinokur

unread,
Oct 30, 2003, 12:10:09 AM10/30/03
to

"Scott Kelly" <sco...@softhome.net> wrote in message news:80m0qvopu4cijkube...@4ax.com...
[snip]

> Considering I can't read C++ (yet), and yes, I'm a novice at C, that
> didn't help. It just showed me that clock time and cpu time can
> differ.
[snip]

OK.

Here is a C related sample.

time() and clock() return different values.

* `time' -- get current calendar time (as single number) - from 'man time'.
Returned value is number of _seconds_.
Elapsed (_wall clock_) time is = end_current_calendar_time - start_current_calendar_time.

* `clock' -- cumulative processor time
Returned value is number of _units_ defined by the machine-dependent macro `CLOCKS_PER_SEC'.
_Processor_ time used is = end_clock_units - start_clock_units.


=======================================
GNU gcc version 3.3.1 (cygming special)
=======================================

========= C++ code : File foo.c : BEGIN =========

#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <unistd.h> // UNIX

int main()
{
time_t start_time, end_time;
clock_t start_clock, end_clock;

clock_t diff_clock;
time_t diff_time;


start_time = time(NULL);
if (start_time == (time_t)(-1))
{
fprintf (stderr, "Unable to get start time()\n");
return 1;
}

start_clock = clock();
if (start_clock == (clock_t)(-1))
{
fprintf (stderr, "Unable to get start clock()\n");
return 1;
}


sleep (3); // do nothing 3 seconds; UNIX, system call

end_time = time(NULL);
if (end_time == (time_t)(-1))
{
fprintf (stderr, "Unable to get end time()\n");
return 1;
}

end_clock = clock();
if (end_clock == (clock_t)(-1))
{
fprintf (stderr, "Unable to get end clock()\n");
return 1;
}


// -------------------------
assert (!(start_time == (time_t)-1));
assert (!(end_time == (time_t)-1));

assert (!(start_clock == (clock_t)-1));
assert (!(end_clock == (clock_t)-1));

// -------------------------
diff_time = end_time - start_time;
diff_clock = end_clock - start_clock;

printf ("time : start_time = %lu sec, end_time = %lu sec\n", start_time, end_time);
printf ("time : elapsed (wall clock) time = %lu sec\n", diff_time);

printf ("\n");
printf ("clock : start_clock = %lu units, end_clock = %lu units\n", start_clock, end_clock);
printf ("clock : processor time used = %.3f sec\n", (double)diff_clock/CLOCKS_PER_SEC);


return 0;
}


========= C++ code : File foo.c : END ===========

========= Compilation & Run : BEGIN ===========

$ gcc -W -Wall foo.c

$ a

time : start_time = 1067489895 sec, end_time = 1067489898 sec
time : elapsed (wall clock) time = 3 sec

clock : start_clock = 10 units, end_clock = 30 units
clock : processor time used = 0.020 sec

========= Compilation & Run : END =============


We can see that
* elapsed (wall clock) time (in other words - real time) is 3 sec,
* processor time used is only 0.020 sec.

--

Alex Vinokur

unread,
Oct 30, 2003, 9:33:24 AM10/30/03
to

"Alex Vinokur" <ale...@bigfoot.com> wrote in message news:bnq6ff$14a5oc$1...@ID-79865.news.uni-berlin.de...
[snip]

> Here is a C related sample.
[snip]

Another sample.

=======================================
CYGWIN_NT-5.0 1.5.4(0.94/3/2)


GNU gcc version 3.3.1 (cygming special)
=======================================


========= C++ code : File foo.c : BEGIN =========

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

// ---------------
int double_less (const void* first, const void* second)
{
if ( (*(const double*) first) < (*(const double*) second) ) return -1;
if ( (*(const double*) first) > (*(const double*) second) ) return 1;
return 0;
}

// ---------------
#define MAX_ARRAY_SIZE 100000

int main(int argc, char** argv)


{
time_t start_time, end_time;
clock_t start_clock, end_clock;

clock_t diff_clock;
time_t diff_time;

double array[MAX_ARRAY_SIZE];

unsigned int actual_size = 0;
unsigned int repetitions = 0;

unsigned int i, k;


printf ("\nYOUR COMMAND LINE IS : ");
for (i = 0; i < argc; i++) printf ("%s ", argv[i]);
printf ("\n\n");

if (argc < 3)
{
fprintf (stderr, "USAGE : %s <array size> <repetitions>\n\n", argv[0]);
return 1;
}
assert (argc >= 3);

actual_size = atoi (argv[1]);
repetitions = atoi (argv[2]);

if (!(actual_size < MAX_ARRAY_SIZE))
{
fprintf (stderr, "ERROR : array size is too big : %lu; must be < %lu\n\n", actual_size, MAX_ARRAY_SIZE);
return 1;
}


// -------------------------
// -------------------------


start_time = time(NULL);
if (start_time == (time_t)(-1))
{
fprintf (stderr, "Unable to get start time()\n");
return 1;
}

start_clock = clock();
if (start_clock == (clock_t)(-1))
{
fprintf (stderr, "Unable to get start clock()\n");
return 1;
}


// -------------------------
for (k = 0; k < repetitions; k++)
{
srand (clock());
for (i = 0; i < actual_size; i++) array[i] = (double) rand();

qsort (array, actual_size, sizeof (double), double_less);
}
// -------------------------

end_time = time(NULL);
if (end_time == (time_t)(-1))
{
fprintf (stderr, "Unable to get end time()\n");
return 1;
}

end_clock = clock();
if (end_clock == (clock_t)(-1))
{
fprintf (stderr, "Unable to get end clock()\n");
return 1;
}
// -------------------------

// -------------------------

// -------------------------
assert (!(start_time == (time_t)-1));
assert (!(end_time == (time_t)-1));

assert (!(start_clock == (clock_t)-1));
assert (!(end_clock == (clock_t)-1));

// -------------------------
diff_time = end_time - start_time;
diff_clock = end_clock - start_clock;

printf ("time : start_time = %lu sec, end_time = %lu sec\n", start_time, end_time);
printf ("time : elapsed (wall clock) time = %lu sec\n", diff_time);

printf ("\n");
printf ("clock : start_clock = %lu units, end_clock = %lu units\n", start_clock, end_clock);
printf ("clock : processor time used = %.3f sec\n", (double)diff_clock/CLOCKS_PER_SEC);


return 0;
}

========= C++ code : File foo.c : END ===========


### Test-1. ###
Invoke the executable file in some console.

========= Run-1 : BEGIN ===========

$ a 10000 2500

YOUR COMMAND LINE IS : a 10000 2500

time : start_time = 1067510402 sec, end_time = 1067510419 sec
time : elapsed (wall clock) time = 17 sec

clock : start_clock = 20 units, end_clock = 17164 units
clock : processor time used = 17.144 sec

========= Run-1 : END =============

Conclusions.
1.1. 'elapsed (wall clock) time' and 'processor time used' are nearly equal.
This means that almost nobody completes with our invoked program in CPU time.
1.2. Odd relation
'elapsed (wall clock) time' < processor time used'
can be explain by the different accuracies of time() and clock() :
* time()'s accuracy is 1 second,
* clock()'s accuracy is defined by the machine-dependent macro `CLOCKS_PER_SEC';
in particular, for Cygwin CLOCKS_PER_SEC = 1000

### Test-2. ###
Now invoke the same executable file in two consoles simultaneously.

========= Run-2 : BEGIN ===========

--- Console-1 (Invocation-1) ---

$ a 10000 2500

YOUR COMMAND LINE IS : a 10000 2500

time : start_time = 1067510479 sec, end_time = 1067510513 sec
time : elapsed (wall clock) time = 34 sec

clock : start_clock = 20 units, end_clock = 17314 units
clock : processor time used = 17.294 sec

--------------------------------


--- Console-2 (Invocation-2) ---

$ a 10000 2500

YOUR COMMAND LINE IS : a 10000 2500

time : start_time = 1067510481 sec, end_time = 1067510504 sec
time : elapsed (wall clock) time = 23 sec

clock : start_clock = 10 units, end_clock = 17024 units
clock : processor time used = 17.014 sec

--------------------------------

========= Run-2 : END =============

Conclusions.
2.1. 'elapsed (wall clock) time' is significantly greater than 'processor time used' for each invocation.
That can be explain by competition between two invocations (of the same program) in the CPU time.

2.2. 'processor time used' is nearly equal for three invocations in Test-1 and Test-2.
This means that clock() works per invocation in contrast to time().

Martijn Lievaart

unread,
Oct 30, 2003, 10:48:51 AM10/30/03
to
On Thu, 30 Oct 2003 04:15:57 +0000, Jerry Coffin wrote:

> In article <80m0qvopu4cijkube...@4ax.com>,
> sco...@softhome.net says...
>
> [ ... ]
>
>> Considering I can't read C++ (yet), and yes, I'm a novice at C, that
>> didn't help. It just showed me that clock time and cpu time can
>> differ.
>
> It's only vaguely related to C or C++ (or any programming language per
> se) but the basic idea is pretty simple: clock time is often known as
> wall time -- it's the period of time taken from starting a task to
> finishing it.

Not completely. Clock time is whatever your timing routine returns. It may
or may not be the same as wall time. Wall time is the time the clock on
the wall tells you, so the 'real' time.

HTH,
M4

Mike Wahler

unread,
Oct 30, 2003, 2:17:16 PM10/30/03
to

"Scott Kelly" <sco...@softhome.net> wrote in message
news:80m0qvopu4cijkube...@4ax.com...
> Considering I can't read C++ (yet), and yes, I'm a novice at C, that
> didn't help. It just showed me that clock time and cpu time can
> differ.
>
> What is doesn't show me is why.

An analogy:

A man is digging a hole.
He starts at 8:00.
He takes a break for rest at 9:00.
He starts digging again at 9:30.
He finishes at 10:00

The time between the start and end of the job
is 2 hours of 'real' or 'wall' or 'clock' time.

The 'worker' (e.g. 'processor') time consumed by the
job is 1.5 hours. For .5 hours, the 'worker' was
doing something else, so that time is not alloted
to the total job time.

-Mike


r norman

unread,
Nov 1, 2003, 3:50:02 PM11/1/03
to
On Sat, 01 Nov 2003 20:30:59 GMT, Scott Kelly <sco...@softhome.net>
wrote:
>On Thu, 30 Oct 2003 19:17:16 GMT, "Mike Wahler"
><mkwa...@mkwahler.net> wrote:
>\
<snip>

>>
>>A man is digging a hole.
>>He starts at 8:00.
>>He takes a break for rest at 9:00.
>>He starts digging again at 9:30.
>>He finishes at 10:00
>>
>>The time between the start and end of the job
>>is 2 hours of 'real' or 'wall' or 'clock' time.
>>
>>The 'worker' (e.g. 'processor') time consumed by the
>>job is 1.5 hours. For .5 hours, the 'worker' was
>>doing something else, so that time is not alloted
>>to the total job time.
>>
>>-Mike
>>

>Sounds like many jobs I've had.
>
>Thanks Mike.

Then there's "billable time" which an entirely different thing!

0 new messages