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