Chromium Code Reviews| Index: sdk/lib/core/stopwatch.dart |
| diff --git a/sdk/lib/core/stopwatch.dart b/sdk/lib/core/stopwatch.dart |
| index e923083a7d174b7d6e611bec9562be51f6579684..8049d99f40c68a9459738520ef81b92d32444140 100644 |
| --- a/sdk/lib/core/stopwatch.dart |
| +++ b/sdk/lib/core/stopwatch.dart |
| @@ -9,31 +9,35 @@ part of dart.core; |
| */ |
| class Stopwatch { |
| /** |
| - * Frequency of the elapsed counter in Hz. |
| + * Cached frequency of the system. Must be initialized in [_initTicker]; |
| */ |
| - int get frequency => _frequency; |
| + static int _frequency; |
|
Lasse Reichstein Nielsen
2016/11/09 13:29:08
Moved field to here, getter below constructor.
floitsch
2016/11/10 16:31:57
Acknowledged.
|
| // 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, |
| - // or is running. |
| - int _start; |
| - int _stop; |
| + // If _stop is null, the stopwatched is running. |
|
floitsch
2016/11/10 16:31:57
stopwatch
|
| + int _start = 0; |
| + int _stop = 0; |
| /** |
| * Creates a [Stopwatch] in stopped state with a zero elapsed count. |
| * |
| * The following example shows how to start a [Stopwatch] |
| * immediately after allocation. |
| - * |
| - * Stopwatch stopwatch = new Stopwatch()..start(); |
| + * ```dart |
|
floitsch
2016/11/10 16:31:57
No need for "dart".
That should be the default in
Lasse Reichstein Nielsen
2016/11/11 09:00:55
Done.
|
| + * Stopwatch stopwatch = new Stopwatch()..start(); |
|
floitsch
2016/11/10 16:31:57
Maybe switch to 'var'.
Lasse Reichstein Nielsen
2016/11/11 09:00:55
Done.
|
| + * ``` |
| */ |
| Stopwatch() { |
| - _initTicker(); |
| + if (_frequency == null) _initTicker(); |
| } |
| /** |
| + * Frequency of the elapsed counter in Hz. |
| + */ |
| + int get frequency => _frequency; |
| + |
| + /** |
| * Starts the [Stopwatch]. |
| * |
| * The [elapsed] count is increasing monotonically. If the [Stopwatch] has |
| @@ -43,14 +47,10 @@ class Stopwatch { |
| * If the [Stopwatch] is currently running, then calling start does nothing. |
| */ |
| void start() { |
| - if (isRunning) return; |
| - if (_start == null) { |
| - // This stopwatch has never been started. |
| - _start = _now(); |
| - } else { |
| - // Restart this stopwatch. Prepend the elapsed time to the current |
| + if (_stop != null) { |
| + // (Re)start this stopwatch. Prepend the elapsed time to the current |
|
floitsch
2016/11/10 16:31:57
Maybe?
(Re)start this stopwatch. Move the start-p
Lasse Reichstein Nielsen
2016/11/11 09:00:55
Reworeded to:
... Don't count the time while the s
|
| // start time. |
| - _start = _now() - (_stop - _start); |
| + _start += _now() - _stop; |
| _stop = null; |
| } |
| } |
| @@ -63,8 +63,7 @@ class Stopwatch { |
| * effect. |
| */ |
| void stop() { |
| - if (!isRunning) return; |
| - _stop = _now(); |
| + _stop ??= _now(); |
|
floitsch
2016/11/10 16:31:57
I found the `isRunning` more telling, but the file
Lasse Reichstein Nielsen
2016/11/11 09:00:55
It is more readable, but it's also calling an over
|
| } |
| /** |
| @@ -73,15 +72,7 @@ class Stopwatch { |
| * This method does not stop or start the [Stopwatch]. |
| */ |
| void reset() { |
| - if (_start == null) return; |
| - // If [_start] is not null, then the stopwatch had already been started. It |
| - // may 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; |
| - } |
| + _start = _stop ?? _now(); |
| } |
| /** |
| @@ -96,10 +87,7 @@ class Stopwatch { |
| * The elapsed number of clock ticks increases by [frequency] every second. |
| */ |
| int get elapsedTicks { |
| - if (_start == null) { |
| - return 0; |
| - } |
| - return (_stop == null) ? (_now() - _start) : (_stop - _start); |
| + return (_stop ?? _now()) - _start; |
| } |
| /** |
| @@ -125,14 +113,9 @@ class Stopwatch { |
| /** |
| - * Returns wether the [StopWatch] is currently running. |
| + * Returns whether the [StopWatch] is currently running. |
|
floitsch
2016/11/10 16:31:57
Remove `Returns`.
Lasse Reichstein Nielsen
2016/11/11 09:00:55
Removed everywhere.
|
| */ |
| - bool get isRunning => _start != null && _stop == null; |
| - |
| - /** |
| - * Cached frequency of the system. Must be initialized in [_initTicker]; |
| - */ |
| - static int _frequency; |
| + bool get isRunning => _stop == null; |
| /** |
| * Initializes the time-measuring system. *Must* initialize the [_frequency] |