From 4e12cd56ea5fda05a7be90c46faf0746caa2ef4e Mon Sep 17 00:00:00 2001 From: "floitsch@google.com" Date: Fri, 8 Feb 2013 10:46:14 +0000 Subject: [PATCH] Remove Stopwatch interface and make it a class. Review URL: https://codereview.chromium.org//12210065 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@18242 260f80e4-7a28-3924-810f-c04153c831b5 --- runtime/lib/stopwatch_patch.dart | 2 +- .../implementation/lib/core_patch.dart | 2 +- sdk/lib/core/stopwatch.dart | 110 +++++++----------- 3 files changed, 47 insertions(+), 67 deletions(-) diff --git a/runtime/lib/stopwatch_patch.dart b/runtime/lib/stopwatch_patch.dart index 19645d1f620..2bdc2c531b0 100644 --- a/runtime/lib/stopwatch_patch.dart +++ b/runtime/lib/stopwatch_patch.dart @@ -4,7 +4,7 @@ // A VM patch of the stopwatch part of dart:core. -patch class _StopwatchImpl { +patch class Stopwatch { // Returns the current clock tick. /* patch */ static int _now() native "Stopwatch_now"; diff --git a/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart b/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart index 8d68c8d4986..09e31da11d0 100644 --- a/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart +++ b/sdk/lib/_internal/compiler/implementation/lib/core_patch.dart @@ -158,7 +158,7 @@ patch class DateTime { // Patch for Stopwatch implementation. -patch class _StopwatchImpl { +patch class Stopwatch { patch static int _frequency() => 1000000; patch static int _now() => Primitives.numMicroseconds(); } diff --git a/sdk/lib/core/stopwatch.dart b/sdk/lib/core/stopwatch.dart index b446069f70c..55648014c42 100644 --- a/sdk/lib/core/stopwatch.dart +++ b/sdk/lib/core/stopwatch.dart @@ -7,70 +7,7 @@ part of dart.core; /** * A simple [Stopwatch] interface to measure elapsed time. */ -abstract class Stopwatch { - /** - * Creates a [Stopwatch] in stopped state with a zero elapsed count. - * - * The following example shows how to start a [Stopwatch] - * right after allocation. - * - * Stopwatch stopwatch = new Stopwatch()..start(); - */ - factory Stopwatch() => new _StopwatchImpl(); - - /** - * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically. - * If the [Stopwatch] has been stopped, then calling start again restarts it - * without resetting the [elapsed] count. - * If the [Stopwatch] is currently running, then calling start does nothing. - */ - void start(); - - /** - * Stops the [Stopwatch]. The [elapsed] count stops increasing. - * If the [Stopwatch] is currently not running, then calling stop does - * nothing. - */ - void stop(); - - /** - * Resets the [elapsed] count to zero. This method does not stop or start - * the [Stopwatch]. - */ - void reset(); - - /** - * Returns the elapsed number of clock ticks since calling [start] while the - * [Stopwatch] is running. - * Returns the elapsed number of clock ticks between calling [start] and - * calling [stop]. - * Returns 0 if the [Stopwatch] has never been started. - * The elapsed number of clock ticks increases by [frequency] every second. - */ - int get elapsedTicks; - - /** - * Returns the [elapsedTicks] counter converted to microseconds. - */ - int get elapsedMicroseconds; - - /** - * Returns the [elapsedTicks] counter converted to milliseconds. - */ - int get elapsedMilliseconds; - - /** - * Returns the frequency of the elapsed counter in Hz. - */ - int get frequency; - - /** - * Returns wether the [StopWatch] is currently running. - */ - bool get isRunning; -} - -class _StopwatchImpl implements Stopwatch { +class Stopwatch { // The _start and _stop fields capture the time when [start] and [stop] // are called respectively. // If _start is null, then the [Stopwatch] has not been started yet. @@ -79,8 +16,22 @@ class _StopwatchImpl implements Stopwatch { int _start; int _stop; - _StopwatchImpl() : _start = null, _stop = null {} + /** + * Creates a [Stopwatch] in stopped state with a zero elapsed count. + * + * The following example shows how to start a [Stopwatch] + * right after allocation. + * + * Stopwatch stopwatch = new Stopwatch()..start(); + */ + Stopwatch() : _start = null, _stop = null {} + /** + * Starts the [Stopwatch]. The [elapsed] count is increasing monotonically. + * If the [Stopwatch] has been stopped, then calling start again restarts it + * without resetting the [elapsed] count. + * If the [Stopwatch] is currently running, then calling start does nothing. + */ void start() { if (isRunning) return; if (_start == null) { @@ -94,11 +45,20 @@ class _StopwatchImpl implements Stopwatch { } } + /** + * Stops the [Stopwatch]. The [elapsed] count stops increasing. + * If the [Stopwatch] is currently not running, then calling stop does + * nothing. + */ void stop() { if (!isRunning) return; _stop = _now(); } + /** + * Resets the [elapsed] count to zero. This method does not stop or start + * the [Stopwatch]. + */ void reset() { if (_start == null) return; // If [_start] is not null, then the stopwatch had already been started. It @@ -111,6 +71,14 @@ class _StopwatchImpl implements Stopwatch { } } + /** + * Returns the elapsed number of clock ticks since calling [start] while the + * [Stopwatch] is running. + * Returns the elapsed number of clock ticks between calling [start] and + * calling [stop]. + * Returns 0 if the [Stopwatch] has never been started. + * The elapsed number of clock ticks increases by [frequency] every second. + */ int get elapsedTicks { if (_start == null) { return 0; @@ -118,16 +86,28 @@ class _StopwatchImpl implements Stopwatch { return (_stop == null) ? (_now() - _start) : (_stop - _start); } + /** + * Returns the [elapsedTicks] counter converted to microseconds. + */ int get elapsedMicroseconds { return (elapsedTicks * 1000000) ~/ frequency; } + /** + * Returns the [elapsedTicks] counter converted to milliseconds. + */ int get elapsedMilliseconds { return (elapsedTicks * 1000) ~/ frequency; } + /** + * Returns the frequency of the elapsed counter in Hz. + */ int get frequency => _frequency(); + /** + * Returns wether the [StopWatch] is currently running. + */ bool get isRunning => _start != null && _stop == null; external static int _frequency();