Chromium Code Reviews| Index: lib/core/stopwatch.dart |
| diff --git a/lib/core/stopwatch.dart b/lib/core/stopwatch.dart |
| index e43f52d4bac7ced5274815a6b56b7d3910e81456..62bba16243d0c03462b17bc8647a180530dad476 100644 |
| --- a/lib/core/stopwatch.dart |
| +++ b/lib/core/stopwatch.dart |
| @@ -14,7 +14,7 @@ abstract class Stopwatch { |
| * |
| * Stopwatch stopwatch = new Stopwatch()..start(); |
| */ |
| - factory Stopwatch() => new StopwatchImplementation(); |
| + factory Stopwatch() => new _StopwatchImpl(); |
| /** |
| * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically. |
| @@ -63,3 +63,69 @@ abstract class Stopwatch { |
| int frequency(); |
| } |
| + |
| +class _StopwatchImpl implements Stopwatch { |
| + // The _start and _stop fields capture the time when [start] and [stop] |
| + // are called respectively. |
| + // If _start is null, then the [Stopwatch] has not been started yet. |
| + // 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
|
| + // or is running. |
| + int _start; |
| + int _stop; |
| + |
| + _StopwatchImpl() : _start = null, _stop = null {} |
| + |
| + void start() { |
| + if (_start === null) { |
| + // This stopwatch has never been started. |
| + _start = _now(); |
| + } else { |
| + if (_stop === null) { |
| + return; |
| + } |
| + // Restarting this stopwatch. Prepend the elapsed time to the current |
| + // start time. |
| + _start = _now() - (_stop - _start); |
| + _stop = null; |
| + } |
| + } |
| + |
| + void stop() { |
| + if (_start === null || _stop !== null) { |
| + return; |
| + } |
| + _stop = _now(); |
| + } |
| + |
| + void reset() { |
| + if (_start === null) return; |
| + // If [_start] is not null, then the stopwatch had already been started. It |
| + // may running right now. |
|
Ivan Posva
2012/10/26 02:52:26
// may be running right now.
|
| + _start = _now(); |
| + if (_stop !== null) { |
| + // The watch is not running. So simply set the [_stop] to [_start] thus |
| + // having an elapsed time of 0. |
| + _stop = _start; |
| + } |
| + } |
| + |
| + int elapsed() { |
| + if (_start === null) { |
| + return 0; |
| + } |
| + return (_stop === null) ? (_now() - _start) : (_stop - _start); |
| + } |
| + |
| + int elapsedInUs() { |
| + return (elapsed() * 1000000) ~/ frequency(); |
| + } |
| + |
| + int elapsedInMs() { |
| + return (elapsed() * 1000) ~/ frequency(); |
| + } |
| + |
| + int frequency() => _frequency(); |
| + |
| + external static int _frequency(); |
| + external static int _now(); |
| +} |