Line data Source code
1 0 : /* 2 : 3 : OOProfilingStopwatch.m 4 : Oolite 5 : 6 : Testing utility to monitor elapsed times at high precision. 7 : 8 : 9 : Copyright (C) 2010-2013 Jens Ayton and contributors 10 : 11 : Permission is hereby granted, free of charge, to any person obtaining a copy 12 : of this software and associated documentation files (the "Software"), to deal 13 : in the Software without restriction, including without limitation the rights 14 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 : copies of the Software, and to permit persons to whom the Software is 16 : furnished to do so, subject to the following conditions: 17 : 18 : The above copyright notice and this permission notice shall be included in all 19 : copies or substantial portions of the Software. 20 : 21 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 : SOFTWARE. 28 : 29 : */ 30 : 31 : #import "OOProfilingStopwatch.h" 32 : 33 : 34 : @implementation OOProfilingStopwatch 35 : 36 0 : - (id) init 37 : { 38 : if ((self = [super init])) 39 : { 40 : _start = OOGetHighResTime(); 41 : _end = OOCopyHighResTime(_start); 42 : _running = YES; 43 : } 44 : return self; 45 : } 46 : 47 : 48 : + (instancetype) stopwatch 49 : { 50 : return [[[self alloc] init] autorelease]; 51 : } 52 : 53 : 54 0 : - (void) dealloc 55 : { 56 : OODisposeHighResTime(_start); 57 : OODisposeHighResTime(_end); 58 : 59 : [super dealloc]; 60 : } 61 : 62 : 63 : - (void) start 64 : { 65 : OOHighResTimeValue temp = _start; 66 : _start = OOGetHighResTime(); 67 : OODisposeHighResTime(temp); 68 : _running = YES; 69 : } 70 : 71 : 72 : - (void) stop 73 : { 74 : OOHighResTimeValue temp = _start; 75 : _end = OOGetHighResTime(); 76 : OODisposeHighResTime(temp); 77 : _running = NO; 78 : } 79 : 80 : 81 : - (OOTimeDelta) currentTime 82 : { 83 : if (_running) 84 : { 85 : OOHighResTimeValue temp = _end; 86 : _end = OOGetHighResTime(); 87 : OODisposeHighResTime(temp); 88 : } 89 : return OOHighResTimeDeltaInSeconds(_start, _end); 90 : } 91 : 92 : 93 : - (OOTimeDelta) reset 94 : { 95 : OOTimeDelta result; 96 : if (_running) 97 : { 98 : OOHighResTimeValue now = OOGetHighResTime(); 99 : result = OOHighResTimeDeltaInSeconds(_start, now); 100 : OODisposeHighResTime(_start); 101 : _start = now; 102 : } 103 : else 104 : { 105 : result = OOHighResTimeDeltaInSeconds(_start, _end); 106 : OODisposeHighResTime(_end); 107 : _end = OOCopyHighResTime(_start); 108 : } 109 : return result; 110 : } 111 : 112 : @end 113 : 114 : 115 0 : OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime) 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 : }