portable_timer.hh revision c981c48f5bc9aefeffc0bcb0cc3934c2fae179dd
1//=====================================================
2// File   :  portable_timer.hh
3// Author :  L. Plagne <laurent.plagne@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#ifdef _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#else
91
92#include <sys/time.h>
93#include <sys/resource.h>
94#include <unistd.h>
95#include <sys/times.h>
96
97class Portable_Timer
98{
99 public:
100
101  Portable_Timer()
102  {
103    m_clkid = BtlConfig::Instance.realclock ? CLOCK_REALTIME : CLOCK_PROCESS_CPUTIME_ID;
104  }
105
106  Portable_Timer(int clkid) : m_clkid(clkid)
107  {}
108
109  void start()
110  {
111    timespec ts;
112    clock_gettime(m_clkid, &ts);
113    m_start_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
114
115  }
116
117  void stop()
118  {
119    timespec ts;
120    clock_gettime(m_clkid, &ts);
121    m_stop_time = double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
122
123  }
124
125  double elapsed()
126  {
127    return  user_time();
128  }
129
130  double user_time()
131  {
132    return m_stop_time - m_start_time;
133  }
134
135
136private:
137
138  int m_clkid;
139  double m_stop_time, m_start_time;
140
141}; // Portable_Timer
142
143#endif
144
145#endif  // PORTABLE_TIMER_HPP
146