Oolite
Loading...
Searching...
No Matches
OOProfilingStopwatch.m
Go to the documentation of this file.
1/*
2
3OOProfilingStopwatch.m
4Oolite
5
6Testing utility to monitor elapsed times at high precision.
7
8
9Copyright (C) 2010-2013 Jens Ayton and contributors
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files (the "Software"), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28
29*/
30
32
33@implementation OOProfilingStopwatch
34
35- (id)init
36{
37 if ((self = [super init])) {
40 _running = YES;
41 }
42 return self;
43}
44
45+ (instancetype)stopwatch
46{
47 return [[[self alloc] init] autorelease];
48}
49
50- (void)dealloc
51{
54
55 [super dealloc];
56}
57
58- (void)start
59{
63 _running = YES;
64}
65
66- (void)stop
67{
71 _running = NO;
72}
73
83
85{
86 OOTimeDelta result;
87 if (_running) {
91 _start = now;
92 } else {
96 }
97 return result;
98}
99
100@end
101
103{
104#if OO_PROFILING_STOPWATCH_MACH_ABSOLUTE_TIME
105 uint64_t diff = endTime - startTime;
106 static double conversion = 0.0;
107
108 if (EXPECT_NOT(conversion == 0.0)) {
109 mach_timebase_info_data_t info;
110 kern_return_t err = mach_timebase_info(&info);
111
112 if (err == 0) {
113 conversion = 1e-9 * (double)info.numer / (double)info.denom;
114 }
115 }
116
117 return conversion * (double)diff;
118#elif OO_PROFILING_STOPWATCH_WINDOWS
119 return 1e-3 * (double)(endTime - startTime);
120#elif OO_PROFILING_STOPWATCH_PERFORMANCE_COUNTER
121 static double conversion = 0.0;
122
123 if (EXPECT_NOT(conversion == 0.0)) {
124 LARGE_INTEGER frequency;
125 if (QueryPerformanceFrequency(&frequency) && frequency.QuadPart != 0) {
126 conversion = 1.0 / (double)frequency.QuadPart;
127 }
128 }
129
130 return conversion * (double)(endTime - startTime);
131#elif OO_PROFILING_STOPWATCH_GETTIMEOFDAY
132 int32_t deltaS = (int32_t)endTime.tv_sec - (int32_t)startTime.tv_sec;
133 int32_t deltaU = (int32_t)endTime.tv_usec - (int32_t)startTime.tv_usec;
134 double result = deltaU;
135 result = (result * 1e-6) + deltaS;
136 return result;
137#elif OO_PROFILING_STOPWATCH_JS_NOW
138 return 1e-6 * (double)(endTime - startTime);
139#endif
140}
#define EXPECT_NOT(x)
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
uint64_t OOHighResTimeValue
#define OOGetHighResTime
#define OODisposeHighResTime(time)
#define OOCopyHighResTime(time)
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
double OOTimeDelta
Definition OOTypes.h:193
OOHighResTimeValue _start