home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 161_01 / begint.c < prev    next >
C/C++ Source or Header  |  1985-08-29  |  4KB  |  134 lines

  1. /* TIMING FUNCTIONS: BEGINTIM, ENDTIM, REPORT
  2.  */
  3. #include <stdio.h>
  4. #include "cputim.h"
  5. #include "config.h"
  6.  
  7. #define MINTYPE 1            /* smallest value of t_type except SKIP */
  8. #define MAXTYPE 3            /* largest value of t_type  */
  9. #define SKIP 0                /* skip this excerpt */
  10. #define SKIPTIME -999.99    /* time value for skipped sample */
  11.  
  12. extern int *returnp();
  13.  
  14. char *t_s = "";                /* descriptive string for this trial */
  15. long t_reps = 0;            /* number of repetitions this iteration */
  16. double t_minsam = MINSAM;    /* desired minimum sample */
  17. double t_sample[NTRY] = {0};/* array of samples */
  18. short t_try = 0;            /* which trial mumber is being done next */
  19. short t_type = 0;            /* selection of timing method */
  20. char *t_title[NTRY] = {0};    /* array of descriptions for printout */
  21. short t_size[NTRY] = {0};    /* array of code sizes in bytes */
  22. double t_ctime[NTRY] = {0};    /* array of actual times */
  23.  
  24. int calibrun = 0;            /* is this a calibration run? */
  25.  
  26. static double t1 = 0;        /* time needed to run one full sample (usec) */
  27. static short thistry = -1;    /* try last done by begintim */
  28. static int *p1 = NULL;        /* return address for the call to begintim */
  29. static int *p2 = NULL;        /* return address for the call to endtim */
  30.  
  31. #define PARMS struct parms
  32. PARMS
  33.     {
  34.     double time;
  35.     unsigned space;
  36.     };
  37. PARMS testparms[3] = {0};    /* harness parms for stmt, reg-asst, dbl-asst */                                                                                                                                  /*
  38. .PE
  39. .PS 45
  40. /* BEGINTIM - START TIMING FOR ONE TRY */
  41. begintim(arg1)
  42.     int arg1;
  43.     {
  44.     int var1;            /* first auto variable */
  45.     int *pa = &FR_ANCHOR;        /* pointer to "frame anchor" */
  46.     static short first = 1;    /* first time */
  47.     static long i = 0;        /* counter for timing loop */
  48.     FILE *fpin;                /* file for reading testparms */
  49.     if (t_try >= NTRY)
  50.         {    
  51.         printf("reached maximum # of tries; ignored '%s'\n", t_s);
  52.         exit(1);
  53.         }    
  54.     t_title[t_try] = t_s;
  55.     if (t_type == SKIP)
  56.         {
  57.         t_size[t_try] = 0;
  58.         t_ctime[t_try] = SKIPTIME;
  59.         return;
  60.         }
  61.     p1 = returnp(pa);
  62.     thistry = t_try;
  63.     if (first && !calibrun)
  64.         {
  65.         first = 0;
  66.         fpin = fopen("parms.dat", "r");
  67.         if (fpin == NULL)
  68.             error("can't open parms.dat;",
  69.                 "Run  calib >parms.dat  to create parms.dat");
  70.         while (getc(fpin) != '\n')
  71.             ;
  72.         for (i = 0; i <= 2; ++i)
  73.             {
  74.             if (2 > fscanf(fpin, "%*22c %d %lf",
  75.                 &testparms[i].space, &testparms[i].time))
  76.                 error("bad data in", "parms.dat");
  77.             while (getc(fpin) != '\n')
  78.                 ;
  79.             }
  80.         fclose(fpin);
  81.         }
  82.     cputim();
  83.         }                                                                                                                                  /*
  84. .PE
  85. .PS 25
  86. /* ENDTIM - COMPUTE TIMES FOR ONE TRIAL */
  87. endtim(arg1)
  88.     int arg1;
  89.     {
  90.     int var1;                /* first auto variable */
  91.     int *pa = &FR_ANCHOR;    /* pointer to "frame anchor" */
  92.  
  93.     t1 = cputim() / ((double)CLOCK_TICKS_PER_SECOND/1e6);
  94.     if (thistry != t_try)
  95.         return;
  96.     p2 = returnp(pa);
  97.     if (t_type < MINTYPE || MAXTYPE < t_type)
  98.         {
  99.         fprintf(stderr, "wrong t_type: %d; t_try=%d\n", t_type, t_try);
  100.         t_size[t_try] = 0;
  101.         t_ctime[t_try] = SKIPTIME;
  102.         }
  103.     else
  104.         {
  105.         t_size[t_try] = (char *)p2 - (char *)p1 - testparms[t_type-1].space;
  106.         t_ctime[t_try] = t1 / (double)t_reps - testparms[t_type-1].time;
  107.         }
  108.     t_sample[t_try] = t_ctime[t_try] * t_reps;
  109.     }                                                                                                                                  /*
  110. .PE
  111. .PS 22
  112. /* REPORT - PRINT TIMES */
  113. report()
  114.     {
  115.     char *fround();    /* function to round output values */
  116.     short i;        /* counter for printout loop */
  117.  
  118.     printf("%-20s  %6s  %12s\n",
  119.         "Code sample", "Size", "Time (usec)");
  120.     for (i = 0; i < t_try; i++) 
  121.         {
  122.         if (t_ctime[i] == SKIPTIME)
  123.             printf("%-20.20s  %6s  %12s\n", t_title[i], "-", "-");
  124.         else
  125.             {
  126.             if (t_ctime[i] < 0.)
  127.                 t_ctime[i] = 0.;
  128.             printf("%-20.20s  %6d  %12s\n",
  129.                 t_title[i], t_size[i], 
  130.                 fround(t_ctime[i], 3, 3));
  131.             }
  132.         }
  133.     }
  134.