Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <clasp/util/timer.h>
00021
00022 #ifdef _WIN32
00023 #define WIN32_LEAN_AND_MEAN // exclude APIs such as Cryptography, DDE, RPC, Shell, and Windows Sockets.
00024 #define NOMINMAX // do not let windows.h define macros min and max
00025 #include <windows.h>
00026 #define TICKS_PER_SEC 10000000
00027
00028 namespace Clasp {
00029
00030 double RealTime::getTime() {
00031 union Convert {
00032 FILETIME time;
00033 __int64 asUint;
00034 } now;
00035 GetSystemTimeAsFileTime(&now.time);
00036 return static_cast<double>(now.asUint/static_cast<double>(TICKS_PER_SEC));
00037 }
00038
00039 double ProcessTime::getTime() {
00040 FILETIME ignoreStart, ignoreExit;
00041 union Convert {
00042 FILETIME time;
00043 __int64 asUint;
00044 } user, system;
00045 GetProcessTimes(GetCurrentProcess(), &ignoreStart, &ignoreExit, &user.time, &system.time);
00046 return (user.asUint + system.asUint) / double(TICKS_PER_SEC);
00047 }
00048
00049 double ThreadTime::getTime() {
00050 FILETIME ignoreStart, ignoreExit;
00051 union Convert {
00052 FILETIME time;
00053 __int64 asUint;
00054 } user, system;
00055 GetThreadTimes(GetCurrentThread(), &ignoreStart, &ignoreExit, &user.time, &system.time);
00056 return (user.asUint + system.asUint) / double(TICKS_PER_SEC);
00057 }
00058
00059 }
00060 #else
00061 #include <sys/times.h>
00062 #include <sys/time.h>
00063 #include <unistd.h>
00064 #include <sys/resource.h>
00065 #include <limits>
00066 namespace Clasp {
00067
00068 double RealTime::getTime() {
00069 struct timeval now;
00070 gettimeofday(&now, NULL);
00071 return static_cast<double>(now.tv_sec) + static_cast<double>(now.tv_usec / 1000000.0);
00072 }
00073
00074 double ProcessTime::getTime() {
00075 struct tms nowTimes;
00076 times(&nowTimes);
00077 return (nowTimes.tms_utime + nowTimes.tms_stime) / double(sysconf(_SC_CLK_TCK));
00078 }
00079
00080 double ThreadTime::getTime() {
00081 #if defined(RUSAGE_THREAD)
00082 int who = RUSAGE_THREAD;
00083 struct rusage usage;
00084 getrusage(who, &usage);
00085 double user = static_cast<double>(usage.ru_utime.tv_sec) + static_cast<double>(usage.ru_utime.tv_usec / 1000000.0);
00086 double sys = static_cast<double>(usage.ru_stime.tv_sec) + static_cast<double>(usage.ru_stime.tv_usec / 1000000.0);
00087 return user + sys;
00088 #else
00089 return std::numeric_limits<double>::quiet_NaN();
00090 #endif
00091 }
00092
00093 }
00094 #endif