Home | History | Annotate | Download | only in timers
      1 //=====================================================
      2 // File   :  portable_timer.hh
      3 // Author :  L. Plagne <laurent.plagne (at) edf.fr)> from boost lib
      4 // Copyright (C) EDF R&D,  lun sep 30 14:23:17 CEST 2002
      5 //=====================================================
      6 //
      7 // This program is free software; you can redistribute it and/or
      8 // modify it under the terms of the GNU General Public License
      9 // as published by the Free Software Foundation; either version 2
     10 // of the License, or (at your option) any later version.
     11 //
     12 // This program is distributed in the hope that it will be useful,
     13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 // GNU General Public License for more details.
     16 // You should have received a copy of the GNU General Public License
     17 // along with this program; if not, write to the Free Software
     18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     19 //
     20 //  simple_time extracted from the boost library
     21 //
     22 #ifndef _PORTABLE_TIMER_HH
     23 #define _PORTABLE_TIMER_HH
     24 
     25 #include <ctime>
     26 #include <cstdlib>
     27 
     28 #include <time.h>
     29 
     30 
     31 #define USEC_IN_SEC 1000000
     32 
     33 
     34 //  timer  -------------------------------------------------------------------//
     35 
     36 //  A timer object measures CPU time.
     37 #if defined(_MSC_VER)
     38 
     39 #define NOMINMAX
     40 #include <windows.h>
     41 
     42 /*#ifndef hr_timer
     43 #include "hr_time.h"
     44 #define hr_timer
     45 #endif*/
     46 
     47  class Portable_Timer
     48  {
     49   public:
     50 
     51    typedef struct {
     52     LARGE_INTEGER start;
     53     LARGE_INTEGER stop;
     54    } stopWatch;
     55 
     56 
     57    Portable_Timer()
     58    {
     59 	 startVal.QuadPart = 0;
     60 	 stopVal.QuadPart = 0;
     61 	 QueryPerformanceFrequency(&frequency);
     62    }
     63 
     64    void start() { QueryPerformanceCounter(&startVal); }
     65 
     66    void stop() { QueryPerformanceCounter(&stopVal); }
     67 
     68    double elapsed() {
     69 	 LARGE_INTEGER time;
     70      time.QuadPart = stopVal.QuadPart - startVal.QuadPart;
     71      return LIToSecs(time);
     72    }
     73 
     74    double user_time() { return elapsed(); }
     75 
     76 
     77  private:
     78 
     79    double LIToSecs(LARGE_INTEGER& L) {
     80      return ((double)L.QuadPart /(double)frequency.QuadPart) ;
     81    }
     82 
     83    LARGE_INTEGER startVal;
     84    LARGE_INTEGER stopVal;
     85    LARGE_INTEGER frequency;
     86 
     87 
     88  }; // Portable_Timer
     89 
     90 #elif defined(__APPLE__)
     91 #include <CoreServices/CoreServices.h>
     92 #include <mach/mach_time.h>
     93 
     94 
     95 class Portable_Timer
     96 {
     97  public:
     98 
     99   Portable_Timer()
    100   {
    101   }
    102 
    103   void start()
    104   {
    105     m_start_time = double(mach_absolute_time())*1e-9;;
    106 
    107   }
    108 
    109   void stop()
    110   {
    111     m_stop_time = double(mach_absolute_time())*1e-9;;
    112 
    113   }
    114 
    115   double elapsed()
    116   {
    117     return  user_time();
    118   }
    119 
    120   double user_time()
    121   {
    122     return m_stop_time - m_start_time;
    123   }
    124 
    125 
    126 private:
    127 
    128   double m_stop_time, m_start_time;
    129 
    130 }; // Portable_Timer (Apple)
    131 
    132 #else
    133 
    134 #include <sys/time.h>
    135 #include <sys/resource.h>
    136 #include <unistd.h>
    137 #include <sys/times.h>
    138 
    139 class Portable_Timer
    140 {
    141  public:
    142 
    143   Portable_Timer()
    144   {
    145     m_clkid = BtlConfig::Instance.realclock ? CLOCK_REALTIME : CLOCK_PROCESS_CPUTIME_ID;
    146   }
    147 
    148   Portable_Timer(int clkid) : m_clkid(clkid)
    149   {}
    150 
    151   void start()
    152   {
    153     timespec ts;
    154     clock_gettime(m_clkid, &ts);
    155     m_start_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
    156 
    157   }
    158 
    159   void stop()
    160   {
    161     timespec ts;
    162     clock_gettime(m_clkid, &ts);
    163     m_stop_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
    164 
    165   }
    166 
    167   double elapsed()
    168   {
    169     return  user_time();
    170   }
    171 
    172   double user_time()
    173   {
    174     return m_stop_time - m_start_time;
    175   }
    176 
    177 
    178 private:
    179 
    180   int m_clkid;
    181   double m_stop_time, m_start_time;
    182 
    183 }; // Portable_Timer (Linux)
    184 
    185 #endif
    186 
    187 #endif  // PORTABLE_TIMER_HPP
    188