NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timer.cpp
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <nvbio/basic/timer.h>
29 
30 #ifdef WIN32
31 
32 #include <windows.h>
33 #include <winbase.h>
34 
35 namespace nvbio {
36 
38 {
39  LARGE_INTEGER freq;
40  QueryPerformanceFrequency(&freq);
41 
42  m_freq = freq.QuadPart;
43 }
44 
45 void Timer::start()
46 {
47  LARGE_INTEGER tick;
48  QueryPerformanceCounter(&tick);
49 
50  m_start = tick.QuadPart;
51 }
52 void Timer::stop()
53 {
54  LARGE_INTEGER tick;
55  QueryPerformanceCounter(&tick);
56 
57  m_stop = tick.QuadPart;
58 }
59 
60 float Timer::seconds() const
61 {
62  return float(double(m_stop - m_start) / double(m_freq));
63 }
64 
65 } // namespace nvbio
66 
67 #else
68 
69 #include <time.h>
70 #include <sys/time.h>
71 
72 namespace nvbio {
73 
74 #if 0
75 
76 void Timer::start()
77 {
78  m_start = clock();
79 }
80 void Timer::stop()
81 {
82  m_stop = clock();
83 }
84 
85 float Timer::seconds() const
86 {
87  return float(m_stop - m_start) / float(CLOCKS_PER_SEC);
88 }
89 
90 #elif 0
91 
92 void Timer::start()
93 {
94  timespec _time;
95  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&_time);
96  m_start = _time.tv_sec;
97  m_start_ns = _time.tv_nsec;
98 }
99 void Timer::stop()
100 {
101  timespec _time;
102  clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&_time);
103  m_stop = _time.tv_sec;
104  m_stop_ns = _time.tv_nsec;
105 }
106 
107 float Timer::seconds() const
108 {
109  if (m_stop_ns < m_start_ns)
110  return float( double(m_stop - m_start - 1) + double(1000000000 + m_stop_ns - m_start_ns)*1.0e-9 );
111  else
112  return float( double(m_stop - m_start) + double(m_stop_ns - m_start_ns)*1.0e-9 );
113 }
114 
115 #else
116 
118 {
119  timeval _time;
120  gettimeofday(&_time,NULL);
121  m_start = _time.tv_sec;
122  m_start_ns = _time.tv_usec;
123 }
125 {
126  timeval _time;
127  gettimeofday(&_time,NULL);
128  m_stop = _time.tv_sec;
129  m_stop_ns = _time.tv_usec;
130 }
131 
132 float Timer::seconds() const
133 {
134  if (m_stop_ns < m_start_ns)
135  return float( double(m_stop - m_start - 1) + double(1000000 + m_stop_ns - m_start_ns)*1.0e-6 );
136  else
137  return float( double(m_stop - m_start) + double(m_stop_ns - m_start_ns)*1.0e-6 );
138 }
139 
140 #endif
141 
142 } // namespace nvbio
143 
144 #endif