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

Side by Side Diff: corelib/src/implementation/stopwatch_implementation.dart

Issue 10890030: Move core and coreimpl from corelib/ to lib/core and lib/coreimpl. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase. Created 8 years, 3 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 | « corelib/src/implementation/splay_tree.dart ('k') | corelib/src/implementation/string.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * A simple implementation of the [Stopwatch] interface.
7 */
8 class StopwatchImplementation implements Stopwatch {
9 // The _start and _stop fields capture the time when [start] and [stop]
10 // are called respectively.
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,
13 // or is running.
14 int _start;
15 int _stop;
16
17 StopwatchImplementation() : _start = null, _stop = null {}
18 StopwatchImplementation.start() : _start = null, _stop = null {
19 start();
20 }
21
22 void start() {
23 if (_start === null) {
24 // This stopwatch has never been started.
25 _start = _now();
26 } else {
27 if (_stop === null) {
28 return;
29 }
30 // Restarting this stopwatch. Prepend the elapsed time to the current
31 // start time.
32 _start = _now() - (_stop - _start);
33 _stop = null;
34 }
35 }
36
37 void stop() {
38 if (_start === null || _stop !== null) {
39 return;
40 }
41 _stop = _now();
42 }
43
44 void reset() {
45 if (_start === null) return;
46 // If [_start] is not null, then the stopwatch had already been started. It
47 // may running right now.
48 _start = _now();
49 if (_stop !== null) {
50 // The watch is not running. So simply set the [_stop] to [_start] thus
51 // having an elapsed time of 0.
52 _stop = _start;
53 }
54 }
55
56 int elapsed() {
57 if (_start === null) {
58 return 0;
59 }
60 return (_stop === null) ? (_now() - _start) : (_stop - _start);
61 }
62
63 int elapsedInUs() {
64 return (elapsed() * 1000000) ~/ frequency();
65 }
66
67 int elapsedInMs() {
68 return (elapsed() * 1000) ~/ frequency();
69 }
70
71 int frequency() => _frequency();
72
73 external static int _frequency();
74 external static int _now();
75 }
OLDNEW
« no previous file with comments | « corelib/src/implementation/splay_tree.dart ('k') | corelib/src/implementation/string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698