OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 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. |
| 7 |
| 8 library engine.utilities.general; |
| 9 |
| 10 /** |
| 11 * Helper for measuring how much time is spent doing some operation. |
| 12 */ |
| 13 class TimeCounter { |
| 14 static final int NANOS_PER_MILLI = 1000 * 1000; |
| 15 static final int NANOS_PER_MICRO = 1000; |
| 16 static TimeCounter _current = null; |
| 17 final Stopwatch _sw = new Stopwatch(); |
| 18 |
| 19 /** |
| 20 * @return the number of milliseconds spent between [start] and [stop]. |
| 21 */ |
| 22 int get result => _sw.elapsedMilliseconds; |
| 23 |
| 24 /** |
| 25 * Starts counting time. |
| 26 * |
| 27 * @return the [TimeCounterHandle] that should be used to stop counting. |
| 28 */ |
| 29 TimeCounter_TimeCounterHandle start() { |
| 30 return new TimeCounter_TimeCounterHandle(this); |
| 31 } |
| 32 } |
| 33 |
| 34 /** |
| 35 * The handle object that should be used to stop and update counter. |
| 36 */ |
| 37 class TimeCounter_TimeCounterHandle { |
| 38 final TimeCounter _counter; |
| 39 int _startMicros; |
| 40 TimeCounter _prev; |
| 41 |
| 42 TimeCounter_TimeCounterHandle(this._counter) { |
| 43 // if there is some counter running, pause it |
| 44 _prev = TimeCounter._current; |
| 45 if (_prev != null) { |
| 46 _prev._sw.stop(); |
| 47 } |
| 48 TimeCounter._current = _counter; |
| 49 // start this counter |
| 50 _startMicros = _counter._sw.elapsedMicroseconds; |
| 51 _counter._sw.start(); |
| 52 } |
| 53 |
| 54 /** |
| 55 * Stops counting time and updates counter. |
| 56 */ |
| 57 int stop() { |
| 58 _counter._sw.stop(); |
| 59 int elapsed = (_counter._sw.elapsedMicroseconds - _startMicros) * |
| 60 TimeCounter.NANOS_PER_MICRO; |
| 61 // restore previous counter and resume it |
| 62 TimeCounter._current = _prev; |
| 63 if (_prev != null) { |
| 64 _prev._sw.start(); |
| 65 } |
| 66 // done |
| 67 return elapsed; |
| 68 } |
| 69 } |
OLD | NEW |