| 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 abstract class Stopwatch { | 10 abstract class Stopwatch { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * Returns the [elapsedTicks] counter converted to milliseconds. | 58 * Returns the [elapsedTicks] counter converted to milliseconds. |
| 59 */ | 59 */ |
| 60 int get elapsedMilliseconds; | 60 int get elapsedMilliseconds; |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * Returns the frequency of the elapsed counter in Hz. | 63 * Returns the frequency of the elapsed counter in Hz. |
| 64 */ | 64 */ |
| 65 int get frequency; | 65 int get frequency; |
| 66 |
| 67 /** |
| 68 * Returns wether the [StopWatch] is currently running. |
| 69 */ |
| 70 bool get isRunning; |
| 66 } | 71 } |
| 67 | 72 |
| 68 class _StopwatchImpl implements Stopwatch { | 73 class _StopwatchImpl implements Stopwatch { |
| 69 // The _start and _stop fields capture the time when [start] and [stop] | 74 // The _start and _stop fields capture the time when [start] and [stop] |
| 70 // are called respectively. | 75 // are called respectively. |
| 71 // If _start is null, then the [Stopwatch] has not been started yet. | 76 // If _start is null, then the [Stopwatch] has not been started yet. |
| 72 // If _stop is null, then the [Stopwatch] has not been stopped yet, | 77 // If _stop is null, then the [Stopwatch] has not been stopped yet, |
| 73 // or is running. | 78 // or is running. |
| 74 int _start; | 79 int _start; |
| 75 int _stop; | 80 int _stop; |
| 76 | 81 |
| 77 _StopwatchImpl() : _start = null, _stop = null {} | 82 _StopwatchImpl() : _start = null, _stop = null {} |
| 78 | 83 |
| 79 void start() { | 84 void start() { |
| 85 if (isRunning) return; |
| 80 if (_start == null) { | 86 if (_start == null) { |
| 81 // This stopwatch has never been started. | 87 // This stopwatch has never been started. |
| 82 _start = _now(); | 88 _start = _now(); |
| 83 } else { | 89 } else { |
| 84 if (_stop == null) { | 90 // Restart this stopwatch. Prepend the elapsed time to the current |
| 85 return; | |
| 86 } | |
| 87 // Restarting this stopwatch. Prepend the elapsed time to the current | |
| 88 // start time. | 91 // start time. |
| 89 _start = _now() - (_stop - _start); | 92 _start = _now() - (_stop - _start); |
| 90 _stop = null; | 93 _stop = null; |
| 91 } | 94 } |
| 92 } | 95 } |
| 93 | 96 |
| 94 void stop() { | 97 void stop() { |
| 95 if (_start == null || _stop != null) { | 98 if (!isRunning) return; |
| 96 return; | |
| 97 } | |
| 98 _stop = _now(); | 99 _stop = _now(); |
| 99 } | 100 } |
| 100 | 101 |
| 101 void reset() { | 102 void reset() { |
| 102 if (_start == null) return; | 103 if (_start == null) return; |
| 103 // If [_start] is not null, then the stopwatch had already been started. It | 104 // If [_start] is not null, then the stopwatch had already been started. It |
| 104 // may running right now. | 105 // may running right now. |
| 105 _start = _now(); | 106 _start = _now(); |
| 106 if (_stop != null) { | 107 if (_stop != null) { |
| 107 // The watch is not running. So simply set the [_stop] to [_start] thus | 108 // The watch is not running. So simply set the [_stop] to [_start] thus |
| (...skipping 12 matching lines...) Expand all Loading... |
| 120 int get elapsedMicroseconds { | 121 int get elapsedMicroseconds { |
| 121 return (elapsedTicks * 1000000) ~/ frequency; | 122 return (elapsedTicks * 1000000) ~/ frequency; |
| 122 } | 123 } |
| 123 | 124 |
| 124 int get elapsedMilliseconds { | 125 int get elapsedMilliseconds { |
| 125 return (elapsedTicks * 1000) ~/ frequency; | 126 return (elapsedTicks * 1000) ~/ frequency; |
| 126 } | 127 } |
| 127 | 128 |
| 128 int get frequency => _frequency(); | 129 int get frequency => _frequency(); |
| 129 | 130 |
| 131 bool get isRunning => _start != null && _stop == null; |
| 132 |
| 130 external static int _frequency(); | 133 external static int _frequency(); |
| 131 external static int _now(); | 134 external static int _now(); |
| 132 } | 135 } |
| OLD | NEW |