11 #include "utils/PVLog.hpp" 15 #define USE_MACH_TIMER 20 #include <mach/mach.h> 21 #include <mach/mach_time.h> 24 #endif // USE_MACH_TIMER 29 uint64_t get_cpu_time() {
31 return mach_absolute_time();
34 gettimeofday(&tim, NULL);
35 return ((uint64_t)tim.tv_sec) * 1000000 + (uint64_t)tim.tv_usec;
40 static double cpu_time_to_sec(uint64_t cpu_elapsed) {
43 static mach_timebase_info_data_t info;
44 mach_timebase_info(&info);
45 cpu_elapsed *= info.numer;
46 cpu_elapsed /= info.denom;
47 us = (double)(cpu_elapsed / 1000);
49 us = (double)cpu_elapsed;
56 Timer::Timer(
double init_time) {
62 Timer::Timer(
const char *timermessage,
double init_time) {
65 message = strdup(timermessage ? timermessage :
"");
68 Timer::Timer(
const char *objname,
const char *objtype,
const char *timertype,
double init_time) {
73 snprintf(&dummy, 1,
"%32s: total time in %6s %10s: ", objname, objtype, timertype);
74 message = (
char *)malloc(charsneeded + 1);
75 if (message == NULL) {
77 "Timer::setMessage unable to allocate memory for Timer message: called with name=%s, " 78 "objtype=%s, timertype=%s\n",
83 int chars_used = snprintf(
84 message, charsneeded + 1,
"%32s: total time in %6s %10s: ", objname, objtype, timertype);
85 assert(chars_used <= charsneeded);
88 Timer::~Timer() { free(message); }
90 void Timer::reset(
double init_time) {
91 time_start = get_cpu_time();
92 time_end = time_start;
93 time_elapsed = init_time;
96 double Timer::start() {
return (
double)(time_start = get_cpu_time()); }
98 double Timer::stop() {
99 time_end = get_cpu_time();
100 time_elapsed += time_end - time_start;
101 return (
double)time_end;
104 double Timer::elapsed_time()
const {
return (
double)time_elapsed; }
106 int Timer::fprint_time(PrintStream &stream)
const {
108 stream << message <<
"processor cycle time == " << (float)cpu_time_to_sec(elapsed_time())