libigl v2.5.0
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1// This file is part of libigl, a simple c++ geometry processing library.
2//
3// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
4//
5// This Source Code Form is subject to the terms of the Mozilla Public License
6// v. 2.0. If a copy of the MPL was not distributed with this file, You can
7// obtain one at http://mozilla.org/MPL/2.0/.
8// High Resolution Timer.
9//
10// Resolution on Mac (clock tick)
11// Resolution on Linux (1 us not tested)
12// Resolution on Windows (clock tick not tested)
13
14#ifndef IGL_TIMER_H
15#define IGL_TIMER_H
16
17#ifdef WIN32 // Windows system specific
18#include <windows.h>
19#elif __APPLE__ // Unix based system specific
20#include <mach/mach_time.h> // for mach_absolute_time
21#else
22#include <sys/time.h>
23#endif
24#include <cstddef>
25#include <cstdint>
26
27namespace igl
28{
30 class Timer
31 {
32 public:
35 stopped(0),
36#ifdef WIN32
37 frequency(),
38 startCount(),
39 endCount()
40#elif __APPLE__
41 startCount(0),
42 endCount(0)
43#else
44 startCount(),
45 endCount()
46#endif
47 {
48#ifdef WIN32
49 QueryPerformanceFrequency(&frequency);
50 startCount.QuadPart = 0;
51 endCount.QuadPart = 0;
52#elif __APPLE__
53 startCount = 0;
54 endCount = 0;
55#else
56 startCount.tv_sec = startCount.tv_usec = 0;
57 endCount.tv_sec = endCount.tv_usec = 0;
58#endif
59
60 stopped = 0;
61 }
62 // default destructor
64 {
65
66 }
67
68#ifdef __APPLE__
73 double subtractTimes( std::uint64_t endTime, std::uint64_t startTime )
74 {
75 std::uint64_t difference = endTime - startTime;
76 static double conversion = 0.0;
77
78 if( conversion == 0.0 )
79 {
80 mach_timebase_info_data_t info;
81 kern_return_t err = mach_timebase_info( &info );
82
83 //Convert the timebase into seconds
84 if( err == 0 )
85 conversion = 1e-9 * (double) info.numer / (double) info.denom;
86 }
87
88 return conversion * (double) difference;
89 }
90#endif
91
93 void start()
94 {
95 stopped = 0; // reset stop flag
96#ifdef WIN32
97 QueryPerformanceCounter(&startCount);
98#elif __APPLE__
99 startCount = mach_absolute_time();
100#else
101 gettimeofday(&startCount, NULL);
102#endif
103
104 }
105
107 void stop()
108 {
109 stopped = 1; // set timer stopped flag
110
111#ifdef WIN32
112 QueryPerformanceCounter(&endCount);
113#elif __APPLE__
114 endCount = mach_absolute_time();
115#else
116 gettimeofday(&endCount, NULL);
117#endif
118
119 }
120
123 {
124 return this->getElapsedTimeInSec();
125 }
126
129 {
130 return this->getElapsedTimeInMicroSec() * 0.000001;
131 }
132
136 {
137 return this->getElapsedTimeInMicroSec() * 0.001;
138 }
139
142 {
143 double startTimeInMicroSec = 0;
144 double endTimeInMicroSec = 0;
145
146#ifdef WIN32
147 if(!stopped)
148 QueryPerformanceCounter(&endCount);
149
150 startTimeInMicroSec =
151 startCount.QuadPart * (1000000.0 / frequency.QuadPart);
152 endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
153#elif __APPLE__
154 if (!stopped)
155 endCount = mach_absolute_time();
156
157 return subtractTimes(endCount,startCount)/1e-6;
158#else
159 if(!stopped)
160 gettimeofday(&endCount, NULL);
161
162 startTimeInMicroSec =
163 (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
164 endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
165#endif
166
167 return endTimeInMicroSec - startTimeInMicroSec;
168 }
169
170 private:
171 // stop flag
172 int stopped;
173#ifdef WIN32
174 // ticks per second
175 LARGE_INTEGER frequency;
176 LARGE_INTEGER startCount;
177 LARGE_INTEGER endCount;
178#elif __APPLE__
179 std::uint64_t startCount;
180 std::uint64_t endCount;
181#else
182 timeval startCount;
183 timeval endCount;
184#endif
185 };
186}
187#endif // TIMER_H_DEF
188
double getElapsedTimeInMicroSec()
get elapsed time in micro-second
Definition Timer.h:141
~Timer()
Definition Timer.h:63
double getElapsedTimeInSec()
get elapsed time in second (same as getElapsedTime)
Definition Timer.h:128
void stop()
stop the timer
Definition Timer.h:107
Timer()
default constructor
Definition Timer.h:34
double getElapsedTime()
get elapsed time in second
Definition Timer.h:122
void start()
start timer
Definition Timer.h:93
double getElapsedTimeInMilliSec()
get elapsed time in milli-second
Definition Timer.h:135
Definition AABB.h:18