00001 /* $Id$ */ 00002 00003 /* GenLib2 - General Library 2 00004 * Copyright (C) 2005 Dirk Stallmann 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #ifndef GENLIB2_TIMER_INCLUDED 00022 #define GENLIB2_TIMER_INCLUDED 00023 00024 #include <climits> 00025 #include <ctime> 00026 00027 00028 namespace GenLib2 00029 { 00030 00036 class Timer 00037 { 00038 public: 00040 Timer() : _running(false), _accu(0), _last_time(0) {} 00041 00043 void reset() { _running = false; _last_time = _accu = 0; } 00044 00046 void start() 00047 { 00048 if (!_running) 00049 { 00050 _last_time = clock(); 00051 _running = true; 00052 } 00053 } 00054 00056 double stop() 00057 { 00058 if (_running) 00059 { 00060 _accu += clock() - _last_time; 00061 _running = false; 00062 } 00063 return double(_accu) / double(CLOCKS_PER_SEC); 00064 } 00065 00067 double time() 00068 { 00069 if (_running) 00070 { 00071 _accu += clock() - _last_time; 00072 _last_time = clock(); 00073 } 00074 return double(_accu) / double(CLOCKS_PER_SEC); 00075 } 00076 00077 private: 00079 bool _running; 00080 00082 clock_t _accu; 00083 00085 clock_t _last_time; 00086 }; 00087 00088 00089 // Implementations 00090 00091 00092 } // namespace GenLib2 00093 00094 #endif /* GENLIB2_TIMER_INCLUDED */