Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: sdk/lib/core/stopwatch.dart

Issue 12210065: Remove Stopwatch interface and make it a class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/core_patch.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 abstract class Stopwatch { 10 class Stopwatch {
11 // The _start and _stop fields capture the time when [start] and [stop]
12 // are called respectively.
13 // 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,
15 // or is running.
16 int _start;
17 int _stop;
18
11 /** 19 /**
12 * Creates a [Stopwatch] in stopped state with a zero elapsed count. 20 * Creates a [Stopwatch] in stopped state with a zero elapsed count.
13 * 21 *
14 * The following example shows how to start a [Stopwatch] 22 * The following example shows how to start a [Stopwatch]
15 * right after allocation. 23 * right after allocation.
16 * 24 *
17 * Stopwatch stopwatch = new Stopwatch()..start(); 25 * Stopwatch stopwatch = new Stopwatch()..start();
18 */ 26 */
19 factory Stopwatch() => new _StopwatchImpl(); 27 Stopwatch() : _start = null, _stop = null {}
20 28
21 /** 29 /**
22 * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically. 30 * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically.
23 * If the [Stopwatch] has been stopped, then calling start again restarts it 31 * If the [Stopwatch] has been stopped, then calling start again restarts it
24 * without resetting the [elapsed] count. 32 * without resetting the [elapsed] count.
25 * If the [Stopwatch] is currently running, then calling start does nothing. 33 * If the [Stopwatch] is currently running, then calling start does nothing.
26 */ 34 */
27 void start();
28
29 /**
30 * Stops the [Stopwatch]. The [elapsed] count stops increasing.
31 * If the [Stopwatch] is currently not running, then calling stop does
32 * nothing.
33 */
34 void stop();
35
36 /**
37 * Resets the [elapsed] count to zero. This method does not stop or start
38 * the [Stopwatch].
39 */
40 void reset();
41
42 /**
43 * Returns the elapsed number of clock ticks since calling [start] while the
44 * [Stopwatch] is running.
45 * Returns the elapsed number of clock ticks between calling [start] and
46 * calling [stop].
47 * Returns 0 if the [Stopwatch] has never been started.
48 * The elapsed number of clock ticks increases by [frequency] every second.
49 */
50 int get elapsedTicks;
51
52 /**
53 * Returns the [elapsedTicks] counter converted to microseconds.
54 */
55 int get elapsedMicroseconds;
56
57 /**
58 * Returns the [elapsedTicks] counter converted to milliseconds.
59 */
60 int get elapsedMilliseconds;
61
62 /**
63 * Returns the frequency of the elapsed counter in Hz.
64 */
65 int get frequency;
66
67 /**
68 * Returns wether the [StopWatch] is currently running.
69 */
70 bool get isRunning;
71 }
72
73 class _StopwatchImpl implements Stopwatch {
74 // The _start and _stop fields capture the time when [start] and [stop]
75 // are called respectively.
76 // If _start is null, then the [Stopwatch] has not been started yet.
77 // If _stop is null, then the [Stopwatch] has not been stopped yet,
78 // or is running.
79 int _start;
80 int _stop;
81
82 _StopwatchImpl() : _start = null, _stop = null {}
83
84 void start() { 35 void start() {
85 if (isRunning) return; 36 if (isRunning) return;
86 if (_start == null) { 37 if (_start == null) {
87 // This stopwatch has never been started. 38 // This stopwatch has never been started.
88 _start = _now(); 39 _start = _now();
89 } else { 40 } else {
90 // Restart this stopwatch. Prepend the elapsed time to the current 41 // Restart this stopwatch. Prepend the elapsed time to the current
91 // start time. 42 // start time.
92 _start = _now() - (_stop - _start); 43 _start = _now() - (_stop - _start);
93 _stop = null; 44 _stop = null;
94 } 45 }
95 } 46 }
96 47
48 /**
49 * Stops the [Stopwatch]. The [elapsed] count stops increasing.
50 * If the [Stopwatch] is currently not running, then calling stop does
51 * nothing.
52 */
97 void stop() { 53 void stop() {
98 if (!isRunning) return; 54 if (!isRunning) return;
99 _stop = _now(); 55 _stop = _now();
100 } 56 }
101 57
58 /**
59 * Resets the [elapsed] count to zero. This method does not stop or start
60 * the [Stopwatch].
61 */
102 void reset() { 62 void reset() {
103 if (_start == null) return; 63 if (_start == null) return;
104 // If [_start] is not null, then the stopwatch had already been started. It 64 // If [_start] is not null, then the stopwatch had already been started. It
105 // may running right now. 65 // may running right now.
106 _start = _now(); 66 _start = _now();
107 if (_stop != null) { 67 if (_stop != null) {
108 // The watch is not running. So simply set the [_stop] to [_start] thus 68 // The watch is not running. So simply set the [_stop] to [_start] thus
109 // having an elapsed time of 0. 69 // having an elapsed time of 0.
110 _stop = _start; 70 _stop = _start;
111 } 71 }
112 } 72 }
113 73
74 /**
75 * Returns the elapsed number of clock ticks since calling [start] while the
76 * [Stopwatch] is running.
77 * Returns the elapsed number of clock ticks between calling [start] and
78 * calling [stop].
79 * Returns 0 if the [Stopwatch] has never been started.
80 * The elapsed number of clock ticks increases by [frequency] every second.
81 */
114 int get elapsedTicks { 82 int get elapsedTicks {
115 if (_start == null) { 83 if (_start == null) {
116 return 0; 84 return 0;
117 } 85 }
118 return (_stop == null) ? (_now() - _start) : (_stop - _start); 86 return (_stop == null) ? (_now() - _start) : (_stop - _start);
119 } 87 }
120 88
89 /**
90 * Returns the [elapsedTicks] counter converted to microseconds.
91 */
121 int get elapsedMicroseconds { 92 int get elapsedMicroseconds {
122 return (elapsedTicks * 1000000) ~/ frequency; 93 return (elapsedTicks * 1000000) ~/ frequency;
123 } 94 }
124 95
96 /**
97 * Returns the [elapsedTicks] counter converted to milliseconds.
98 */
125 int get elapsedMilliseconds { 99 int get elapsedMilliseconds {
126 return (elapsedTicks * 1000) ~/ frequency; 100 return (elapsedTicks * 1000) ~/ frequency;
127 } 101 }
128 102
103 /**
104 * Returns the frequency of the elapsed counter in Hz.
105 */
129 int get frequency => _frequency(); 106 int get frequency => _frequency();
130 107
108 /**
109 * Returns wether the [StopWatch] is currently running.
110 */
131 bool get isRunning => _start != null && _stop == null; 111 bool get isRunning => _start != null && _stop == null;
132 112
133 external static int _frequency(); 113 external static int _frequency();
134 external static int _now(); 114 external static int _now();
135 } 115 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/core_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698