home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_5 / issue_06 / benchmarks / c_source / timer < prev    next >
Encoding:
Text File  |  1991-06-04  |  2.8 KB  |  125 lines

  1. /*
  2.  * Time taking functions for use with the benchmarks.
  3.  *
  4.  * Martin Houston. Apricot Advanced Technology. 9/1/87
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/times.h>
  10. #include "timer.h"
  11.  
  12. long times();
  13.  
  14. static struct tms starttime, endtime;
  15. static long  startticks, endticks;
  16. static int HZ = 50;                /* system HZ value */
  17.  
  18. /*
  19.  * Function to set up the process timer sub-system.
  20.  *
  21.  * HZ value in environment overrides 50 Hz default if present.
  22.  * Assumes that HZ values < 50 are never encountered.
  23.  *
  24.  */
  25.  
  26. void init_timer()
  27. {
  28.     int hz;
  29.  
  30.     if((hz = atoi(getenv("HZ="))) > HZ)
  31.         HZ = hz; /* use HZ value from the environment */
  32. }
  33.  
  34. /*
  35.  * Function to start time accounting.
  36.  *
  37.  * A return value of -1 indicates that the time operation failed
  38.  * to start. the time is returned for correct operation.
  39.  */
  40.  
  41. int start_timer()
  42. {
  43.  
  44.     return(startticks = times(&starttime));
  45. }
  46.  
  47. /*
  48.  * Function to stop time accounting and make a report on elapsed time.
  49.  *
  50.  * A return value of -1 indicates that the time operation failed
  51.  * to start. the time is returned for correct operation.
  52.  */
  53.  
  54. int print_elapsed(message, report_type)
  55. char *message;
  56. int report_type;
  57. {
  58.     long secs, ms, rms;
  59.  
  60.     endticks = times(&endtime);
  61.     if(endticks > 0L) /* timeing whent ok */
  62.     {
  63.         ms = (endticks - startticks) * (1000L / HZ);
  64.         secs = (endticks - startticks) / HZ;
  65.         rms = ms - (secs * 1000);
  66.         if(report_type & REALMS)
  67.         {
  68.             fprintf(stdout,"%s: real %ld ms\n", message, ms);
  69.         }
  70.         if(report_type & REALSEC)
  71.         {
  72.             fprintf(stdout,"%s: real %ld sec %ld ms\n",
  73.                                 message, secs, rms);
  74.         }
  75.         if(report_type & REALMIN)
  76.         {
  77.             fprintf(stdout,"%s: real %ld min %ld sec %ld ms\n",
  78.                                 message,
  79.                 secs / 60L, secs % 60L, rms);
  80.         }
  81.  
  82.         ms = (endtime.tms_utime - starttime.tms_utime) * (1000L / HZ);
  83.         secs = (endtime.tms_utime - starttime.tms_utime) / HZ;
  84.         rms = ms - (secs * 1000);
  85.  
  86.         if(report_type & USERMS)
  87.         {
  88.             fprintf(stdout,"%s: user %ld ms\n", message, ms);
  89.         }
  90.         if(report_type & USERSEC)
  91.         {
  92.             fprintf(stdout,"%s: user %ld sec %ld ms\n",
  93.                                 message, secs, rms);
  94.         }
  95.         if(report_type & USERMIN)
  96.         {
  97.             fprintf(stdout,"%s: user %ld min %ld sec %ld ms\n",
  98.                                 message,
  99.                 secs / 60L, secs % 60L, rms);
  100.         }
  101.  
  102.         ms = (endtime.tms_stime - starttime.tms_stime) * (1000L / HZ);
  103.         secs = (endtime.tms_stime - starttime.tms_stime) / HZ;
  104.         rms = ms - (secs * 1000);
  105.  
  106.         if(report_type & SYSTEMMS)
  107.         {
  108.             fprintf(stdout,"%s: system %ld ms\n", message, ms);
  109.         }
  110.         if(report_type & SYSTEMSEC)
  111.         {
  112.             fprintf(stdout,"%s: system %ld sec %ld ms\n",
  113.                     message, secs, rms);
  114.         }
  115.         if(report_type & SYSTEMMIN)
  116.         {
  117.             fprintf(stdout,"%s: system %ld min %ld sec %ld ms\n",
  118.                                 message,
  119.                 secs / 60L, secs % 60L, rms);
  120.         }
  121.     }
  122.     else
  123.         fprintf(stdout,"%s: timer failed!\n", message);
  124. }
  125.