| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * A simple implementation of the [Stopwatch] interface. | 6 * A simple implementation of the [Stopwatch] interface. |
| 7 */ | 7 */ |
| 8 class StopwatchImplementation implements Stopwatch { | 8 class StopwatchImplementation implements Stopwatch { |
| 9 // The _start and _stop fields capture the time when [start] and [stop] | 9 // The _start and _stop fields capture the time when [start] and [stop] |
| 10 // are called respectively. | 10 // are called respectively. |
| 11 // If _start is null, then the [Stopwatch] has not been started yet. | 11 // If _start is null, then the [Stopwatch] has not been started yet. |
| 12 // If _stop is null, then the [Stopwatch] has not been stopped yet, | 12 // If _stop is null, then the [Stopwatch] has not been stopped yet, |
| 13 // or is running. | 13 // or is running. |
| 14 int _start; | 14 int _start; |
| 15 int _stop; | 15 int _stop; |
| 16 | 16 |
| 17 StopwatchImplementation() : _start = null, _stop = null {} | 17 StopwatchImplementation() : _start = null, _stop = null {} |
| 18 | 18 |
| 19 void start() { | 19 void start() { |
| 20 if (_start === null) { | 20 if (_start == null) { |
| 21 // This stopwatch has never been started. | 21 // This stopwatch has never been started. |
| 22 _start = _now(); | 22 _start = _now(); |
| 23 } else { | 23 } else { |
| 24 if (_stop === null) { | 24 if (_stop == null) { |
| 25 return; | 25 return; |
| 26 } | 26 } |
| 27 // Restarting this stopwatch. Prepend the elapsed time to the current | 27 // Restarting this stopwatch. Prepend the elapsed time to the current |
| 28 // start time. | 28 // start time. |
| 29 _start = _now() - (_stop - _start); | 29 _start = _now() - (_stop - _start); |
| 30 _stop = null; | 30 _stop = null; |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 void stop() { | 34 void stop() { |
| 35 if (_start === null || _stop !== null) { | 35 if (_start == null || _stop != null) { |
| 36 return; | 36 return; |
| 37 } | 37 } |
| 38 _stop = _now(); | 38 _stop = _now(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void reset() { | 41 void reset() { |
| 42 if (_start === null) return; | 42 if (_start == null) return; |
| 43 // If [_start] is not null, then the stopwatch had already been started. It | 43 // If [_start] is not null, then the stopwatch had already been started. It |
| 44 // may running right now. | 44 // may running right now. |
| 45 _start = _now(); | 45 _start = _now(); |
| 46 if (_stop !== null) { | 46 if (_stop != null) { |
| 47 // The watch is not running. So simply set the [_stop] to [_start] thus | 47 // The watch is not running. So simply set the [_stop] to [_start] thus |
| 48 // having an elapsed time of 0. | 48 // having an elapsed time of 0. |
| 49 _stop = _start; | 49 _stop = _start; |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 int elapsed() { | 53 int elapsed() { |
| 54 if (_start === null) { | 54 if (_start == null) { |
| 55 return 0; | 55 return 0; |
| 56 } | 56 } |
| 57 return (_stop === null) ? (_now() - _start) : (_stop - _start); | 57 return (_stop == null) ? (_now() - _start) : (_stop - _start); |
| 58 } | 58 } |
| 59 | 59 |
| 60 int elapsedInUs() { | 60 int elapsedInUs() { |
| 61 return (elapsed() * 1000000) ~/ frequency(); | 61 return (elapsed() * 1000000) ~/ frequency(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 int elapsedInMs() { | 64 int elapsedInMs() { |
| 65 return (elapsed() * 1000) ~/ frequency(); | 65 return (elapsed() * 1000) ~/ frequency(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 int frequency() => _frequency(); | 68 int frequency() => _frequency(); |
| 69 | 69 |
| 70 external static int _frequency(); | 70 external static int _frequency(); |
| 71 external static int _now(); | 71 external static int _now(); |
| 72 } | 72 } |
| OLD | NEW |