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

  1. /********************************************************************/
  2. /*                                                                  */
  3. /* (C) Copyright IBM UK Ltd. 1996                                   */
  4. /*                                                                  */
  5. /* TMR   - Example of DosTmrQueryTime and DosTmrQueryFreq usage.    */
  6. /*                                                                  */
  7. /* Query the high resolution timer.                                 */
  8. /* Richard Moore 16th November 98                                   */
  9. /* Version 1.0                                                      */
  10. /*                                                                  */
  11. /* Syntax TMR <none>                                                */
  12. /*                                                                  */
  13. /*                                                                  */
  14. /********************************************************************/
  15.  
  16.  
  17.  
  18. #define INCL_DOSPROFILE
  19.  
  20. #include <os2.h>
  21. #include <conio.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25.  
  26. /*************************************************************************************/
  27. /*                                                                                   */
  28. /* Purpose:                                                                          */
  29. /*                                                                                   */
  30. /* DosTmrQueryTime queries the 64-bit high resolution timer.                         */
  31. /*                                                                                   */
  32. /* DosTmrQueryFreq queries the frequency of the high resolution timer.               */
  33. /* Subtracting two 64-bit times and dividing by the frequency will give the          */
  34. /* high resolution time interval in seconds.                                         */
  35. /*                                                                                   */
  36. /*************************************************************************************/
  37.  
  38. /*************************************************************************************/
  39. /*                                                                                   */
  40. /*      Prototype definitions for DosTmrQueryTime and DosTmrQueryFreq                */
  41. /*                                                                                   */
  42. /*************************************************************************************/
  43. /*                                                                                   */
  44. /*   APIRET APIENTRY DosTmrQueryTime(PQWORD time);                                   */
  45. /*   APIRET APIENTRY DosTmrQueryFreq(PULONG freq);                                   */
  46. /*                                                                                   */
  47. /*************************************************************************************/
  48.  
  49. /*************************************************************************************/
  50. /*                                                                                   */
  51. /* DosTmrQueryTime returns the following return codes:                               */
  52. /*                                                                                   */
  53. /*************************************************************************************/
  54. /*                                                                                   */
  55. /* 0       NO_ERROR                                                                  */
  56. /* 87      ERROR_INVALID_PARAMETER                                                   */
  57. /* 99      ERROR_DEVICE_IN_USE                                                       */
  58. /* 535     ERROR_TMR_NO_DEVICE                                                       */
  59. /* 536     ERROR_TMR_INVALID_TIME                                                    */
  60. /*                                                                                   */
  61. /*************************************************************************************/
  62.  
  63. /*************************************************************************************/
  64. /*                                                                                   */
  65. /* DosTmrQueryFreq returns the following return codes:                               */
  66. /*                                                                                   */
  67. /*************************************************************************************/
  68. /*                                                                                   */
  69. /* 0       NO_ERROR                                                                  */
  70. /* 87      ERROR_INVALID_PARAMETER                                                   */
  71. /* 99      ERROR_DEVICE_IN_USE                                                       */
  72. /* 535     ERROR_TMR_NO_DEVICE                                                       */
  73. /*                                                                                   */
  74. /*************************************************************************************/
  75.  
  76. void int3(void);
  77.  
  78. int main(int argc, char *argv[], char *envp[])
  79. {
  80.    QWORD start_time;  
  81.    QWORD end_time;  
  82.    QWORD interval;
  83.  
  84.    ULONG freq;
  85.    APIRET rc;
  86.  
  87.    rc=DosTmrQueryTime(&start_time);
  88.    printf("DosTmrQueryTime rc=%u time=0x%08x%08x\n",rc,start_time.ulHi,start_time.ulLo);
  89.                  
  90.    DosSleep(100);
  91.    printf("Sleeping 100ms\n");
  92.    
  93.    rc=DosTmrQueryTime(&end_time);
  94.    printf("DosTmrQueryTime rc=%u time=0x%08x%08x\n",rc,end_time.ulHi,end_time.ulLo);
  95.    
  96.    rc=DosTmrQueryFreq(&freq);
  97.    printf("DosTmrQueryFreq rc=%u freq=%uHz\n",rc,freq);
  98.  
  99.    interval.ulLo = end_time.ulLo - start_time.ulLo;
  100.    interval.ulHi = (end_time.ulLo >= start_time.ulLo) ? end_time.ulHi - start_time.ulHi: end_time.ulHi - start_time.ulHi - 1;
  101.    printf("Time interval=0x%08x%08x units=%uns\n",interval.ulHi,interval.ulLo,1000000000/freq);
  102.    if (interval.ulHi == 0) printf("Appox. %uns\n",interval.ulLo*(1000000000/freq));
  103.    
  104.    return 0;
  105. }
  106.  
  107.