last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrTimer.h 00044 * Contains the class cvr::timer to measure wall-clock times with 00045 * microsecond precision. 00046 * \author Pablo Alvarado 00047 * \date 12.06.2000 00048 * 00049 * $Id: cvrTimer.h,v 1.6 2005/04/20 15:05:24 libuda Exp $ 00050 */ 00051 00052 #ifndef _CVR_TIMER_H 00053 #define _CVR_TIMER_H 00054 00055 #include "cvrObject.h" 00056 #include "cvrMacroSymbols.h" 00057 #include <string> 00058 00059 namespace cvr { 00060 00061 /** 00062 * The timer allows to measure time with a precision of about 30us 00063 * on Linux systems and a value dependent on the performance counter 00064 * on Windows systems. It seems like the precision for CPU time is 00065 * 1ms on Windows systems. The elapsed time is returned in 00066 * microseconds. 00067 * 00068 * Depending on the eTimeType (setTimeType()) The CPU time (Cpu) or 00069 * wall-clock time (Wall) are measured. The default is Cpu. 00070 * 00071 * The maximum time that can be measured with this function in Wall 00072 * mode is 1 day (86.4E+09 microseconds). To measure longer time 00073 * intervalls use the standard time() function. 00074 * 00075 * Example: 00076 * 00077 * \code 00078 * 00079 * cvr::timer chron; 00080 * 00081 * chron.start(); 00082 * 00083 * // do something 00084 * 00085 * chron.stop(); 00086 * 00087 * std::cout << "something takes " << chron.getTime() << " microseconds\n"; 00088 * 00089 * \endcode 00090 */ 00091 class timer : public object { 00092 public: 00093 00094 /** 00095 * Determines which time is measured. 00096 */ 00097 enum eTimeType { 00098 Wall, /**< Use Wall-time */ 00099 Cpu /**< Use CPU time */ 00100 }; 00101 00102 /** 00103 * Default constructor 00104 */ 00105 timer(); 00106 00107 /** 00108 * Constructor to set the time type. 00109 */ 00110 timer(const eTimeType& timeType); 00111 00112 /** 00113 * Copy constructor 00114 */ 00115 timer(const timer& other); 00116 00117 /** 00118 * Destructor 00119 */ 00120 virtual ~timer(); 00121 00122 /** 00123 * Start the timer 00124 */ 00125 void start(); 00126 00127 /** 00128 * Stop the timer 00129 */ 00130 void stop(); 00131 00132 /** 00133 * Get the elapsed time (in microsecond) between start() and stop() or the 00134 * actual time (if stop() is not been called yet!) 00135 * 00136 * @return Elapsed time 00137 */ 00138 double getTime() const; 00139 00140 /** 00141 * Sets the time type. 00142 * 00143 * @param timeType the new eTimeType 00144 */ 00145 void setTimeType(const eTimeType& timeType); 00146 00147 /** 00148 * Returns the current time type. 00149 * 00150 * @return the current eTimeType. 00151 */ 00152 const eTimeType& getTimeType(); 00153 00154 /** 00155 * Return a string containing the current time. Obviously this 00156 * always uses the Wall time. 00157 * 00158 * The format is the one return by the libc function \c ctime, for example 00159 * \code 00160 * Sat Jul 24 22:46:18 CEST 2004 00161 * \endcode 00162 * 00163 */ 00164 static std::string getDateAndTime(); 00165 00166 /** 00167 * Returns the current CPU time in usec. Don't use this function 00168 * for time measurement. Use start(), stop(), getTime() instead. 00169 */ 00170 static double getCpuTime(); 00171 00172 /** 00173 * Class name 00174 */ 00175 virtual const std::string& name() const; 00176 00177 /** 00178 * Clone method. 00179 */ 00180 virtual timer* clone() const; 00181 00182 /** 00183 * New instance method. 00184 */ 00185 virtual timer* newInstance() const; 00186 00187 protected: 00188 00189 /** 00190 * Time type used. 00191 */ 00192 eTimeType timeType_; 00193 00194 /** 00195 * Time at which start() was called. 00196 */ 00197 double startTime_; 00198 00199 /** 00200 * Time at which stop() was called. 00201 */ 00202 double endTime_; 00203 00204 /** 00205 * Flag to indicate if start() was called, but not stop(). 00206 */ 00207 bool started_; 00208 00209 /** 00210 * Get actual time. 00211 * 00212 * The implementation of this method depends on the OS. 00213 */ 00214 double getActualTime() const; 00215 00216 # ifdef _CVR_MSC_VER 00217 private: 00218 /** 00219 * \name MS VC++ version 00220 */ 00221 //@{ 00222 /** 00223 * Constant used to represent in a floating point representation the 00224 * value 2^32 00225 */ 00226 static const double max32bit_; 00227 00228 /** 00229 * Frequency (Hz) of the performance counter 00230 */ 00231 double freq_; 00232 //@} 00233 # endif 00234 00235 }; 00236 } 00237 00238 #endif 00239