Subject: using times()
From: [email-masked] (John Mamer)

21 Sep 1997 12:48:50 -0700
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
To: [email-masked]

Hi there!

Ive encountered a problem in trying to call the times() 
function under gnuwin32.  My understanding of the times function 
is that it takes as an argument a pointer to a structure of type 
tms, this structure is generally of the form:

struct tms{
     clock_t tms_utime;
     clock_t tms_stime;
     clock_t tms_cutime;
     clock_t tms_cstime;
};
Where clock_t is the appropriate type for a clock tick (unsigned 
long) the member tms_utime is the time in clock ticks
expent by the user process, tms_stime is the
 system time so far for this process, tms_cutime is the user time
spent on child processes, and tms_cstime is the system time for
child processes. The  function times() is supposed to return the
number of clock ticks since the system last restarted.
I executed the following code, which times a simple loop.  In the 
code below, I wanted to create a simple timer function that would 
return a double indicating the number of clock ticks spent on the 
user process.  

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <sys/times.h>

double jtime()
     /*This function is supposed to encapsulate the times() function and=20
return a double containing the user time as reported by times().
*/
{
  struct tms otime;
  unsigned long int irr;
  double rr;
  /*Print out buffers before*/
  printf("Before calling times()\n");
  printf("        Unsigned:                  Double: \n");
  printf("User:     %u                        %.3lf  \n",
                      otime.tms_utime,(double)otime.tms_utime);
  printf("System:   %u                        %.3lf  \n",
                      otime.tms_stime,(double)otime.tms_stime);
  printf("Children: %u                        %.3lf  \n",
                      otime.tms_cutime,(double)otime.tms_cutime);

  /*call time*/

  irr=3Dtimes(&otime);

  /*Print out buffers after*/

  printf("After calling times\n");
  printf("        Unsigned:                  Double: \n");
  printf("User:     %u                        %.3lf  \n",
                      otime.tms_utime,(double)otime.tms_utime);
  printf("System:   %u                        %.3lf  \n",
                      otime.tms_stime,(double)otime.tms_stime);
  printf("Children: %u                        %.3lf  \n",
                      otime.tms_cutime,(double)otime.tms_cutime);

  printf("...returned from times: unsigned: %u double:%lf\n",
   irr,(double)irr);
  return(((double)otime.tms_utime));
}

main ()
{
   int ix,jx,kx;
   double t1, t2;
   t1=3Djtime();
   /* something to time*/
   for (ix=3D1; ix<=3D10000; ix++) {
      kx=3Djx+2-jx*2;
   }
   t2=3Djtime();
   printf(" Elapsed time calculation read from otime.tms_utime:\n");
   printf(" Start: %lf    End: %lf   Elapsed time: %lf\n",t1, t2, t2-t1);
}
     =20

Under gnuwin32 I got the following output:

After calling times
        Unsigned:                  Double:=20
User:     3477706751                        3477706751.000 =20
System:   2031161765                        2031161765.000 =20
Children: 0                        0.000 =20
....returned from times: unsigned: 167754 double:167754.000000

After calling times
        Unsigned:                  Double:=20
User:     4162328443                        4162328443.000 =20
System:   2904355490                        2904355490.000 =20
Children: 0                        0.000 =20
....returned from times: unsigned: 168440 double:168440.000000

 Elapsed time calculation read from otime.tms_utime:
 Start: 3477706751.000    End: 4162328443.000   Elapsed time: 684621692.000

These results are pretty unbelievable, even if you scale clock 
ticks by 1000.

Under Linux on the same machine I got:
After calling times
        Unsigned:                  Double:=20
User:     1                        1.000 =20
System:   1                        1.000 =20
Children: 0                        0.000 =20
....returned from times: unsigned: 9642468 double:9642468.000000

After calling times
        Unsigned:                  Double:=20
User:     47                        47.000 =20
System:   1                        1.000 =20
Children: 0                        0.000 =20
....returned from times: unsigned: 9642514 double:9642514.000000

 Elapsed time calculation read from otime.tms_utime:
 Start: 1.000    End: 47.000   Elapsed time: 46.000

My suspicion is that Ive misinterpreted the structure for the 
buffer, or how to invoke times.

Has anyone else encountered a problem like this?  Or better yet,
figured out how to do this correctly?  Also, what is the correct 
scale factor to go from clock ticks to seconds?  

Thank you
john
----------------------
John Mamer
[email-masked]

----------------------
John Mamer
[email-masked]

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".