| 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 |
|
| 8 |
#if defined(_WIN32) |
| 9 |
#include <Windows.h> |
| 10 |
|
| 11 |
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) |
| 12 |
#include <unistd.h> |
| 13 |
#include <sys/resource.h> |
| 14 |
#include <sys/times.h> |
| 15 |
#include <time.h> |
| 16 |
|
| 17 |
#else |
| 18 |
#error "Unable to define get_cpu_time() for an unknown OS." |
| 19 |
#endif |
| 20 |
|
| 21 |
/** |
| 22 |
* Returns the amount of CPU time used by the current process, |
| 23 |
* in seconds, or -1.0 if an error occurred. |
| 24 |
*/ |
| 25 |
double get_cpu_time() |
| 26 |
{ |
| 27 |
#if defined(_WIN32) |
| 28 |
/* Windows -------------------------------------------------- */ |
| 29 |
FILETIME createTime; |
| 30 |
FILETIME exitTime; |
| 31 |
FILETIME kernelTime; |
| 32 |
FILETIME userTime; |
| 33 |
if ( GetProcessTimes( GetCurrentProcess( ), |
| 34 |
&createTime, &exitTime, &kernelTime, &userTime ) != -1 ) |
| 35 |
{ |
| 36 |
SYSTEMTIME userSystemTime; |
| 37 |
if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 ) |
| 38 |
return (double)userSystemTime.wHour * 3600.0 + |
| 39 |
(double)userSystemTime.wMinute * 60.0 + |
| 40 |
(double)userSystemTime.wSecond + |
| 41 |
(double)userSystemTime.wMilliseconds / 1000.0; |
| 42 |
} |
| 43 |
|
| 44 |
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) |
| 45 |
/* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */ |
| 46 |
|
| 47 |
#if _POSIX_TIMERS > 0 |
| 48 |
/* Prefer high-res POSIX timers, when available. */ |
| 49 |
{ |
| 50 |
clockid_t id; |
| 51 |
struct timespec ts; |
| 52 |
#if _POSIX_CPUTIME > 0 |
| 53 |
/* Clock ids vary by OS. Query the id, if possible. */ |
| 54 |
if ( clock_getcpuclockid( 0, &id ) == -1 ) |
| 55 |
#endif |
| 56 |
#if defined(CLOCK_PROCESS_CPUTIME_ID) |
| 57 |
/* Use known clock id for AIX, Linux, or Solaris. */ |
| 58 |
id = CLOCK_PROCESS_CPUTIME_ID; |
| 59 |
#elif defined(CLOCK_VIRTUAL) |
| 60 |
/* Use known clock id for BSD or HP-UX. */ |
| 61 |
id = CLOCK_VIRTUAL; |
| 62 |
#else |
| 63 |
id = (clockid_t)-1; |
| 64 |
#endif |
| 65 |
if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 ) |
| 66 |
return (double)ts.tv_sec + |
| 67 |
(double)ts.tv_nsec / 1000000000.0; |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
#if defined(RUSAGE_SELF) |
| 72 |
{ |
| 73 |
struct rusage rusage; |
| 74 |
if ( getrusage( RUSAGE_SELF, &rusage ) != -1 ) |
| 75 |
return (double)rusage.ru_utime.tv_sec + |
| 76 |
(double)rusage.ru_utime.tv_usec / 1000000.0; |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
#if defined(_SC_CLK_TCK) |
| 81 |
{ |
| 82 |
const double ticks = (double)sysconf( _SC_CLK_TCK ); |
| 83 |
struct tms tms; |
| 84 |
if ( times( &tms ) != (clock_t)-1 ) |
| 85 |
return (double)tms.tms_utime / ticks; |
| 86 |
} |
| 87 |
#endif |
| 88 |
|
| 89 |
#if defined(CLOCKS_PER_SEC) |
| 90 |
{ |
| 91 |
clock_t cl = clock( ); |
| 92 |
if ( cl != (clock_t)-1 ) |
| 93 |
return (double)cl / (double)CLOCKS_PER_SEC; |
| 94 |
} |
| 95 |
#endif |
| 96 |
|
| 97 |
#endif |
| 98 |
|
| 99 |
return -1.0; /* Failed. */ |
| 100 |
} |