00001 // This file is part of ff3d - http://www.freefem.org/ff3d 00002 // Copyright (C) 2001, 2002, 2003 Stéphane Del Pino 00003 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2, or (at your option) 00007 // any later version. 00008 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software Foundation, 00016 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 // $Id: Timer.hpp,v 1.8 2007/06/09 10:37:06 delpinux Exp $ 00019 00020 00021 #ifndef TIMER_HPP 00022 #define TIMER_HPP 00023 00024 #ifndef __MINGW32__ 00025 00026 #include <ctime> 00027 #include <unistd.h> // std::sysconf() 00028 #include <sys/times.h> // std::times() 00029 #endif // __MINGW32__ 00030 00031 #include <iostream> 00032 00033 #include <Types.hpp> 00034 00035 00048 class Timer 00049 { 00050 private: 00051 #ifndef __MINGW32__ 00052 real_t __startTimeCPU; 00053 real_t __stopTimeCPU; 00054 00055 time_t __startTime; 00056 time_t __stopTime; 00057 00058 bool __started; 00059 bool __stopped; 00060 00061 struct tms tp; 00062 00063 real_t __cpuTime() 00064 { 00065 static const long _CLK_TCK = sysconf(_SC_CLK_TCK); 00066 times(&tp); 00067 return static_cast<real_t>(tp.tms_utime)/_CLK_TCK; 00068 } 00069 #endif // __MINGW32__ 00070 00071 public: 00072 void start() 00073 { 00074 #ifndef __MINGW32__ 00075 ASSERT(!__started); 00076 __started=true; 00077 __stopped=false; 00078 __startTime = std::time(NULL); 00079 __startTimeCPU = __cpuTime(); 00080 #endif // __MINGW32__ 00081 } 00082 00083 void stop() 00084 { 00085 #ifndef __MINGW32__ 00086 ASSERT(__started); 00087 __started=false; 00088 __stopped=true; 00089 00090 __stopTime = std::time(NULL); 00091 __stopTimeCPU = __cpuTime(); 00092 #endif // __MINGW32__ 00093 } 00094 00095 friend std::ostream& operator << (std::ostream& os, const Timer& T) 00096 { 00097 #ifndef __MINGW32__ 00098 if (!T.__stopped) { 00099 os << "Timer not stopped\n"; 00100 return os; 00101 } 00102 os << "time: " << std::difftime(T.__stopTime, T.__startTime) << 's' 00103 << " - CPU time: " << T.__stopTimeCPU - T.__startTimeCPU << 's'; 00104 #else // __MINGW32__ 00105 os << "time: not implemented under windows"; 00106 #endif // __MINGW32__ 00107 return os; 00108 } 00109 00110 Timer() 00111 #ifndef __MINGW32__ 00112 : __startTimeCPU(0), 00113 __stopTimeCPU(0), 00114 __startTime(0), 00115 __stopTime(0), 00116 __started(false), 00117 __stopped(false) 00118 #endif // __MINGW32__ 00119 { 00120 ; 00121 } 00122 }; 00123 00124 #endif // TIMER_HPP
1.5.6