home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SPEED.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  150 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /******** CALCULATE I/O PERFORMANCE TO NUL FILE ********/
  4.  
  5. #include <dos.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "errors.h"           /* For cant()     */
  9.  
  10. #define CHK 100L              /* speed factor   */
  11.  
  12. long ticks(long tick)                                   /* GET BIOS TIME TICK */
  13. {
  14.         union REGS reg;
  15.         reg.h.ah=0;
  16.         int86(0x1A, ®, ®);
  17.         return ((long)reg.x.cx<<16)+reg.x.dx-tick;
  18. }
  19.  
  20. long time_it(void(*func)(void))
  21. {
  22.         long t = ticks(0L);
  23.         (*func)();
  24.         return ticks(t);
  25. }
  26.  
  27. void show_it(long t)
  28. {
  29.         long lquot, lrem;
  30.         t = (t*1000/182+5)/10;
  31.         lquot = t/10;
  32.         lrem = t%10;
  33.         printf("%3ld.%02d sec", lquot, (int)lrem);
  34. }
  35.  
  36. void t_printf(void)
  37. {
  38.         register FILE *fp;
  39.         register unsigned u;
  40.  
  41.         fp = cant("NUL", "wt");
  42.         for (u=0; u<50*CHK; ++u)
  43.                 fprintf(fp, "Now is %d time for %d little indians\n", 123, -456);
  44.         fclose(fp);
  45. }
  46.  
  47. void b_printf(void)
  48. {
  49.         register FILE *fp;
  50.         register unsigned u;
  51.  
  52.         fp = cant("NUL", "wb");
  53.         for (u=0; u<50*CHK; ++u)
  54.                 fprintf(fp, "Now is %d time for %d little indians\n", 123, -456);
  55.         fclose(fp);
  56. }
  57.  
  58. void tu_printf(void)
  59. {
  60.         register FILE *fp;
  61.         register unsigned u;
  62.  
  63.         fp = cant("NUL", "wt");
  64.         setbuf(fp, NULL);
  65.         for (u=0; u<5*CHK; ++u)
  66.                 fprintf(fp, "Now is %d time for %d little indians\n", 123, -456);
  67.         fclose(fp);
  68. }
  69.  
  70. void bu_printf(void)
  71. {
  72.         register FILE *fp;
  73.         register unsigned u;
  74.  
  75.         fp = cant("NUL", "wb");
  76.         setbuf(fp, NULL);
  77.         for (u=0; u<5*CHK; ++u)
  78.                 fprintf(fp, "Now is %d time for %d little indians\n", 123, -456);
  79.         fclose(fp);
  80. }
  81.  
  82. void t_write(void)
  83. {
  84.         register FILE *fp;
  85.         register unsigned u;
  86.  
  87.         fp = cant("NUL", "wt");
  88.         for (u=0; u<250*CHK; ++u)
  89.                 fwrite("Now is the time for all good men to come\n", 41, 1, fp);
  90.         fclose(fp);
  91. }
  92.  
  93. void b_write(void)
  94. {
  95.         register FILE *fp;
  96.         register unsigned u;
  97.  
  98.         fp = cant("NUL", "wb");
  99.         for (u=0; u<500*CHK; ++u)
  100.                 fwrite("Now is the time for all good men to come\n", 41, 1, fp);
  101.         fclose(fp);
  102. }
  103.  
  104. void tu_write(void)
  105. {
  106.         register FILE *fp;
  107.         register unsigned u;
  108.  
  109.         fp = cant("NUL", "wt");
  110.         setbuf(fp, NULL);
  111.         for (u=0; u<100*CHK; ++u)
  112.                 fwrite("Now is the time for all good men to come\n", 41, 1, fp);
  113.         fclose(fp);
  114. }
  115.  
  116. void bu_write(void)
  117. {
  118.         register FILE *fp;
  119.         register unsigned u;
  120.  
  121.         fp = cant("NUL", "wb");
  122.         setbuf(fp, NULL);
  123.         for (u=0; u<200*CHK; ++u)
  124.                 fwrite("Now is the time for all good men to come\n", 41, 1, fp);
  125.         fclose(fp);
  126. }
  127.  
  128. main(void)
  129. {
  130.         show_it(time_it(t_printf));
  131.         printf(": time for text printf buffered\n");
  132.         show_it(time_it(b_printf));
  133.         printf(": time for binary printf buffered\n");
  134.         show_it(time_it(tu_printf));
  135.         printf(": time for text printf unbuffered\n");
  136.         show_it(time_it(bu_printf));
  137.         printf(": time for binary printf unbuffered\n");
  138.  
  139.         show_it(time_it(t_write));
  140.         printf(": time for text write buffered\n");
  141.         show_it(time_it(b_write));
  142.         printf(": time for binary write buffered\n");
  143.         show_it(time_it(tu_write));
  144.         printf(": time for text write unbuffered\n");
  145.         show_it(time_it(bu_write));
  146.         printf(": time for binary write unbuffered\n");
  147.  
  148.         return 0;
  149. }
  150.