00001 // 00002 // ******************************************************************** 00003 // * License and Disclaimer * 00004 // * * 00005 // * The Geant4 software is copyright of the Copyright Holders of * 00006 // * the Geant4 Collaboration. It is provided under the terms and * 00007 // * conditions of the Geant4 Software License, included in the file * 00008 // * LICENSE and available at http://cern.ch/geant4/license . These * 00009 // * include a list of copyright holders. * 00010 // * * 00011 // * Neither the authors of this software system, nor their employing * 00012 // * institutes,nor the agencies providing financial support for this * 00013 // * work make any representation or warranty, express or implied, * 00014 // * regarding this software system or assume any liability for its * 00015 // * use. Please see the license in the file LICENSE and URL above * 00016 // * for the full disclaimer and the limitation of liability. * 00017 // * * 00018 // * This code implementation is the result of the scientific and * 00019 // * technical work of the GEANT4 collaboration. * 00020 // * By using, copying, modifying or distributing the software (or * 00021 // * any work based on the software) you agree to acknowledge its * 00022 // * use in resulting scientific publications, and indicate your * 00023 // * acceptance of all terms of the Geant4 Software license. * 00024 // ******************************************************************** 00025 // 00026 // 00027 // $Id$ 00028 // 00029 // 00030 // ---------------------------------------------------------------------- 00031 // Class G4Timer 00032 // 00033 // Class description: 00034 // 00035 // Class for timer objects, able to measure elasped user/system process time. 00036 // 00037 // Note: Uses <sys/times.h> & <unistd.h> - POSIX.1 defined 00038 // If used, this header must be included in the source (.cc) file and it 00039 // must be the first header file to be included! 00040 // 00041 // Member functions: 00042 // 00043 // G4Timer() 00044 // Construct a timer object 00045 // Start() 00046 // Start timing 00047 // Stop() 00048 // Stop timing 00049 // G4bool IsValid() 00050 // Return true if have a valid time (ie start() and stop() called) 00051 // G4double GetRealElapsed() 00052 // Return the elapsed real time between last calling start() and stop() 00053 // G4double GetSystemElapsed() 00054 // Return the elapsed system time between last calling start() and stop() 00055 // G4double GetUserElapsed() 00056 // Return the elapsed user time between last calling start() and stop() 00057 // 00058 // Operators: 00059 // 00060 // std::ostream& operator << (std::ostream& os, const G4Timer& t); 00061 // Print the elapsed real,system and usertimes on os. Prints **s for times 00062 // if !IsValid 00063 // 00064 // Member data: 00065 // 00066 // G4bool fValidTimes 00067 // True after start and stop have both been called more than once and 00068 // an equal number of times 00069 // clock_t fStartRealTime,fEndRealTime 00070 // Real times (arbitrary time 0) 00071 // tms fStartTimes,fEndTimes 00072 // Timing structures (see times(2)) for start and end times 00073 00074 // History: 00075 // 23.08.96 P.Kent Updated to also computed real elapsed time 00076 // 21.08.95 P.Kent 00077 // 29.04.97 G.Cosmo Added timings for Windows/NT 00078 00079 #ifndef G4TIMER_HH 00080 #define G4TIMER_HH 00081 00082 #ifndef WIN32 00083 # include <unistd.h> 00084 # include <sys/times.h> 00085 #else 00086 # include <time.h> 00087 # define _SC_CLK_TCK 1 00088 00089 extern "C" { 00090 int sysconf(int); 00091 }; 00092 00093 // Structure returned by times() 00094 00095 struct tms { 00096 clock_t tms_utime; /* user time */ 00097 clock_t tms_stime; /* system time */ 00098 clock_t tms_cutime; /* user time, children */ 00099 clock_t tms_cstime; /* system time, children */ 00100 }; 00101 00102 extern "C" { 00103 extern clock_t times(struct tms *); 00104 }; 00105 #endif /* WIN32 */ 00106 00107 #include "G4Types.hh" 00108 #include "G4ios.hh" 00109 00110 class G4Timer 00111 { 00112 public: 00113 00114 G4Timer(); 00115 00116 inline void Start(); 00117 inline void Stop(); 00118 inline G4bool IsValid() const; 00119 inline const char* GetClockTime() const; 00120 G4double GetRealElapsed() const; 00121 G4double GetSystemElapsed() const; 00122 G4double GetUserElapsed() const; 00123 00124 private: 00125 00126 G4bool fValidTimes; 00127 clock_t fStartRealTime,fEndRealTime; 00128 tms fStartTimes,fEndTimes; 00129 }; 00130 00131 std::ostream& operator << (std::ostream& os, const G4Timer& t); 00132 00133 #include "G4Timer.icc" 00134 00135 #define times ostimes 00136 00137 #endif