Advertisement
HellFinger

Untitled

Feb 23rd, 2022
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /*
  2. * Author: David Robert Nadeau
  3. * Site: http://NadeauSoftware.com/
  4. * License: Creative Commons Attribution 3.0 Unported License
  5. * http://creativecommons.org/licenses/by/3.0/deed.en_US
  6. */
  7. #if defined(_WIN32)
  8. #include <Windows.h>
  9.  
  10. #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
  11. #include <unistd.h>
  12. #include <sys/resource.h>
  13. #include <sys/times.h>
  14. #include <time.h>
  15.  
  16. #else
  17. #error "Unable to define getCPUTime( ) for an unknown OS."
  18. #endif
  19.  
  20. /**
  21. * Returns the amount of CPU time used by the current process,
  22. * in seconds, or -1.0 if an error occurred.
  23. */
  24. double getCPUTime( )
  25. {
  26. #if defined(_WIN32)
  27. /* Windows -------------------------------------------------- */
  28. FILETIME createTime;
  29. FILETIME exitTime;
  30. FILETIME kernelTime;
  31. FILETIME userTime;
  32. if ( GetProcessTimes( GetCurrentProcess( ),
  33. &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
  34. {
  35. SYSTEMTIME userSystemTime;
  36. if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
  37. return (double)userSystemTime.wHour * 3600.0 +
  38. (double)userSystemTime.wMinute * 60.0 +
  39. (double)userSystemTime.wSecond +
  40. (double)userSystemTime.wMilliseconds / 1000.0;
  41. }
  42.  
  43. #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
  44. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */
  45.  
  46. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  47. /* Prefer high-res POSIX timers, when available. */
  48. {
  49. clockid_t id;
  50. struct timespec ts;
  51. #if _POSIX_CPUTIME > 0
  52. /* Clock ids vary by OS. Query the id, if possible. */
  53. if ( clock_getcpuclockid( 0, &id ) == -1 )
  54. #endif
  55. #if defined(CLOCK_PROCESS_CPUTIME_ID)
  56. /* Use known clock id for AIX, Linux, or Solaris. */
  57. id = CLOCK_PROCESS_CPUTIME_ID;
  58. #elif defined(CLOCK_VIRTUAL)
  59. /* Use known clock id for BSD or HP-UX. */
  60. id = CLOCK_VIRTUAL;
  61. #else
  62. id = (clockid_t)-1;
  63. #endif
  64. if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 )
  65. return (double)ts.tv_sec +
  66. (double)ts.tv_nsec / 1000000000.0;
  67. }
  68. #endif
  69.  
  70. #if defined(RUSAGE_SELF)
  71. {
  72. struct rusage rusage;
  73. if ( getrusage( RUSAGE_SELF, &rusage ) != -1 )
  74. return (double)rusage.ru_utime.tv_sec +
  75. (double)rusage.ru_utime.tv_usec / 1000000.0;
  76. }
  77. #endif
  78.  
  79. #if defined(_SC_CLK_TCK)
  80. {
  81. const double ticks = (double)sysconf( _SC_CLK_TCK );
  82. struct tms tms;
  83. if ( times( &tms ) != (clock_t)-1 )
  84. return (double)tms.tms_utime / ticks;
  85. }
  86. #endif
  87.  
  88. #if defined(CLOCKS_PER_SEC)
  89. {
  90. clock_t cl = clock( );
  91. if ( cl != (clock_t)-1 )
  92. return (double)cl / (double)CLOCKS_PER_SEC;
  93. }
  94. #endif
  95.  
  96. #endif
  97.  
  98. return -1; /* Failed. */
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement