Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOProfilingStopwatch.h
Go to the documentation of this file.
1/*
2
3OOProfilingStopwatch.h
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
31#ifndef OOSTOPWATCH_STANDALONE
32#import "OOCocoa.h"
34#import "OOTypes.h"
35#endif
36
37
38/* Platform-specific high-resolution timer:
39
40 OOHighResTimeValue is a time value. This could be a scalar, struct or pointer.
41 OOHighResTimeValue OOGetHighResTime(void) returns the current time.
42 OODisposeHighResTime() destroys an existing OOHighResTimeValue, if necessary.
43 It must do nothing if the value passed is zeroed out.
44 OOCopyHighResTime(x) returns a timer value equal to x.
45 OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
46 returns the difference between two time values, in seconds.
47*/
48
49#if OOLITE_MAC_OS_X
50
51// Mac OS X: always use MACH_ABSOLUTE_TIME.
52#define OO_PROFILING_STOPWATCH_MACH_ABSOLUTE_TIME 1
53#import <mach/mach_time.h>
54
55typedef uint64_t OOHighResTimeValue;
56
57#define OOGetHighResTime mach_absolute_time
58#define OODisposeHighResTime(time) do { (void)time; } while (0)
59#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
60
61#elif OOLITE_WINDOWS
62
63// Windows: if standalone, use timeGetTime...
64#if OOSTOPWATCH_STANDALONE
65#define OO_PROFILING_STOPWATCH_WINDOWS 1
66typedef DWORD OOHighResTimeValue; // Rolls over once every 50 days, but we can live with that.
67
68// Note: timeGetTime returns time in milliseconds. This results in lower time resolution in Windows, but at this stage I
69// don't think we need to do something about it. If we really need microseond precision, we might consider an implementation
70// based on the Win32 API QueryPerformanceCounter function - Nikos 20100615.
71#define OOGetHighResTime timeGetTime
72#define OODisposeHighResTime(time) do { (void)time; } while (0)
73#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
74
75#else
76/* ...otherwise, use JS_Now() for higher precision. The Windows implementation
77 does the messy work of calibrating performance counters against low-res
78 timers.
79*/
80#define OO_PROFILING_STOPWATCH_JS_NOW 1
81#endif
82
83#else
84
85// Other platforms (presumed unixy): use gettimeofday().
86
87#define OO_PROFILING_STOPWATCH_GETTIMEOFDAY 1
88#include <sys/time.h>
89
90typedef struct timeval OOHighResTimeValue;
91
93{
94 struct timeval tv;
95 gettimeofday(&tv, NULL);
96 return tv;
97}
98
99#define OODisposeHighResTime(time) do { (void)time; } while (0)
100#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
101
102#endif
103
104#if OO_PROFILING_STOPWATCH_JS_NOW
105#include <jsapi.h>
106typedef int64 OOHighResTimeValue;
107
108#define OOGetHighResTime JS_Now
109#define OODisposeHighResTime(time) do { (void)time; } while (0)
110#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
111#endif
112
114
115
116@interface OOProfilingStopwatch: NSObject
117{
118@private
122}
123
124+ (instancetype) stopwatch; // New stopwatch is initially started.
125
126- (void) start;
127- (void) stop;
128- (OOTimeDelta) currentTime; // Returns stop time - start time if stopped, or now - start time if running.
129
130/* Resets timer to zero, returning the current value. This is drift-free, i.e.
131 if it is called twice in a row while running the sum is an accurate time
132 since the timer started.
133*/
134- (OOTimeDelta) reset;
135
136@end
#define OOINLINE
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
uint64_t OOHighResTimeValue
#define OOGetHighResTime
double OOTimeDelta
Definition OOTypes.h:224
OOHighResTimeValue _start