Oolite 1.91.0.7646-241128-10e222e
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
34@implementation OOProfilingStopwatch
35
36- (id) init
37{
38 if ((self = [super init]))
39 {
42 _running = YES;
43 }
44 return self;
45}
46
47
48+ (instancetype) stopwatch
49{
50 return [[[self alloc] init] autorelease];
51}
52
53
54- (void) dealloc
55{
58
59 [super dealloc];
60}
61
62
63- (void) start
64{
68 _running = YES;
69}
70
71
72- (void) stop
73{
77 _running = NO;
78}
79
80
82{
83 if (_running)
84 {
88 }
90}
91
92
94{
95 OOTimeDelta result;
96 if (_running)
97 {
101 _start = now;
102 }
103 else
104 {
108 }
109 return result;
110}
111
112@end
113
114
116{
117#if OO_PROFILING_STOPWATCH_MACH_ABSOLUTE_TIME
118 uint64_t diff = endTime - startTime;
119 static double conversion = 0.0;
120
121 if (EXPECT_NOT(conversion == 0.0))
122 {
123 mach_timebase_info_data_t info;
124 kern_return_t err = mach_timebase_info(&info);
125
126 if (err == 0)
127 {
128 conversion = 1e-9 * (double)info.numer / (double)info.denom;
129 }
130 }
131
132 return conversion * (double)diff;
133#elif OO_PROFILING_STOPWATCH_WINDOWS
134 return 1e-3 * (double)(endTime - startTime);
135#elif OO_PROFILING_STOPWATCH_GETTIMEOFDAY
136 int_fast32_t deltaS = (int_fast32_t)endTime.tv_sec - (int_fast32_t)startTime.tv_sec;
137 int_fast32_t deltaU = (int_fast32_t)endTime.tv_usec - (int_fast32_t)startTime.tv_usec;
138 double result = deltaU;
139 result = (result * 1e-6) + deltaS;
140 return result;
141#elif OO_PROFILING_STOPWATCH_JS_NOW
142 return 1e-6 * (double)(endTime - startTime);
143#endif
144}
#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:224
OOHighResTimeValue _start