| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
| 6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
| 7 | 7 |
| 8 library engine.utilities.general; | 8 library engine.utilities.general; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Helper for measuring how much time is spent doing some operation. | 11 * Helper for measuring how much time is spent doing some operation. |
| 12 */ | 12 */ |
| 13 class TimeCounter { | 13 class TimeCounter { |
| 14 Stopwatch _sw = new Stopwatch(); | 14 static TimeCounter _current = null; |
| 15 final Stopwatch _sw = new Stopwatch(); |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * @return the number of milliseconds spent between [start] and [stop]. | 18 * @return the number of milliseconds spent between [start] and [stop]. |
| 18 */ | 19 */ |
| 19 int get result => _sw.elapsedMilliseconds; | 20 int get result => _sw.elapsedMilliseconds; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Starts counting time. | 23 * Starts counting time. |
| 23 * | 24 * |
| 24 * @return the [TimeCounterHandle] that should be used to stop counting. | 25 * @return the [TimeCounterHandle] that should be used to stop counting. |
| 25 */ | 26 */ |
| 26 TimeCounter_TimeCounterHandle start() => new TimeCounter_TimeCounterHandle(thi
s); | 27 TimeCounter_TimeCounterHandle start() { |
| 28 return new TimeCounter_TimeCounterHandle(this); |
| 29 } |
| 27 } | 30 } |
| 28 | 31 |
| 29 /** | 32 /** |
| 30 * The handle object that should be used to stop and update counter. | 33 * The handle object that should be used to stop and update counter. |
| 31 */ | 34 */ |
| 32 class TimeCounter_TimeCounterHandle { | 35 class TimeCounter_TimeCounterHandle { |
| 33 final TimeCounter _counter; | 36 final TimeCounter _counter; |
| 37 TimeCounter _prev; |
| 34 | 38 |
| 35 TimeCounter_TimeCounterHandle(this._counter) { | 39 TimeCounter_TimeCounterHandle(this._counter) { |
| 40 // if there is some counter running, pause it |
| 41 _prev = TimeCounter._current; |
| 42 if (_prev != null) { |
| 43 _prev._sw.stop(); |
| 44 } |
| 45 TimeCounter._current = _counter; |
| 46 // start this counter |
| 36 _counter._sw.start(); | 47 _counter._sw.start(); |
| 37 } | 48 } |
| 38 | 49 |
| 39 /** | 50 /** |
| 40 * Stops counting time and updates counter. | 51 * Stops counting time and updates counter. |
| 41 */ | 52 */ |
| 42 void stop() { | 53 void stop() { |
| 43 _counter._sw.stop(); | 54 _counter._sw.stop(); |
| 55 // restore previous counter and resume it |
| 56 TimeCounter._current = _prev; |
| 57 if (_prev != null) { |
| 58 _prev._sw.start(); |
| 59 } |
| 44 } | 60 } |
| 45 } | 61 } |
| OLD | NEW |