home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / common / time.c < prev    next >
C/C++ Source or Header  |  2000-07-29  |  1KB  |  56 lines

  1. #include "../h/gsupport.h"
  2.  
  3. /*
  4.  * millisec - returns execution time in milliseconds. Time is measured
  5.  *  from the function's first call. The granularity of the time is
  6.  *  generally more than one millisecond and on some systems it my only
  7.  *  be accurate to the second.
  8.  */
  9.  
  10. #if UNIX
  11.  
  12. /*
  13.  * For some unfathomable reason, the Open Group's "Single Unix Specification"
  14.  *  requires that the ANSI C clock() function be defined in units of 1/1000000
  15.  *  second.  This means that the result overflows a 32-bit signed clock_t
  16.  *  value only about 35 minutes.  So, under UNIX, we use the POSIX standard
  17.  *  times() function instead.
  18.  */
  19.  
  20. static long cptime()
  21.    {
  22.    struct tms tp;
  23.    times(&tp);
  24.    return (long) (tp.tms_utime + tp.tms_stime);
  25.    }
  26.  
  27. long millisec()
  28.    {
  29.    static long starttime = -2;
  30.    long t;
  31.  
  32.    t = cptime();
  33.    if (starttime == -2)
  34.       starttime = t;
  35.    return (long) ((1000.0 / CLK_TCK) * (t - starttime));
  36.    }
  37.  
  38. #else                    /* UNIX */
  39.  
  40. /*
  41.  * On anything other than UNIX, just use the ANSI C clock() function.
  42.  */
  43.  
  44. long millisec()
  45.    {
  46.    static clock_t starttime = -2;
  47.    clock_t t;
  48.  
  49.    t = clock();
  50.    if (starttime == -2)
  51.       starttime = t;
  52.    return (long) ((1000.0 / CLOCKS_PER_SEC) * (t - starttime));
  53.    }
  54.  
  55. #endif                    /* UNIX */
  56.