/src/play/unix/timeu.c

https://bitbucket.org/dpgrote/pygist · C · 71 lines · 49 code · 8 blank · 14 comment · 5 complexity · 30db68f09de38047c5fd7bce962152d4 MD5 · raw file

  1. /*
  2. * $Id: timeu.c,v 1.1 2005-09-18 22:05:39 dhmunro Exp $
  3. * p_cpu_secs for UNIX machines (ANSI C version not UNIX specific)
  4. */
  5. /* Copyright (c) 2005, The Regents of the University of California.
  6. * All rights reserved.
  7. * This file is part of yorick (http://yorick.sourceforge.net).
  8. * Read the accompanying LICENSE file for details.
  9. */
  10. #include "config.h"
  11. #include "play.h"
  12. /* if getrusage exists, it is likely best choice (eg- Digital UNIX) */
  13. #ifdef USE_GETRUSAGE
  14. /* this is BSD way to get user and system time */
  15. #undef _POSIX_SOURCE
  16. #include <sys/time.h>
  17. #include <sys/resource.h>
  18. double
  19. p_cpu_secs(double *sys)
  20. {
  21. struct rusage cpu;
  22. getrusage(RUSAGE_SELF, &cpu);
  23. if (sys) *sys = cpu.ru_stime.tv_sec + 1.0e-6*cpu.ru_stime.tv_usec;
  24. return cpu.ru_utime.tv_sec + 1.0e-6*cpu.ru_utime.tv_usec;
  25. }
  26. #else
  27. # ifdef USE_TIMES
  28. /* this is POSIX 1003.1-1990 standard timing interface */
  29. #ifndef _POSIX_SOURCE
  30. #define _POSIX_SOURCE 1
  31. #endif
  32. #include <time.h>
  33. #include <sys/times.h>
  34. /* try to handle modest deviations from POSIX standard (e.g.- Sun) */
  35. # ifndef CLK_TCK
  36. # include <unistd.h>
  37. # ifndef CLK_TCK
  38. # define CLK_TCK sysconf(_SC_CLK_TCK)
  39. # endif
  40. # endif
  41. static double secs_per_tick = 0.0;
  42. double
  43. p_cpu_secs(double *sys)
  44. {
  45. struct tms cpu;
  46. times(&cpu);
  47. if (secs_per_tick==0.0) secs_per_tick = 1./((double)CLK_TCK);
  48. if (sys) *sys = cpu.tms_stime*secs_per_tick;
  49. return cpu.tms_utime*secs_per_tick;
  50. }
  51. # else
  52. /* ANSI C standard should at least compile anywhere */
  53. #include "time.h"
  54. static double secs_per_tick = 0.0;
  55. double
  56. p_cpu_secs(double *sys)
  57. {
  58. if (secs_per_tick==0.0) secs_per_tick = 1./((double)CLOCKS_PER_SEC);
  59. if (sys) *sys = 0.0;
  60. return clock()*secs_per_tick;
  61. }
  62. # endif
  63. #endif