| 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 StopwatchImplementation.start() : _start = null, _stop = null { | |
| 19 start(); | |
| 20 } | |
| 21 | |
| 22 void start() { | |
| 23 if (_start === null) { | |
| 24 // This stopwatch has never been started. | |
| 25 _start = _now(); | |
| 26 } else { | |
| 27 if (_stop === null) { | |
| 28 return; | |
| 29 } | |
| 30 // Restarting this stopwatch. Prepend the elapsed time to the current | |
| 31 // start time. | |
| 32 _start = _now() - (_stop - _start); | |
| 33 _stop = null; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void stop() { | |
| 38 if (_start === null || _stop !== null) { | |
| 39 return; | |
| 40 } | |
| 41 _stop = _now(); | |
| 42 } | |
| 43 | |
| 44 void reset() { | |
| 45 if (_start === null) return; | |
| 46 // If [_start] is not null, then the stopwatch had already been started. It | |
| 47 // may running right now. | |
| 48 _start = _now(); | |
| 49 if (_stop !== null) { | |
| 50 // The watch is not running. So simply set the [_stop] to [_start] thus | |
| 51 // having an elapsed time of 0. | |
| 52 _stop = _start; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 int elapsed() { | |
| 57 if (_start === null) { | |
| 58 return 0; | |
| 59 } | |
| 60 return (_stop === null) ? (_now() - _start) : (_stop - _start); | |
| 61 } | |
| 62 | |
| 63 int elapsedInUs() { | |
| 64 return (elapsed() * 1000000) ~/ frequency(); | |
| 65 } | |
| 66 | |
| 67 int elapsedInMs() { | |
| 68 return (elapsed() * 1000) ~/ frequency(); | |
| 69 } | |
| 70 | |
| 71 int frequency() => _frequency(); | |
| 72 | |
| 73 external static int _frequency(); | |
| 74 external static int _now(); | |
| 75 } | |
| OLD | NEW |