home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libg++ / src / timer.c < prev    next >
C/C++ Source or Header  |  1993-10-26  |  3KB  |  136 lines

  1. /* 
  2. Copyright (C) 1990, 1992 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21.  
  22. /* Timing functions from Doug Schmidt... */
  23.  
  24. /* no such thing as "negative time"! */
  25. #define  TIMER_ERROR_VALUE -1.0   
  26.  
  27. /* If this does not work on your system, change this to #if 0, and 
  28.    report the problem. */
  29.  
  30. #if 1
  31.  
  32. #include <_G_config.h>
  33. #if _G_HAVE_SYS_RESOURCE
  34. #include <sys/time.h>
  35. #include <sys/resource.h>
  36. #endif
  37. #if !_G_HAVE_SYS_RESOURCE || !defined(RUSAGE_SELF)
  38. #define USE_TIMES
  39. #include <sys/param.h>
  40. #include <sys/times.h>
  41. #if !defined (HZ) && defined(CLK_TCK)
  42. #define HZ CLK_TCK
  43. #endif
  44. static struct tms Old_Time;
  45. static struct tms New_Time;
  46. #else
  47. static struct rusage Old_Time;
  48. static struct rusage New_Time;
  49. #endif
  50. static int    Timer_Set = 0;
  51.  
  52. double
  53. start_timer()
  54. {
  55.    Timer_Set = 1;
  56. #ifdef USE_TIMES
  57.    times(&Old_Time);
  58.    return((double) Old_Time.tms_utime / HZ);
  59. #else
  60.    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
  61.    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
  62. #endif
  63. }
  64.  
  65. /* Returns process time since Last_Time.
  66.    If parameter is 0.0, returns time since the Old_Time was set.
  67.    Returns TIMER_ERROR_VALUE if `start_timer' is not called first.  */
  68.  
  69. double
  70. return_elapsed_time(Last_Time)
  71.      double Last_Time;
  72. {
  73.    if (!Timer_Set) {
  74.       return(TIMER_ERROR_VALUE);
  75.    }   
  76.    else {
  77.     /* get process time */
  78. #ifdef USE_TIMES
  79.       times(&New_Time);
  80. #else
  81.       getrusage(RUSAGE_SELF,&New_Time);
  82. #endif
  83.       if (Last_Time == 0.0) {
  84. #ifdef USE_TIMES
  85.      return((double) (New_Time.tms_utime - Old_Time.tms_utime) / HZ);
  86. #else
  87.          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) + 
  88.                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec) 
  89.                 / 1000000.0));
  90. #endif
  91.       }
  92.       else {
  93. #ifdef USE_TIMES
  94.      return((double) New_Time.tms_utime / HZ - Last_Time);
  95. #else
  96.          return((New_Time.ru_utime.tv_sec + 
  97.                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
  98. #endif
  99.       }
  100.    }
  101. }
  102.  
  103. #ifdef VMS
  104. void sys$gettim(unsigned int*) asm("sys$gettim");
  105.  
  106. getrusage(int dummy,struct rusage* time){
  107.     double rtime;
  108.     unsigned int systime[2];
  109.     int i;
  110.     sys$gettim(&systime[0]);
  111.     rtime=systime[1];
  112.     for(i=0;i<4;i++) rtime *= 256;
  113.     rtime+= systime[0];
  114. /* we subtract an offset to make sure that the number fits in a long int*/
  115.     rtime=rtime/1.0e+7-4.144e+9;
  116.     time->ru_utime.tv_sec= rtime;
  117.     rtime=(rtime-time->ru_utime.tv_sec)*1.0e6;    
  118.     time->ru_utime.tv_usec= rtime;
  119. }
  120. #endif
  121. #else /* dummy them out */
  122.  
  123. double start_timer()
  124. {
  125.   return TIMER_ERROR_VALUE;
  126. }
  127.  
  128. double return_elapsed_time(double)
  129. {
  130.   return TIMER_ERROR_VALUE;
  131. }
  132.  
  133. #endif /* timing stuff */
  134.  
  135.  
  136.