Chromium Code Reviews| 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 /** | 5 /** |
| 6 * A simple [Stopwatch] interface to measure elapsed time. | 6 * A simple [Stopwatch] interface to measure elapsed time. |
| 7 */ | 7 */ |
| 8 abstract class Stopwatch { | 8 abstract class Stopwatch { |
| 9 /** | 9 /** |
| 10 * Creates a [Stopwatch] in stopped state with a zero elapsed count. | 10 * Creates a [Stopwatch] in stopped state with a zero elapsed count. |
| 11 * | 11 * |
| 12 * The following example shows how to start a [Stopwatch] | 12 * The following example shows how to start a [Stopwatch] |
| 13 * right after allocation. | 13 * right after allocation. |
| 14 * | 14 * |
| 15 * Stopwatch stopwatch = new Stopwatch()..start(); | 15 * Stopwatch stopwatch = new Stopwatch()..start(); |
| 16 */ | 16 */ |
| 17 factory Stopwatch() => new StopwatchImplementation(); | 17 factory Stopwatch() => new _StopwatchImpl(); |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically. | 20 * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically. |
| 21 * If the [Stopwatch] has been stopped, then calling start again restarts it | 21 * If the [Stopwatch] has been stopped, then calling start again restarts it |
| 22 * without resetting the [elapsed] count. | 22 * without resetting the [elapsed] count. |
| 23 * If the [Stopwatch] is currently running, then calling start does nothing. | 23 * If the [Stopwatch] is currently running, then calling start does nothing. |
| 24 */ | 24 */ |
| 25 void start(); | 25 void start(); |
| 26 | 26 |
| 27 /** | 27 /** |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 56 * Returns the [elapsed] counter converted to milliseconds. | 56 * Returns the [elapsed] counter converted to milliseconds. |
| 57 */ | 57 */ |
| 58 int elapsedInMs(); | 58 int elapsedInMs(); |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * Returns the frequency of the elapsed counter in Hz. | 61 * Returns the frequency of the elapsed counter in Hz. |
| 62 */ | 62 */ |
| 63 int frequency(); | 63 int frequency(); |
| 64 | 64 |
| 65 } | 65 } |
| 66 | |
| 67 class _StopwatchImpl implements Stopwatch { | |
| 68 // The _start and _stop fields capture the time when [start] and [stop] | |
| 69 // are called respectively. | |
| 70 // If _start is null, then the [Stopwatch] has not been started yet. | |
| 71 // If _stop is null, then the [Stopwatch] has not been stopped yet, | |
|
floitsch
2012/10/24 11:03:30
has not been started yet, or is running. (NYC)
Anders Johnsen
2012/10/24 12:19:55
No?:
93 void stop() {
94 if (_start ===
Ivan Posva
2012/10/26 02:52:26
Florian, care to explain this?
floitsch
2012/10/26 09:25:15
It was just a different way of saying the same thi
| |
| 72 // or is running. | |
| 73 int _start; | |
| 74 int _stop; | |
| 75 | |
| 76 _StopwatchImpl() : _start = null, _stop = null {} | |
| 77 | |
| 78 void start() { | |
| 79 if (_start === null) { | |
| 80 // This stopwatch has never been started. | |
| 81 _start = _now(); | |
| 82 } else { | |
| 83 if (_stop === null) { | |
| 84 return; | |
| 85 } | |
| 86 // Restarting this stopwatch. Prepend the elapsed time to the current | |
| 87 // start time. | |
| 88 _start = _now() - (_stop - _start); | |
| 89 _stop = null; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void stop() { | |
| 94 if (_start === null || _stop !== null) { | |
| 95 return; | |
| 96 } | |
| 97 _stop = _now(); | |
| 98 } | |
| 99 | |
| 100 void reset() { | |
| 101 if (_start === null) return; | |
| 102 // If [_start] is not null, then the stopwatch had already been started. It | |
| 103 // may running right now. | |
|
Ivan Posva
2012/10/26 02:52:26
// may be running right now.
| |
| 104 _start = _now(); | |
| 105 if (_stop !== null) { | |
| 106 // The watch is not running. So simply set the [_stop] to [_start] thus | |
| 107 // having an elapsed time of 0. | |
| 108 _stop = _start; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 int elapsed() { | |
| 113 if (_start === null) { | |
| 114 return 0; | |
| 115 } | |
| 116 return (_stop === null) ? (_now() - _start) : (_stop - _start); | |
| 117 } | |
| 118 | |
| 119 int elapsedInUs() { | |
| 120 return (elapsed() * 1000000) ~/ frequency(); | |
| 121 } | |
| 122 | |
| 123 int elapsedInMs() { | |
| 124 return (elapsed() * 1000) ~/ frequency(); | |
| 125 } | |
| 126 | |
| 127 int frequency() => _frequency(); | |
| 128 | |
| 129 external static int _frequency(); | |
| 130 external static int _now(); | |
| 131 } | |
| OLD | NEW |