home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / imc9102.zip / TIMER.C < prev   
C/C++ Source or Header  |  1991-01-07  |  2KB  |  69 lines

  1. /****************************************************************
  2. * TIMER.C -- This program times the execution of a function        *
  3. * "testfunc" which contains the code to be timed                *
  4. *                                                                *
  5. * To compile: "cl /Ox TIMER.C /Od TEST.C"                        *
  6. * Where you supply TEST.C (with the "emptyfunc" and "testfunc"    *
  7. * functions).                                                    *
  8. * RHS 9/17/90                                                    *
  9. ****************************************************************/
  10. #include <time.h>
  11. #include <process.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #define DEF_ITERATIONS   100000L
  16. #define TRUE 1
  17. #define FALSE 0
  18.  
  19. void testfunc(void);
  20. void emptyfunc(void);
  21. void cdecl main(int argc, char **argv);
  22.  
  23. void cdecl main(int argc, char **argv)
  24.     {
  25.     long start=0L, end=0L, control_start=0L, control_end=0L;
  26.     unsigned long iterations, setting =  DEF_ITERATIONS;
  27.     unsigned def = FALSE;
  28.     float test,empty,op;
  29.  
  30.     if(argc < 2)
  31.         def = TRUE;
  32.     else
  33.         {
  34.         if(!atol(argv[1]))
  35.             def = TRUE;
  36.         else
  37.             setting = atol(argv[1]);
  38.         }
  39.  
  40.     if(def)
  41.         printf("Testing with default iterations: %lu\n",setting);
  42.     else
  43.         printf("Testing with %lu iterations\n",setting);
  44.  
  45.     puts("Timing empty function...");
  46.     iterations = setting;
  47.     control_start = clock();
  48.     for( ; iterations; iterations--)
  49.         emptyfunc();
  50.     control_end = clock();
  51.     empty = (float)(control_end-control_start);
  52.     empty /= CLK_TCK;
  53.  
  54.     puts("Testing code...");
  55.     iterations = setting;
  56.     start = clock();
  57.     for( ; iterations; iterations--)
  58.         testfunc();
  59.     end = clock();
  60.     test = (float)(end-start);
  61.     test /= CLK_TCK;
  62.     op = (test-empty);
  63.  
  64.     printf("Test end:\n");
  65.     printf("          Test required: %04.02f seconds\n",test);
  66.     printf("Empty function required: %04.02f \n",empty);
  67.     printf("Operation test required: %04.02f \n",op);
  68.     }
  69.