G4Timer.cc

Go to the documentation of this file.
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 // Implementation
00034 // 29.04.97 G.Cosmo Added timings for Windows systems
00035 
00036 #include "G4Timer.hh"
00037 #include "G4ios.hh"
00038 
00039 #undef times
00040 
00041 // Global error function
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    // extract milliseconds time unit
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 ); // get real time in 10^-9 sec
00077            if( t != 0 ){
00078                    GetProcessTimes( GetCurrentProcess(), &ct, &et, &st, &ut);// get process time in 10^-9 sec
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 /* WIN32 */
00085 
00086 // Print timer status n std::ostream
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 

Generated on Mon May 27 17:50:00 2013 for Geant4 by  doxygen 1.4.7