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. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // are called respectively. | 68 // are called respectively. |
69 // If _start is null, then the [Stopwatch] has not been started yet. | 69 // If _start is null, then the [Stopwatch] has not been started yet. |
70 // If _stop is null, then the [Stopwatch] has not been stopped yet, | 70 // If _stop is null, then the [Stopwatch] has not been stopped yet, |
71 // or is running. | 71 // or is running. |
72 int _start; | 72 int _start; |
73 int _stop; | 73 int _stop; |
74 | 74 |
75 _StopwatchImpl() : _start = null, _stop = null {} | 75 _StopwatchImpl() : _start = null, _stop = null {} |
76 | 76 |
77 void start() { | 77 void start() { |
78 if (_start === null) { | 78 if (_start == null) { |
79 // This stopwatch has never been started. | 79 // This stopwatch has never been started. |
80 _start = _now(); | 80 _start = _now(); |
81 } else { | 81 } else { |
82 if (_stop === null) { | 82 if (_stop == null) { |
83 return; | 83 return; |
84 } | 84 } |
85 // Restarting this stopwatch. Prepend the elapsed time to the current | 85 // Restarting this stopwatch. Prepend the elapsed time to the current |
86 // start time. | 86 // start time. |
87 _start = _now() - (_stop - _start); | 87 _start = _now() - (_stop - _start); |
88 _stop = null; | 88 _stop = null; |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 void stop() { | 92 void stop() { |
93 if (_start === null || _stop !== null) { | 93 if (_start == null || _stop != null) { |
94 return; | 94 return; |
95 } | 95 } |
96 _stop = _now(); | 96 _stop = _now(); |
97 } | 97 } |
98 | 98 |
99 void reset() { | 99 void reset() { |
100 if (_start === null) return; | 100 if (_start == null) return; |
101 // If [_start] is not null, then the stopwatch had already been started. It | 101 // If [_start] is not null, then the stopwatch had already been started. It |
102 // may running right now. | 102 // may running right now. |
103 _start = _now(); | 103 _start = _now(); |
104 if (_stop !== null) { | 104 if (_stop != null) { |
105 // The watch is not running. So simply set the [_stop] to [_start] thus | 105 // The watch is not running. So simply set the [_stop] to [_start] thus |
106 // having an elapsed time of 0. | 106 // having an elapsed time of 0. |
107 _stop = _start; | 107 _stop = _start; |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 int get elapsedTicks { | 111 int get elapsedTicks { |
112 if (_start === null) { | 112 if (_start == null) { |
113 return 0; | 113 return 0; |
114 } | 114 } |
115 return (_stop === null) ? (_now() - _start) : (_stop - _start); | 115 return (_stop == null) ? (_now() - _start) : (_stop - _start); |
116 } | 116 } |
117 | 117 |
118 int get elapsedMicroseconds { | 118 int get elapsedMicroseconds { |
119 return (elapsedTicks * 1000000) ~/ frequency; | 119 return (elapsedTicks * 1000000) ~/ frequency; |
120 } | 120 } |
121 | 121 |
122 int get elapsedMilliseconds { | 122 int get elapsedMilliseconds { |
123 return (elapsedTicks * 1000) ~/ frequency; | 123 return (elapsedTicks * 1000) ~/ frequency; |
124 } | 124 } |
125 | 125 |
126 int get frequency => _frequency(); | 126 int get frequency => _frequency(); |
127 | 127 |
128 external static int _frequency(); | 128 external static int _frequency(); |
129 external static int _now(); | 129 external static int _now(); |
130 } | 130 } |
OLD | NEW |