00001 /* 00002 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc. 00003 * All rights reserved. This program is made available under the terms of the 00004 * Eclipse Public License v1.0 which accompanies this distribution, and is 00005 * available at http://www.eclipse.org/legal/epl-v10.html 00006 * Contributors: 00007 * National Institute of Advanced Industrial Science and Technology (AIST) 00008 */ 00009 00010 #ifndef OPENHRP_UTIL_TIME_MEASURE_H_INCLUDED 00011 #define OPENHRP_UTIL_TIME_MEASURE_H_INCLUDED 00012 00013 00014 #ifndef __WIN32__ 00015 00016 #include <sys/time.h> 00017 00018 class TimeMeasure 00019 { 00020 struct timeval tv; 00021 double time_; 00022 double maxTime_; 00023 double totalTime_; 00024 int numCalls_; 00025 00026 public: 00027 TimeMeasure() { 00028 totalTime_ = 0.0; 00029 maxTime_ = 0.0; 00030 numCalls_ = 0; 00031 } 00032 00033 void begin() { 00034 gettimeofday(&tv, 0); 00035 } 00036 00037 void end(){ 00038 double beginTime = tv.tv_sec + (double)tv.tv_usec * 1.0e-6; 00039 gettimeofday(&tv, 0); 00040 double endTime = tv.tv_sec + (double)tv.tv_usec * 1.0e-6; 00041 time_ = endTime - beginTime; 00042 if (time_ > maxTime_) maxTime_ = time_; 00043 totalTime_ += time_; 00044 numCalls_++; 00045 } 00046 00047 double time() const { return time_; } 00048 double totalTime() const { return totalTime_; } 00049 double maxTime() const { return maxTime_; } 00050 double averageTime() const { return totalTime_ / numCalls_; } 00051 int numCalls() const { return numCalls_; } 00052 }; 00053 00054 00055 #else 00056 #include <windows.h> 00057 typedef unsigned __int64 ulonglong; 00058 00059 class TimeMeasure 00060 { 00061 ulonglong iTimerScale; 00062 ulonglong beginTime; 00063 ulonglong endTime; 00064 double time_; 00065 double maxTime_; 00066 double totalTime_; 00067 int numCalls_; 00068 00069 public: 00070 TimeMeasure() { 00071 totalTime_ = 0.0; 00072 maxTime_ = 0.0; 00073 numCalls_ = 0; 00074 BOOL iDummyBool = QueryPerformanceFrequency ((LARGE_INTEGER *) &iTimerScale); 00075 if(!iDummyBool) 00076 iTimerScale=1; 00077 } 00078 00079 void begin() { 00080 BOOL iDummyBool = QueryPerformanceCounter ((LARGE_INTEGER *) &beginTime); 00081 if(!iDummyBool) 00082 beginTime=1; 00083 } 00084 00085 void end(){ 00086 BOOL iDummyBool = QueryPerformanceCounter ((LARGE_INTEGER *) &endTime); 00087 if(!iDummyBool) 00088 endTime=0; 00089 time_ = (double)(endTime - beginTime) / iTimerScale; 00090 if (time_ > maxTime_) maxTime_ = time_; 00091 totalTime_ += time_; 00092 numCalls_++; 00093 } 00094 double time() const { return time_; } 00095 double totalTime() const { return totalTime_; } 00096 double maxTime() const { return maxTime_; } 00097 double averageTime() const { return totalTime_ / numCalls_; } 00098 int numCalls() const { return numCalls_; } 00099 }; 00100 00101 #endif 00102 00103 00104 #endif