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 implementation of the [StopWatch] interface. | 6 * A simple implementation of the [Stopwatch] interface. |
| 7 */ | 7 */ |
| 8 class StopWatchImplementation implements StopWatch { | 8 class StopwatchImplementation implements Stopwatch { |
| 9 // The _start and _stop fields capture the time when [start] and [stop] | 9 // The _start and _stop fields capture the time when [start] and [stop] |
| 10 // are called respectively. | 10 // are called respectively. |
| 11 // If _start is null, then the [StopWatch] has not been started yet. | 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. | 12 // If _stop is null, then the [Stopwatch] has not been stopped yet. |
| 13 int _start; | 13 int _start; |
| 14 int _stop; | 14 int _stop; |
| 15 | 15 |
| 16 StopWatchImplementation() : _start = null, _stop = null {} | 16 StopwatchImplementation() : _start = null, _stop = null {} |
| 17 StopwatchImplementation.start() : _start = null, _stop = null { | |
| 18 start(); | |
| 19 } | |
| 17 | 20 |
| 18 void start() { | 21 void start() { |
| 19 if (_start === null) { | 22 if (_start === null) { |
| 20 // This stopwatch has never been started. | 23 // This stopwatch has never been started. |
| 21 _start = Clock.now(); | 24 _start = Clock.now(); |
| 22 } else { | 25 } else { |
| 23 if (_stop === null) { | 26 if (_stop === null) { |
| 24 return; | 27 return; |
| 25 } | 28 } |
| 26 // Restarting this stopwatch. Prepend the elapsed time to the current | 29 // Restarting this stopwatch. Prepend the elapsed time to the current |
| 27 // start time. | 30 // start time. |
| 28 _start = Clock.now() - (_stop - _start); | 31 _start = Clock.now() - (_stop - _start); |
| 29 } | 32 } |
| 30 } | 33 } |
| 31 | 34 |
| 32 void stop() { | 35 void stop() { |
| 33 if (_start === null) { | 36 if (_start === null) { |
| 34 return; | 37 return; |
| 35 } | 38 } |
| 36 _stop = Clock.now(); | 39 _stop = Clock.now(); |
| 37 } | 40 } |
| 38 | 41 |
| 42 void reset() { | |
| 43 if (_start === null) return; | |
| 44 _start = Clock.now(); | |
|
Ivan Posva
2011/11/11 16:31:54
A comment why it is OK to not set _start to null w
floitsch
2011/11/14 13:55:59
Done.
| |
| 45 if (_stop !== null) { | |
| 46 _stop = _start; | |
| 47 } | |
| 48 } | |
| 49 | |
| 39 int elapsed() { | 50 int elapsed() { |
| 40 if (_start === null) { | 51 if (_start === null) { |
| 41 return 0; | 52 return 0; |
| 42 } | 53 } |
| 43 return (_stop === null) ? (Clock.now() - _start) : (_stop - _start); | 54 return (_stop === null) ? (Clock.now() - _start) : (_stop - _start); |
| 44 } | 55 } |
| 45 | 56 |
| 46 int elapsedInUs() { | 57 int elapsedInUs() { |
| 47 return (elapsed() * 1000000) ~/ frequency(); | 58 return (elapsed() * 1000000) ~/ frequency(); |
| 48 } | 59 } |
| 49 | 60 |
| 50 int elapsedInMs() { | 61 int elapsedInMs() { |
| 51 return (elapsed() * 1000) ~/ frequency(); | 62 return (elapsed() * 1000) ~/ frequency(); |
| 52 } | 63 } |
| 53 | 64 |
| 54 int frequency() { | 65 int frequency() { |
| 55 return Clock.frequency(); | 66 return Clock.frequency(); |
| 56 } | 67 } |
| 57 | 68 |
| 58 } | 69 } |
| OLD | NEW |