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