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 1430 2009-04-20 20:58:25Z andreasw $ 00006 // 00007 // Authors: Andreas Waechter IBM 2005-09-19 00008 00009 #ifndef __IPTIMEDTASK_HPP__ 00010 #define __IPTIMEDTASK_HPP__ 00011 00012 #include "IpUtils.hpp" 00013 00014 namespace Ipopt 00015 { 00018 class TimedTask 00019 { 00020 public: 00024 TimedTask() 00025 : 00026 total_time_(0.), 00027 start_called_(false), 00028 end_called_(true) 00029 {} 00030 00032 ~TimedTask() 00033 {} 00035 00037 void Reset() 00038 { 00039 total_time_ = 0.; 00040 start_called_ = false; 00041 end_called_ = true; 00042 } 00043 00045 void Start() 00046 { 00047 DBG_ASSERT(end_called_); 00048 DBG_ASSERT(!start_called_); 00049 end_called_ = false; 00050 start_called_ = true; 00051 start_time_ = CpuTime(); 00052 } 00053 00055 void End() 00056 { 00057 DBG_ASSERT(!end_called_); 00058 DBG_ASSERT(start_called_); 00059 end_called_ = true; 00060 start_called_ = false; 00061 total_time_ += CpuTime() - start_time_; 00062 } 00063 00068 void EndIfStarted() 00069 { 00070 if (start_called_) { 00071 end_called_ = true; 00072 start_called_ = false; 00073 total_time_ += CpuTime() - start_time_; 00074 } 00075 DBG_ASSERT(end_called_); 00076 } 00077 00079 Number TotalTime() const 00080 { 00081 DBG_ASSERT(end_called_); 00082 return total_time_; 00083 } 00084 00085 private: 00094 TimedTask(const TimedTask&); 00095 00097 void operator=(const TimedTask&); 00099 00101 Number start_time_; 00103 Number total_time_; 00104 00107 bool start_called_; 00108 bool end_called_; 00110 00111 }; 00112 } // namespace Ipopt 00113 00114 #endif