00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "G4Timer.hh"
00037 #include "G4ios.hh"
00038
00039 #undef times
00040
00041
00042 #include "G4ExceptionSeverity.hh"
00043 void G4Exception(const char* originOfException,
00044 const char* exceptionCode,
00045 G4ExceptionSeverity severity,
00046 const char* comments);
00047
00048 #if defined(IRIX6_2)
00049 # if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE_EXTENDED==1)
00050 # define __vfork vfork
00051 # endif
00052 #endif
00053
00054 #ifdef WIN32
00055 # include <sys/types.h>
00056 # include <windows.h>
00057
00058
00059 int sysconf(int a){
00060 if( a == _SC_CLK_TCK ) return 1000;
00061 else return 0;
00062 }
00063
00064 static clock_t filetime2msec( FILETIME* t ){
00065
00066 return (clock_t)((((float)t->dwHighDateTime)*429496.7296)+
00067 (((float)t->dwLowDateTime)*.0001) );
00068 }
00069
00070
00071 clock_t times(struct tms * t){
00072 FILETIME ct = {0,0}, et = {0,0}, st = {0,0}, ut = {0,0}, rt = {0,0};
00073 SYSTEMTIME realtime;
00074
00075 GetSystemTime( &realtime );
00076 SystemTimeToFileTime( &realtime, &rt );
00077 if( t != 0 ){
00078 GetProcessTimes( GetCurrentProcess(), &ct, &et, &st, &ut);
00079 t->tms_utime = t->tms_cutime = filetime2msec(&ut);
00080 t->tms_stime = t->tms_cstime = filetime2msec(&st);
00081 }
00082 return filetime2msec(&rt);
00083 }
00084 #endif
00085
00086
00087 std::ostream& operator << (std::ostream& os, const G4Timer& t)
00088 {
00089 if (t.IsValid())
00090 {
00091 os << "User=" << t.GetUserElapsed()
00092 << "s Real=" << t.GetRealElapsed()
00093 << "s Sys=" << t.GetSystemElapsed() << "s";
00094 }
00095 else
00096 {
00097 os << "User=****s Real=****s Sys=****s";
00098 }
00099 return os;
00100 }
00101
00102 G4Timer::G4Timer()
00103 : fValidTimes(false)
00104 {
00105 }
00106
00107 G4double G4Timer::GetRealElapsed() const
00108 {
00109 if (!fValidTimes)
00110 {
00111 G4Exception("G4Timer::GetRealElapsed()", "InvalidCondition",
00112 FatalException, "Timer not stopped or times not recorded!");
00113 }
00114 G4double diff=fEndRealTime-fStartRealTime;
00115 return diff/sysconf(_SC_CLK_TCK);
00116 }
00117
00118
00119 G4double G4Timer::GetSystemElapsed() const
00120 {
00121 if (!fValidTimes)
00122 {
00123 G4Exception("G4Timer::GetSystemElapsed()", "InvalidCondition",
00124 FatalException, "Timer not stopped or times not recorded!");
00125 }
00126 G4double diff=fEndTimes.tms_stime-fStartTimes.tms_stime;
00127 return diff/sysconf(_SC_CLK_TCK);
00128 }
00129
00130 G4double G4Timer::GetUserElapsed() const
00131 {
00132 if (!fValidTimes)
00133 {
00134 G4Exception("G4Timer::GetUserElapsed()", "InvalidCondition",
00135 FatalException, "Timer not stopped or times not recorded");
00136 }
00137 G4double diff=fEndTimes.tms_utime-fStartTimes.tms_utime;
00138 return diff/sysconf(_SC_CLK_TCK);
00139 }
00140