Back to index

salome-kernel  6.5.0
Utils_Timer.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library 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 GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Utils : general SALOME's definitions and tools
00024 //  File   : Utils_Timer.cxx
00025 //  Module : SALOME
00026 //
00027 # include "Utils_Timer.hxx"
00028 
00029 # include <iostream>
00030 
00031 #include "utilities.h"
00032 
00033 #ifndef WIN32
00034 static struct timezone *tz=(struct timezone*) malloc(sizeof(struct timezone));
00035 #else
00036 //timezone *tz=_timezone;
00037 #endif
00038 
00039 #ifndef CLK_TCK
00040 # define CLK_TCK      CLOCKS_PER_SEC
00041 #endif
00042 
00043 Utils_Timer::Utils_Timer() {
00044 #ifndef WIN32
00045   RefToInitialTMS = new tms;
00046   RefToCurrentTMS = new tms;
00047 
00048   RefToInitialTimeB = new timeval;
00049   RefToCurrentTimeB = new timeval;
00050 #else
00051   RefToInitialTMS = new FILETIME;
00052   RefToCurrentTMS = new FILETIME;
00053 
00054   RefToInitialTimeB = new time_t;
00055   RefToCurrentTimeB = new time_t;
00056 #endif
00057 
00058   Cumul_user      = Cumul_sys = 0.;
00059   Stopped         = 1;
00060 }
00061 
00062 Utils_Timer::~Utils_Timer() {
00063   delete RefToInitialTMS ;
00064   delete RefToCurrentTMS ;
00065 
00066   delete RefToInitialTimeB ;
00067   delete RefToCurrentTimeB ;
00068 }
00069 
00070 void Utils_Timer::Start() {
00071   if (Stopped) {
00072     Stopped = 0;
00073 #ifndef WIN32
00074     times(RefToInitialTMS);
00075     gettimeofday(RefToInitialTimeB,tz);
00076 #else
00077     SYSTEMTIME st;
00078     GetSystemTime(&st);
00079     SystemTimeToFileTime(&st, RefToInitialTMS);
00080           time(RefToCurrentTimeB);
00081 #endif
00082   }
00083 }
00084 
00085 void Utils_Timer::Stop() {
00086   if (!Stopped) {
00087 #ifndef WIN32
00088     times(RefToCurrentTMS);
00089     int diffr_user = RefToCurrentTMS->tms_utime - RefToInitialTMS->tms_utime;
00090     int diffr_sys  = RefToCurrentTMS->tms_stime - RefToInitialTMS->tms_stime;
00091     gettimeofday(RefToCurrentTimeB,tz);
00092 
00093     Cumul_user += (double) diffr_user / CLK_TCK ;
00094     Cumul_sys  += (double) diffr_sys  / CLK_TCK ;
00095 #else
00096     SYSTEMTIME st;
00097     GetSystemTime(&st);
00098     SystemTimeToFileTime(&st, RefToCurrentTMS);
00099     Cumul_user += (int)(((ULARGE_INTEGER*)(RefToCurrentTMS))->QuadPart - ((ULARGE_INTEGER*)(RefToInitialTMS))->QuadPart) / 10000000;
00100           Cumul_sys = Cumul_user;
00101           time(RefToCurrentTimeB);
00102 #endif
00103    Stopped = 1;
00104   }
00105 }
00106 
00107 void Utils_Timer::Show() {
00108   bool StopSav = Stopped;
00109   if (!StopSav) Stop();
00110   MESSAGE("CPU user time: "   << Cumul_user  << " seconds ");
00111   MESSAGE("CPU system time: " << Cumul_sys   << " seconds ");
00112   if (!StopSav) Start();
00113 }
00114 
00115 void Utils_Timer::Reset() {
00116   Stopped     = 1;
00117   Cumul_user  = Cumul_sys = 0. ;
00118 }
00119 
00120 void Utils_Timer::ShowAbsolute(){
00121 #if defined(_DEBUG_) || defined(_DEBUG)
00122 #ifndef WIN32
00123     unsigned long Absolute_user = (unsigned long) ((timeval*)RefToCurrentTimeB)->tv_sec ;
00124 #else
00125     unsigned long Absolute_user = (unsigned long) *RefToCurrentTimeB;
00126 #endif
00127     MESSAGE("Absolute time: "   << Absolute_user  << " seconds ");
00128 #endif
00129 }