home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libiberty / getruntime.c < prev    next >
C/C++ Source or Header  |  1994-02-10  |  2KB  |  81 lines

  1. /* Return time used so far, in microseconds.
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with libiberty; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include "ansidecl.h"
  21. #include "libiberty.h"
  22.  
  23. /* There are several ways to get elapsed execution time; unfortunately no
  24.    single way is available for all host systems, nor are there reliable
  25.    ways to find out which way is correct for a given host. */
  26.  
  27. #include <time.h>
  28.  
  29. /* These should go away when libiberty uses autoconf. */
  30.  
  31. #if defined(__sun__) && !defined(__svr4__)
  32. #define HAVE_GETRUSAGE
  33. #endif
  34.  
  35. #ifdef HAVE_SYSCONF
  36. #define HAVE_TIMES
  37. #endif
  38.  
  39. #ifdef HAVE_GETRUSAGE
  40. #include <sys/time.h>
  41. #include <sys/resource.h>
  42. #endif
  43.  
  44. #ifdef HAVE_TIMES
  45. #include <sys/param.h>
  46. #include <sys/times.h>
  47. #endif
  48.  
  49. /* This is a fallback; if wrong, it will likely make obviously wrong
  50.    results. */
  51.  
  52. #ifndef CLOCKS_PER_SEC
  53. #define CLOCKS_PER_SEC 1
  54. #endif
  55.  
  56. long
  57. get_run_time ()
  58. {
  59. #ifdef HAVE_GETRUSAGE
  60.   struct rusage rusage;
  61.  
  62.   getrusage (0, &rusage);
  63.   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  64.       + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  65. #else /* ! HAVE_GETRUSAGE */
  66. #ifdef HAVE_TIMES
  67.   struct tms tms;
  68.  
  69.   times (&tms);
  70.   return (tms.tms_utime + tms.tms_stime) * (1000000 / HZ);
  71. #else /* ! HAVE_TIMES */
  72.   /* Fall back on clock and hope it's correctly implemented. */
  73. #if CLOCKS_PER_SEC <= 1000000
  74.   return clock () * (1000000 / CLOCKS_PER_SEC);
  75. #else
  76.   return clock () / CLOCKS_PER_SEC;
  77. #endif
  78. #endif  /* HAVE_TIMES */
  79. #endif  /* HAVE_GETRUSAGE */
  80. }
  81.