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

  1. /* avg - compute averages of cpu time
  2.  */
  3. #include <stdio.h>
  4. #include <math.h>
  5. #define NOPRINT 0
  6. #define PRINT 1
  7. #define NAME_MAX 64
  8. main(ac, av)
  9.     int ac;
  10.     char *av[];
  11.     {
  12.     char fname[NAME_MAX];
  13.     double atof();
  14.     double avg;
  15.     double favg();
  16.     double trial;
  17.  
  18.     if (ac != 3)
  19.         error("usage: avg filename trial", "");
  20.     strcpy(fname, av[1]);
  21.     trial = atof(av[2]);
  22.     avg = favg(fname, trial, 5., NOPRINT);
  23.     avg = favg(fname, avg, 4., NOPRINT);
  24.     avg = favg(fname, avg, 3., NOPRINT);
  25.     avg = favg(fname, avg, 3., PRINT);
  26.     }
  27. /* favg - compute avg of all entries within factor of prev avg
  28.  */
  29. double favg(fname, prev, factor, doprint)
  30.     char fname[];    /* file name */
  31.     double prev;    /* previous average */
  32.     double factor;    /* acceptance factor */
  33.     int doprint;    /* print resulting table? */
  34.     {
  35.     char buf[100];
  36.     char timestr[20];
  37.     double avg;    /* average of selected samples */
  38.     double fit;    /* how close is sample */
  39.     double sum;    /* sum of samples */
  40.     double x;
  41.     short n;    /* number of samples */
  42.     FILE *fp;
  43.     FILE *fopen();
  44.  
  45.     sum = 0.;
  46.     n = 0;
  47.     fp = fopen(fname, "r");
  48.     if (fp == NULL)
  49.         error("can't open", fname);
  50.     fgets(buf, sizeof(buf), fp);    /* skip heading */
  51.     while (fgets(buf, sizeof(buf), fp))
  52.         {
  53.         sscanf(&buf[29], "%s", timestr);
  54.         if (strcmp(timestr, "-") == 0 || sscanf(&buf[29], "%lf", &x) < 1)
  55.             continue;
  56.         fit = x / prev;
  57.         if (fit < 1. && fit != 0.)
  58.             fit = 1/fit;
  59.         if (fit < factor && fit != 0.)
  60.             {
  61.             sum = sum + x;
  62.             ++n;
  63.             }
  64.         else if (doprint)
  65.             {
  66.             printf("%29.29s %12s\n", buf, fround(x, 3, 3));
  67.             }
  68.         }
  69.     if (n > 0)
  70.         avg = sum / n;
  71.     if (doprint)
  72.         printf("%28.28s %12s\n",
  73.             "Average of other samples", fround(avg, 3, 3));
  74.     fclose(fp);
  75.     return (avg);
  76.     }
  77.