home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / samprog2.zip / tmr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-11  |  6.2 KB  |  107 lines

  1. /********************************************************************/
  2.  
  3. /*                                                                  */
  4.  
  5. /* (C) Copyright IBM UK Ltd. 1996                                   */
  6.  
  7. /*                                                                  */
  8.  
  9. /* TMR   - Example of DosTmrQueryTime and DosTmrQueryFreq usage.    */
  10.  
  11. /*                                                                  */
  12.  
  13. /* Query the high resolution timer.                                 */
  14.  
  15. /* Richard Moore 16th November 98                                   */
  16.  
  17. /* Version 1.0                                                      */
  18.  
  19. /*                                                                  */
  20.  
  21. /* Syntax TMR <none>                                                */
  22.  
  23. /*                                                                  */
  24.  
  25. /*                                                                  */
  26.  
  27. /********************************************************************/
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. #define INCL_DOSPROFILE
  36.  
  37.  
  38.  
  39. #include <os2.h>
  40.  
  41. #include <conio.h>
  42.  
  43. #include <stdio.h>
  44.  
  45. #include <string.h>
  46.  
  47. #include <stdlib.h>
  48.  
  49.  
  50.  
  51. /*************************************************************************************/
  52.  
  53. /*                                                                                   */
  54.  
  55. /* Purpose:                                                                          */
  56.  
  57. /*                                                                                   */
  58.  
  59. /* DosTmrQueryTime queries the 64-bit high resolution timer.                         */
  60.  
  61. /*                                                                                   */
  62.  
  63. /* DosTmrQueryFreq queries the frequency of the high resolution timer.               */
  64.  
  65. /* Subtracting two 64-bit times and dividing by the frequency will give the          */
  66.  
  67. /* high resolution time interval in seconds.                                         */
  68.  
  69. /*                                                                                   */
  70.  
  71. /*************************************************************************************/
  72.  
  73.  
  74.  
  75. /*************************************************************************************/
  76.  
  77. /*                                                                                   */
  78.  
  79. /*      Prototype definitions for DosTmrQueryTime and DosTmrQueryFreq                */
  80.  
  81. /*                                                                                   */
  82.  
  83. /*************************************************************************************/
  84.  
  85. /*                                                                                   */
  86.  
  87. /*   APIRET APIENTRY DosTmrQueryTime(PQWORD time);                                   */
  88.  
  89. /*   APIRET APIENTRY DosTmrQueryFreq(PULONG freq);                                   */
  90.  
  91. /*                                                                                   */
  92.  
  93. /*************************************************************************************/
  94.  
  95.  
  96.  
  97. /*************************************************************************************/
  98.  
  99. /*                                                                                   */
  100.  
  101. /* DosTmrQueryTime returns the following return codes:                               */
  102.  
  103. /*                                                                                   */
  104.  
  105. /*************************************************************************************/
  106.  
  107. /*                                                                                   */
  108.  
  109. /* 0       NO_ERROR                                                                  */
  110.  
  111. /* 87      ERROR_INVALID_PARAMETER                                                   */
  112.  
  113. /* 99      ERROR_DEVICE_IN_USE                                                       */
  114.  
  115. /* 535     ERROR_TMR_NO_DEVICE                                                       */
  116.  
  117. /* 536     ERROR_TMR_INVALID_TIME                                                    */
  118.  
  119. /*                                                                                   */
  120.  
  121. /*************************************************************************************/
  122.  
  123.  
  124.  
  125. /*************************************************************************************/
  126.  
  127. /*                                                                                   */
  128.  
  129. /* DosTmrQueryFreq returns the following return codes:                               */
  130.  
  131. /*                                                                                   */
  132.  
  133. /*************************************************************************************/
  134.  
  135. /*                                                                                   */
  136.  
  137. /* 0       NO_ERROR                                                                  */
  138.  
  139. /* 87      ERROR_INVALID_PARAMETER                                                   */
  140.  
  141. /* 99      ERROR_DEVICE_IN_USE                                                       */
  142.  
  143. /* 535     ERROR_TMR_NO_DEVICE                                                       */
  144.  
  145. /*                                                                                   */
  146.  
  147. /*************************************************************************************/
  148.  
  149.  
  150.  
  151. void int3(void);
  152.  
  153.  
  154.  
  155. int main(int argc, char *argv[], char *envp[])
  156.  
  157. {
  158.  
  159.    QWORD start_time;  
  160.  
  161.    QWORD end_time;  
  162.  
  163.    QWORD interval;
  164.  
  165.  
  166.  
  167.    ULONG freq;
  168.  
  169.    APIRET rc;
  170.  
  171.  
  172.  
  173.    rc=DosTmrQueryTime(&start_time);
  174.  
  175.    printf("DosTmrQueryTime rc=%u time=0x%08x%08x\n",rc,start_time.ulHi,start_time.ulLo);
  176.  
  177.                  
  178.  
  179.    DosSleep(100);
  180.  
  181.    printf("Sleeping 100ms\n");
  182.  
  183.    
  184.  
  185.    rc=DosTmrQueryTime(&end_time);
  186.  
  187.    printf("DosTmrQueryTime rc=%u time=0x%08x%08x\n",rc,end_time.ulHi,end_time.ulLo);
  188.  
  189.    
  190.  
  191.    rc=DosTmrQueryFreq(&freq);
  192.  
  193.    printf("DosTmrQueryFreq rc=%u freq=%uHz\n",rc,freq);
  194.  
  195.  
  196.  
  197.    interval.ulLo = end_time.ulLo - start_time.ulLo;
  198.  
  199.    interval.ulHi = (end_time.ulLo >= start_time.ulLo) ? end_time.ulHi - start_time.ulHi: end_time.ulHi - start_time.ulHi - 1;
  200.  
  201.    printf("Time interval=0x%08x%08x units=%uns\n",interval.ulHi,interval.ulLo,1000000000/freq);
  202.  
  203.    if (interval.ulHi == 0) printf("Appox. %uns\n",interval.ulLo*(1000000000/freq));
  204.  
  205.    
  206.  
  207.    return 0;
  208.  
  209. }
  210.  
  211.  
  212.  
  213.