| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * A simple stopwatch interface to measure elapsed time. | |
| 9 */ | |
| 10 class Stopwatch { | |
| 11 /** | |
| 12 * Frequency of the elapsed counter in Hz. | |
| 13 */ | |
| 14 int get frequency => _frequency; | |
| 15 | |
| 16 // The _start and _stop fields capture the time when [start] and [stop] | |
| 17 // are called respectively. | |
| 18 // If _start is null, then the [Stopwatch] has not been started yet. | |
| 19 // If _stop is null, then the [Stopwatch] has not been stopped yet, | |
| 20 // or is running. | |
| 21 int _start; | |
| 22 int _stop; | |
| 23 | |
| 24 /** | |
| 25 * Creates a [Stopwatch] in stopped state with a zero elapsed count. | |
| 26 * | |
| 27 * The following example shows how to start a [Stopwatch] | |
| 28 * immediately after allocation. | |
| 29 * | |
| 30 * Stopwatch stopwatch = new Stopwatch()..start(); | |
| 31 */ | |
| 32 Stopwatch() { | |
| 33 _initTicker(); | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Starts the [Stopwatch]. | |
| 38 * | |
| 39 * The [elapsed] count is increasing monotonically. If the [Stopwatch] has | |
| 40 * been stopped, then calling start again restarts it without resetting the | |
| 41 * [elapsed] count. | |
| 42 * | |
| 43 * If the [Stopwatch] is currently running, then calling start does nothing. | |
| 44 */ | |
| 45 void start() { | |
| 46 if (isRunning) return; | |
| 47 if (_start == null) { | |
| 48 // This stopwatch has never been started. | |
| 49 _start = _now(); | |
| 50 } else { | |
| 51 // Restart this stopwatch. Prepend the elapsed time to the current | |
| 52 // start time. | |
| 53 _start = _now() - (_stop - _start); | |
| 54 _stop = null; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Stops the [Stopwatch]. | |
| 60 * | |
| 61 * The [elapsedTicks] count stops increasing after this call. If the | |
| 62 * [Stopwatch] is currently not running, then calling this method has no | |
| 63 * effect. | |
| 64 */ | |
| 65 void stop() { | |
| 66 if (!isRunning) return; | |
| 67 _stop = _now(); | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Resets the [elapsed] count to zero. | |
| 72 * | |
| 73 * This method does not stop or start the [Stopwatch]. | |
| 74 */ | |
| 75 void reset() { | |
| 76 if (_start == null) return; | |
| 77 // If [_start] is not null, then the stopwatch had already been started. It | |
| 78 // may running right now. | |
| 79 _start = _now(); | |
| 80 if (_stop != null) { | |
| 81 // The watch is not running. So simply set the [_stop] to [_start] thus | |
| 82 // having an elapsed time of 0. | |
| 83 _stop = _start; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Returns the elapsed number of clock ticks since calling [start] while the | |
| 89 * [Stopwatch] is running. | |
| 90 * | |
| 91 * Returns the elapsed number of clock ticks between calling [start] and | |
| 92 * calling [stop]. | |
| 93 * | |
| 94 * Returns 0 if the [Stopwatch] has never been started. | |
| 95 * | |
| 96 * The elapsed number of clock ticks increases by [frequency] every second. | |
| 97 */ | |
| 98 int get elapsedTicks { | |
| 99 if (_start == null) { | |
| 100 return 0; | |
| 101 } | |
| 102 return (_stop == null) ? (_now() - _start) : (_stop - _start); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Returns the [elapsedTicks] counter converted to a [Duration]. | |
| 107 */ | |
| 108 Duration get elapsed { | |
| 109 return new Duration(microseconds: elapsedMicroseconds); | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Returns the [elapsedTicks] counter converted to microseconds. | |
| 114 */ | |
| 115 int get elapsedMicroseconds { | |
| 116 return (elapsedTicks * 1000000) ~/ frequency; | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * Returns the [elapsedTicks] counter converted to milliseconds. | |
| 121 */ | |
| 122 int get elapsedMilliseconds { | |
| 123 return (elapsedTicks * 1000) ~/ frequency; | |
| 124 } | |
| 125 | |
| 126 | |
| 127 /** | |
| 128 * Returns wether the [StopWatch] is currently running. | |
| 129 */ | |
| 130 bool get isRunning => _start != null && _stop == null; | |
| 131 | |
| 132 /** | |
| 133 * Cached frequency of the system. Must be initialized in [_initTicker]; | |
| 134 */ | |
| 135 static int _frequency; | |
| 136 | |
| 137 /** | |
| 138 * Initializes the time-measuring system. *Must* initialize the [_frequency] | |
| 139 * variable. | |
| 140 */ | |
| 141 external static void _initTicker(); | |
| 142 external static int _now(); | |
| 143 } | |
| OLD | NEW |