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
26namespace igl
27{
29 class Timer
30 {
31 public:
34 stopped(0),
35#ifdef WIN32
36 frequency(),
37 startCount(),
38 endCount()
39#elif __APPLE__
40 startCount(0),
41 endCount(0)
42#else
43 startCount(),
44 endCount()
45#endif
46 {
47#ifdef WIN32
48 QueryPerformanceFrequency(&frequency);
49 startCount.QuadPart = 0;
50 endCount.QuadPart = 0;
51#elif __APPLE__
52 startCount = 0;
53 endCount = 0;
54#else
55 startCount.tv_sec = startCount.tv_usec = 0;
56 endCount.tv_sec = endCount.tv_usec = 0;
57#endif
58
59 stopped = 0;
60 }
61 // default destructor
63 {
64
65 }
66
67#ifdef __APPLE__
72 double subtractTimes( uint64_t endTime, uint64_t startTime )
73 {
74 uint64_t difference = endTime - startTime;
75 static double conversion = 0.0;
76
77 if( conversion == 0.0 )
78 {
79 mach_timebase_info_data_t info;
80 kern_return_t err = mach_timebase_info( &info );
81
82 //Convert the timebase into seconds
83 if( err == 0 )
84 conversion = 1e-9 * (double) info.numer / (double) info.denom;
85 }
86
87 return conversion * (double) difference;
88 }
89#endif
90
92 void start()
93 {
94 stopped = 0; // reset stop flag
95#ifdef WIN32
96 QueryPerformanceCounter(&startCount);
97#elif __APPLE__
98 startCount = mach_absolute_time();
99#else
100 gettimeofday(&startCount, NULL);
101#endif
102
103 }
104
106 void stop()
107 {
108 stopped = 1; // set timer stopped flag
109
110#ifdef WIN32
111 QueryPerformanceCounter(&endCount);
112#elif __APPLE__
113 endCount = mach_absolute_time();
114#else
115 gettimeofday(&endCount, NULL);
116#endif
117
118 }
122 {
123 return this->getElapsedTimeInSec();
124 }
128 {
129 return this->getElapsedTimeInMicroSec() * 0.000001;
130 }
131
135 {
136 return this->getElapsedTimeInMicroSec() * 0.001;
137 }
141 {
142 double startTimeInMicroSec = 0;
143 double endTimeInMicroSec = 0;
144
145#ifdef WIN32
146 if(!stopped)
147 QueryPerformanceCounter(&endCount);
148
149 startTimeInMicroSec =
150 startCount.QuadPart * (1000000.0 / frequency.QuadPart);
151 endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
152#elif __APPLE__
153 if (!stopped)
154 endCount = mach_absolute_time();
155
156 return subtractTimes(endCount,startCount)/1e-6;
157#else
158 if(!stopped)
159 gettimeofday(&endCount, NULL);
160
161 startTimeInMicroSec =
162 (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
163 endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
164#endif
165
166 return endTimeInMicroSec - startTimeInMicroSec;
167 }
168
169 private:
170 // stop flag
171 int stopped;
172#ifdef WIN32
173 // ticks per second
174 LARGE_INTEGER frequency;
175 LARGE_INTEGER startCount;
176 LARGE_INTEGER endCount;
177#elif __APPLE__
178 uint64_t startCount;
179 uint64_t endCount;
180#else
181 timeval startCount;
182 timeval endCount;
183#endif
184 };
185}
186#endif // TIMER_H_DEF
187
Simple timer class.
Definition Timer.h:30
double getElapsedTimeInMicroSec()
get elapsed time in micro-second
Definition Timer.h:140
~Timer()
Definition Timer.h:62
double getElapsedTimeInSec()
get elapsed time in second (same as getElapsedTime)
Definition Timer.h:127
void stop()
stop the timer
Definition Timer.h:106
Timer()
default constructor
Definition Timer.h:33
double getElapsedTime()
get elapsed time in second
Definition Timer.h:121
void start()
start timer
Definition Timer.h:92
double getElapsedTimeInMilliSec()
get elapsed time in milli-second
Definition Timer.h:134
Definition AABB.h:17