home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctech / hlsrc / hltimer.c < prev    next >
C/C++ Source or Header  |  1988-09-09  |  2KB  |  92 lines

  1. /*+
  2.     Name:       HLTIMER.C
  3.     Author:     Kent J. Quirk
  4.         (c) Copyright 1988 Ziff Communications Co.
  5.     Abstract:   This file contains timer routines for
  6.         use in benchmarking.
  7.     History:    kjq - Mar 88 - Original Version
  8.         kjq - Sep 88 - Version 1.00
  9. -*/
  10.  
  11. #ifdef C_terp
  12. #define NDEBUG
  13. #endif
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <bios.h>
  18. #include <assert.h>
  19. #include "hl.h"
  20.  
  21. #define MAXTIMERS 10
  22. static long start_tick[MAXTIMERS];
  23. static long end_tick;
  24.  
  25. /**** s t a r t _ t i m e r ****
  26.     This routine simply records the current time in a static array
  27.     (indexed by an input parameter) for later use by end_timer.
  28.  *******************************/
  29. void start_timer(n)
  30. int n;
  31. {
  32.     assert(((n >= 0) && (n < MAXTIMERS)))
  33.     _bios_timeofday(_TIME_GETCLOCK, &(start_tick[n]));
  34. }
  35.  
  36. /**** s t o p _ t i m e r ****
  37.     Saves the current time in end timer.
  38.  *****************************/
  39. void stop_timer()
  40. {
  41.     _bios_timeofday(_TIME_GETCLOCK, &end_tick);
  42. }
  43.  
  44. /**** g e t _ t i m e r ****
  45.     Finds the time difference between the end_timer and the selected
  46.     start_timer.
  47.  ****************************/
  48. long get_timer(n)
  49. int n;
  50. {
  51.     assert((n >= 0) && (n < MAXTIMERS));
  52.     return(end_tick - start_tick[n]);
  53. }
  54.  
  55. char *time_pct(tick1, tick2)
  56. long tick1, tick2;
  57. {
  58.     static char buf[40];
  59.     static int next = 0;
  60.     ldiv_t rel;
  61.  
  62.     next = (next + 10) % sizeof(buf);
  63.     if ((tick1 < 0) | (tick2 < 0))
  64.         strcpy(buf + next, " N/A  ");
  65.     else
  66.     {
  67.         rel = ldiv(tick2*100L, tick1);
  68.         sprintf(buf + next, "%4ld%%", rel.quot);
  69.     }
  70.     return(buf + next);
  71. }
  72.  
  73. char *time_secs(ticks)
  74. long ticks;
  75. {
  76.     ldiv_t secs, fracs;
  77.     static char buf[40];
  78.     static int next = 0;
  79.  
  80.     next = (next + 10) % sizeof(buf);
  81.     if (ticks < 0)
  82.         strcpy(buf + next, " N/A  ");
  83.     else
  84.     {
  85.         ticks *= 5L;        /* so we can divide by 91 and be accurate */
  86.         secs = ldiv(ticks, 91L);    /* 91 = 18.2 ticks/sec * 5 */
  87.         fracs = ldiv(secs.rem * 100L, 91L);
  88.         sprintf(buf + next, "%3ld.%02ld", secs.quot, fracs.quot);
  89.     }
  90.     return(buf + next);
  91. }
  92.