00001 // Copyright (C) 2006 International Business Machines and others. 00002 // All Rights Reserved. 00003 // This code is published under the Common Public License. 00004 // 00005 // $Id: IpTimedTask.hpp 735 2006-06-04 06:10:05Z andreasw $ 00006 // 00007 // Authors: Andreas Waechter IBM 2005-09-19 00008 00009 #ifndef __IPTIMEDTASK_HPP__ 00010 #define __IPTIMEDTASK_HPP__ 00011 00012 #ifdef HAVE_CTIME 00013 # include <ctime> 00014 #else 00015 # ifdef HAVE_TIME_H 00016 # include <time.h> 00017 # else 00018 # error "don't have header file for time" 00019 # endif 00020 #endif 00021 00022 // The following lines are copied from CoinTime.hpp 00023 // We should probably make some more tests here 00024 #if defined(_MSC_VER) 00025 // Turn off compiler warning about long names 00026 # pragma warning(disable:4786) 00027 #else 00028 // MacOS-X and FreeBSD needs sys/time.h 00029 # if defined(__MACH__) || defined (__FreeBSD__) 00030 # include <sys/time.h> 00031 # endif 00032 # if !defined(__MSVCRT__) 00033 # include <sys/resource.h> 00034 # endif 00035 #endif 00036 00037 namespace Ipopt 00038 { 00041 class TimedTask 00042 { 00043 public: 00047 TimedTask() 00048 : 00049 total_time_(0.), 00050 start_called_(false), 00051 end_called_(true) 00052 {} 00053 00055 ~TimedTask() 00056 {} 00058 00060 void Reset() 00061 { 00062 total_time_ = 0.; 00063 start_called_ = false; 00064 end_called_ = true; 00065 } 00066 00068 void Start() 00069 { 00070 DBG_ASSERT(end_called_); 00071 DBG_ASSERT(!start_called_); 00072 end_called_ = false; 00073 start_called_ = true; 00074 start_time_ = CpuTime(); 00075 } 00076 00078 void End() 00079 { 00080 DBG_ASSERT(!end_called_); 00081 DBG_ASSERT(start_called_); 00082 end_called_ = true; 00083 start_called_ = false; 00084 total_time_ += CpuTime() - start_time_; 00085 } 00086 00091 void EndIfStarted() 00092 { 00093 if (start_called_) { 00094 end_called_ = true; 00095 start_called_ = false; 00096 total_time_ += CpuTime() - start_time_; 00097 } 00098 DBG_ASSERT(end_called_); 00099 } 00100 00102 Number TotalTime() const 00103 { 00104 DBG_ASSERT(end_called_); 00105 return total_time_; 00106 } 00107 00108 // The following lines were taken from CoinTime.hpp in COIN/Coin 00110 static inline Number CpuTime() 00111 { 00112 double cpu_temp; 00113 #if defined(_MSC_VER) || defined(__MSVCRT__) 00114 00115 unsigned int ticksnow; /* clock_t is same as int */ 00116 00117 ticksnow = (unsigned int)clock(); 00118 00119 cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC); 00120 #else 00121 00122 struct rusage usage; 00123 getrusage(RUSAGE_SELF,&usage); 00124 cpu_temp = usage.ru_utime.tv_sec; 00125 cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec); 00126 #endif 00127 00128 return cpu_temp; 00129 } 00130 00131 private: 00140 TimedTask(const TimedTask&); 00141 00143 void operator=(const TimedTask&); 00145 00147 Number start_time_; 00149 Number total_time_; 00150 00153 bool start_called_; 00154 bool end_called_; 00156 00157 }; 00158 } // namespace Ipopt 00159 00160 #endif