Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Timer.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Scientific Computing and Imaging Institute,
10 // University of Utah (USA) and Department of Aeronautics, Imperial
11 // College London (UK).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Time getting class
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 
38 namespace Nektar
39 {
41  m_start(),
42  m_end(),
43  m_resolution()
44  {
45  }
46 
48  {
49  }
50 
51  void Timer::Start()
52  {
53  #ifdef _WIN32
54  QueryPerformanceCounter(&m_start);
55  #elif defined(__APPLE__)
56  gettimeofday(&m_start, 0);
57  #else
58  clock_gettime(CLOCK_REALTIME, &m_start);
59  #endif
60  }
61 
62  void Timer::Stop()
63  {
64  #ifdef _WIN32
65  QueryPerformanceCounter(&m_end);
66  #elif defined(__APPLE__)
67  gettimeofday(&m_end, 0);
68  #else
69  clock_gettime(CLOCK_REALTIME, &m_end);
70  #endif
71  }
72 
74  {
75  #ifdef _WIN32
76  CounterType result;
77  result.QuadPart = m_end.QuadPart - m_start.QuadPart;
78  return result;
79  #elif defined(__APPLE__)
80  CounterType result = m_end;
81 
82  if( result.tv_usec < m_start.tv_usec)
83  {
84  result.tv_sec -= 1;
85  result.tv_usec += 1000000;
86  }
87 
88  result.tv_sec -= m_start.tv_sec;
89  result.tv_usec -= m_start.tv_usec;
90 
91  return result;
92  #else
93  CounterType result = m_end;
94 
95  if( result.tv_nsec < m_start.tv_nsec)
96  {
97  result.tv_sec -= 1;
98  result.tv_nsec += 1000000000;
99  }
100 
101  result.tv_sec -= m_start.tv_sec;
102  result.tv_nsec -= m_start.tv_nsec;
103 
104  return result;
105  #endif
106  }
107 
109  {
110  #ifdef _WIN32
111  CounterType frequency;
112  QueryPerformanceFrequency(&frequency);
113  return Elapsed().QuadPart/static_cast<NekDouble>(n) * 1.0/frequency.QuadPart;
114  #elif defined(__APPLE__)
115  CounterType elapsed = Elapsed();
116  NekDouble result = elapsed.tv_sec/static_cast<NekDouble>(n) +
117  ( elapsed.tv_usec/static_cast<NekDouble>(n) * 1.0e-6);
118  return result;
119  #else
120  CounterType elapsed = Elapsed();
121  NekDouble result = elapsed.tv_sec/static_cast<NekDouble>(n) +
122  ( elapsed.tv_nsec/static_cast<NekDouble>(n) * 1.0e-9);
123  return result;
124  #endif
125  }
126 
127 }