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 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A simple stopwatch interface to measure elapsed time. | 8 * A simple stopwatch interface to measure elapsed time. |
9 */ | 9 */ |
10 class Stopwatch { | 10 class Stopwatch { |
| 11 /** |
| 12 * Frequency of the elapsed counter in Hz. |
| 13 */ |
| 14 final int frequency = _frequency(); |
| 15 |
11 // The _start and _stop fields capture the time when [start] and [stop] | 16 // The _start and _stop fields capture the time when [start] and [stop] |
12 // are called respectively. | 17 // are called respectively. |
13 // If _start is null, then the [Stopwatch] has not been started yet. | 18 // If _start is null, then the [Stopwatch] has not been started yet. |
14 // If _stop is null, then the [Stopwatch] has not been stopped yet, | 19 // If _stop is null, then the [Stopwatch] has not been stopped yet, |
15 // or is running. | 20 // or is running. |
16 int _start; | 21 int _start; |
17 int _stop; | 22 int _stop; |
18 | 23 |
19 /** | 24 /** |
20 * Creates a [Stopwatch] in stopped state with a zero elapsed count. | 25 * Creates a [Stopwatch] in stopped state with a zero elapsed count. |
21 * | 26 * |
22 * The following example shows how to start a [Stopwatch] | 27 * The following example shows how to start a [Stopwatch] |
23 * immediately after allocation. | 28 * immediately after allocation. |
24 * | 29 * |
25 * Stopwatch stopwatch = new Stopwatch()..start(); | 30 * Stopwatch stopwatch = new Stopwatch()..start(); |
26 */ | 31 */ |
27 Stopwatch() : _start = null, _stop = null {} | 32 Stopwatch(); |
28 | 33 |
29 /** | 34 /** |
30 * Starts the [Stopwatch]. | 35 * Starts the [Stopwatch]. |
31 * | 36 * |
32 * The [elapsed] count is increasing monotonically. If the [Stopwatch] has | 37 * The [elapsed] count is increasing monotonically. If the [Stopwatch] has |
33 * been stopped, then calling start again restarts it without resetting the | 38 * been stopped, then calling start again restarts it without resetting the |
34 * [elapsed] count. | 39 * [elapsed] count. |
35 * | 40 * |
36 * If the [Stopwatch] is currently running, then calling start does nothing. | 41 * If the [Stopwatch] is currently running, then calling start does nothing. |
37 */ | 42 */ |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 return (elapsedTicks * 1000000) ~/ frequency; | 114 return (elapsedTicks * 1000000) ~/ frequency; |
110 } | 115 } |
111 | 116 |
112 /** | 117 /** |
113 * Returns the [elapsedTicks] counter converted to milliseconds. | 118 * Returns the [elapsedTicks] counter converted to milliseconds. |
114 */ | 119 */ |
115 int get elapsedMilliseconds { | 120 int get elapsedMilliseconds { |
116 return (elapsedTicks * 1000) ~/ frequency; | 121 return (elapsedTicks * 1000) ~/ frequency; |
117 } | 122 } |
118 | 123 |
119 /** | |
120 * Returns the frequency of the elapsed counter in Hz. | |
121 */ | |
122 int get frequency => _frequency(); | |
123 | 124 |
124 /** | 125 /** |
125 * Returns wether the [StopWatch] is currently running. | 126 * Returns wether the [StopWatch] is currently running. |
126 */ | 127 */ |
127 bool get isRunning => _start != null && _stop == null; | 128 bool get isRunning => _start != null && _stop == null; |
128 | 129 |
129 external static int _frequency(); | 130 external static int _frequency(); |
130 external static int _now(); | 131 external static int _now(); |
131 } | 132 } |
OLD | NEW |