Re: CPU time versus wall clock time

From: Alex Vinokur (alexvn_at_bigfoot.com)
Date: 10/30/03


Date: Thu, 30 Oct 2003 07:10:09 +0200


"Scott Kelly" <scottjk@softhome.net> wrote in message news:80m0qvopu4cijkube7hv6tl385ep3h30uk@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
     mailto:alexvn@connect.to
     http://mathforum.org/library/view/10978.html
     news://news.gmane.org/gmane.comp.lang.c++.perfometer
   =====================================


Relevant Pages

  • Re: Uncle assAl: (SR) Lorentz t, x = Intervals
    ... > and find lists of experiments purporting to support relativity. ... > would disagree with the GPS clock? ... to this day GPS satellites ... <Snip my ignored reasoning.> ...
    (sci.math)
  • Re: Uncle assAl: (SR) Lorentz t, x = Intervals
    ... > and find lists of experiments purporting to support relativity. ... > would disagree with the GPS clock? ... to this day GPS satellites ... <Snip my ignored reasoning.> ...
    (sci.physics)
  • Re: Uncle assAl: (SR) Lorentz t, x = Intervals
    ... > and find lists of experiments purporting to support relativity. ... > would disagree with the GPS clock? ... to this day GPS satellites ... <Snip my ignored reasoning.> ...
    (sci.physics.relativity)
  • Re: OT-Fuck me
    ... Vikings seems to have used `sun compasses' - actual instruments. ... length of the day was determined by the rotation of the earth relative to ... clock, in about 1637, but never got it working. ...
    (uk.people.support.depression)
  • Re: The earth is flat... why not the universe?
    ... [snip internet path overhead] ... A clock is a regular oscillator and a counter. ... For each oscillator there is a count ratio to ...
    (sci.physics.relativity)