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