portable_timer.hh
Go to the documentation of this file.
00001 //=====================================================
00002 // File   :  portable_timer.hh
00003 // Author :  L. Plagne <laurent.plagne@edf.fr)> from boost lib
00004 // Copyright (C) EDF R&D,  lun sep 30 14:23:17 CEST 2002
00005 //=====================================================
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 //
00020 //  simple_time extracted from the boost library
00021 //
00022 #ifndef _PORTABLE_TIMER_HH
00023 #define _PORTABLE_TIMER_HH
00024 
00025 #include <ctime>
00026 #include <cstdlib>
00027 
00028 #include <time.h>
00029 
00030 
00031 #define USEC_IN_SEC 1000000
00032 
00033 
00034 //  timer  -------------------------------------------------------------------//
00035 
00036 //  A timer object measures CPU time.
00037 #ifdef _MSC_VER
00038 
00039 #define NOMINMAX
00040 #include <windows.h>
00041 
00042 /*#ifndef hr_timer
00043 #include "hr_time.h"
00044 #define hr_timer
00045 #endif*/
00046 
00047  class Portable_Timer
00048  {
00049   public:
00050 
00051    typedef struct {
00052     LARGE_INTEGER start;
00053     LARGE_INTEGER stop;
00054    } stopWatch;
00055 
00056 
00057    Portable_Timer()
00058    {
00059          startVal.QuadPart = 0;
00060          stopVal.QuadPart = 0;
00061          QueryPerformanceFrequency(&frequency);
00062    }
00063 
00064    void start() { QueryPerformanceCounter(&startVal); }
00065 
00066    void stop() { QueryPerformanceCounter(&stopVal); }
00067 
00068    double elapsed() {
00069          LARGE_INTEGER time;
00070      time.QuadPart = stopVal.QuadPart - startVal.QuadPart;
00071      return LIToSecs(time);
00072    }
00073 
00074    double user_time() { return elapsed(); }
00075 
00076 
00077  private:
00078 
00079    double LIToSecs(LARGE_INTEGER& L) {
00080      return ((double)L.QuadPart /(double)frequency.QuadPart) ;
00081    }
00082 
00083    LARGE_INTEGER startVal;
00084    LARGE_INTEGER stopVal;
00085    LARGE_INTEGER frequency;
00086 
00087 
00088  }; // Portable_Timer
00089 
00090 #else
00091 
00092 #include <sys/time.h>
00093 #include <sys/resource.h>
00094 #include <unistd.h>
00095 #include <sys/times.h>
00096 
00097 class Portable_Timer
00098 {
00099  public:
00100 
00101   Portable_Timer()
00102   {
00103     m_clkid = BtlConfig::Instance.realclock ? CLOCK_REALTIME : CLOCK_PROCESS_CPUTIME_ID;
00104   }
00105 
00106   Portable_Timer(int clkid) : m_clkid(clkid)
00107   {}
00108 
00109   void start()
00110   {
00111     timespec ts;
00112     clock_gettime(m_clkid, &ts);
00113     m_start_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
00114 
00115   }
00116 
00117   void stop()
00118   {
00119     timespec ts;
00120     clock_gettime(m_clkid, &ts);
00121     m_stop_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
00122 
00123   }
00124 
00125   double elapsed()
00126   {
00127     return  user_time();
00128   }
00129 
00130   double user_time()
00131   {
00132     return m_stop_time - m_start_time;
00133   }
00134 
00135 
00136 private:
00137 
00138   int m_clkid;
00139   double m_stop_time, m_start_time;
00140 
00141 }; // Portable_Timer
00142 
00143 #endif
00144 
00145 #endif  // PORTABLE_TIMER_HPP


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:09