Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StopWatch improvements #1881

Closed
DartBot opened this issue Feb 27, 2012 · 12 comments
Closed

StopWatch improvements #1881

DartBot opened this issue Feb 27, 2012 · 12 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue

Comments

@DartBot
Copy link

DartBot commented Feb 27, 2012

This issue was originally filed by @seaneagan


interface StopWatch {

  const Stopwatch(); // change to "const" constructor
  // remove Stopwatch.start(), since issue #552 provides this convenience in a much more orthogonal way

  // use getters instead of methods for elapsed times
  int get ticks(); // more descriptive name
  int get hertz(); // replaces "frequency" with self-documenting name
  int get milliseconds(); // avoids abbreviations consistent with Date, Duration
  int get microseconds(); // ditto

  void start();
  void stop();
  void reset();

  // "lap" (or "split") functionality for timing loop iterations
  void lap(); // starts a new lap
  List<int> get laps();
  List<int> get millisecondLaps();
  List<int> get microsecondLaps();
}

@iposva-google
Copy link
Contributor

Thanks for your suggestions. I'll take a look at them in more detail, still here are some first reactions:

const Stopwatch(); // change to "const" constructor
This is impossible as a const object could not be stopped, restarted or reset. Actually a const object is a compile time constant and thus it being a Stopwatch would be a bit pointless.

use getters instead of methods for elapsed time
I can definitely sympathize with using getters instead of methods. As far as the naming of the getters goes, to me the frequency is self-documenting: frequencies are measured in hertz. The others are probably fine to be renamed, I'll check the naming of similar things in the core libraries.

"lap" (or "split") functionality for timing loop iterations
The biggest problem with this adding a lap time feature is the fact that the Stopwatch ends up not being a very cheap interface any longer, for every lap you might need to do allocation to grow the backing array and suddenly the mere fact of measuring can disturb measurements.


Set owner to @iposva-google.
Added Area-Library, Accepted labels.

@DartBot
Copy link
Author

DartBot commented Feb 28, 2012

This comment was originally written by @seaneagan


A couple things to add:

Should it just rely on the new Clock.frequency instead of having its own ?

Regarding lap/split functionality, the StopWatch could be stopped during array allocation. Could be implemented as something like:

class StopWatchImpl implements StopWatch {

  //...

  lap() {
    // record lap time / stop StopWatch
    int lapTime = _currentLap();
    // add lapTime to backing array
    _laps.add(lapTime);
    // cache the total elapsed time for all laps
    _total += lapTime;
    // start StopWatch for next lap
    _lapStart = Clock.now; // restart timing
  }
  int get ticks() => _total + _currentLap()
  List get laps() {
    if(_laps == null) _laps = new _LapList(this);
    return _laps;
  }

  _LapList _laps;
  int _lapStart;
  int _total;
  _currentLap() => Clock.now - _lapStart;
}

class _LapList implements List<int> {

  _LapList(this._stopWatch) => _internal = new List();

  List<int> _internal;
  StopWatch _stopWatch

  int last() => _stopWatch._currentLap();
  int get length() _internal.length + 1;
  //... etc.

}

@DartBot
Copy link
Author

DartBot commented Feb 28, 2012

This comment was originally written by @seaneagan


Also, a Duration getter:

Duration get elapsed();

could replace the millisecond and microsecond getters (and add a bunch more for longer running StopWatches):

stopWatch.elapsed.inMicroseconds; // would need to be added to Duration
stopWatch.elapsed.inMilliseconds;
stopWatch.elapsed.inSeconds;
stopWatch.elapsed.inMinutes;
// etc.

@DartBot
Copy link
Author

DartBot commented Feb 28, 2012

This comment was originally written by @seaneagan


Could even add the clock ticks getter to Duration:

// tick frequency is same as [Clock.frequency]
int get inTicks();

Then to get the elapsed ticks, just do:

stopWatch.elapsed.inTicks

@DartBot
Copy link
Author

DartBot commented Feb 28, 2012

This comment was originally written by @seaneagan


So in summary, it might look something like:

interface Stopwatch {
  void start();
  void stop();
  void reset();
  Duration get elapsed();

  void split();
  List<Duration> get splits();
}

@DartBot
Copy link
Author

DartBot commented May 9, 2012

This comment was originally written by @mdakin


I propose adding a deltaInMs() method which would return the time between now and last call of delta (or start)

Rationale:

For benchmarking I am interested in finding deltas between measurements. The pattern is usually something like this:

Stopwatch w = new Stopwatch();
w.start();
<Operation 1>
print("Operation 1: ${w.elapsedInMs()} ms.");
w.reset();
<Operation 2>
print("Contains: ${w.elapsedInMs()} ms.");

This approach has a problem that I lose elapsed time from beginning. and I have to call reset() before making incremental measurements.

so Instead, we could have:

Stopwatch w = new Stopwatch();
w.start();
<Operation 1>
print("Operation 1: ${w.deltaInMs()} ms.");
<Operation 2>
print("operation 2: ${w.deltaInMs()} ms.");
...
print("Total elapsed time: ${w.deltaInMs()} ms.");

The only additional burden is an extra integer in the class.

@DartBot
Copy link
Author

DartBot commented May 10, 2012

This comment was originally written by @mdakin


Sorry, there is an error in my example (last line should have used elapsedInMS():

Current Stopwatch implementation:

Stopwatch w = new Stopwatch();
w.start();
<Operation 1>
print("Operation 1: ${w.elapsedInMs()} ms.");
w.reset();
<Operation 2>
print("operation 2: ${w.elapsedInMs()} ms.");

with deltaInMs():

Stopwatch w = new Stopwatch();
w.start();
<Operation 1>
print("Operation 1: ${w.deltaInMs()} ms.");
<Operation 2>
print("operation 2: ${w.deltaInMs()} ms.");
...
print("Total elapsed time: ${w.elapsedInMs()} ms.");

@DartBot
Copy link
Author

DartBot commented May 10, 2012

This comment was originally written by @seaneagan


@7: Your "deltaInMs" is basically the same as my "split" above, except you are returning the value rather than storing it in a List. Method names should be verbs, so I think "split" is better than "deltaInMs":

int split();

I also would prefer using a Duration:

Duration split();

and I still would see value in storing the splits:

List<Duration> get splits();

but those are separate issues.

@DartBot
Copy link
Author

DartBot commented May 10, 2012

This comment was originally written by @mdakin


I think keeping the class as simple as possible is a good idea, I asked for deltaInMs() because it is convenient and name matches elapsedInMs().

split() doesn't sound good for this use case: What are we splitting?
print("operation 1 took: ${w.split()} ms.");

@DartBot
Copy link
Author

DartBot commented May 10, 2012

This comment was originally written by @seaneagan


Physical stopwatches can record split (aka lap) times, see http://en.wikipedia.org/wiki/Stopwatch. A more explicit name might be better, such as "recordSplit" or "getSplit". I don't think abbreviations are generally a good idea, so I think any "InMs" suffix would really need to be "InMilliseconds" which is too long, and anyway unnecessary, especially if using Durations instead of ints.

@sethladd
Copy link
Contributor

sethladd commented Sep 3, 2012

cc @lrhn.

@lrhn
Copy link
Member

lrhn commented Aug 22, 2013

We have no current plan to add more improvements to StopWatch.
The getters are now getters, but laps/splits will have to be handled externally.
It should be fairly simple to wrap a StopWatch with something handling splits, I'd prefer to keep the base class simple.


Added NotPlanned label.

@DartBot DartBot added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue labels Aug 22, 2013
copybara-service bot pushed a commit that referenced this issue Jan 17, 2023
…, webdev

Revisions updated by `dart tools/rev_sdk_deps.dart`.

dartdoc (https://github.com/dart-lang/dartdoc/compare/c4ab682..a99abd4):
  a99abd4b  2023-01-16  dependabot[bot]  Bump dart-lang/setup-dart from 1.3 to 1.4 (#3305)
  a692eeaa  2023-01-16  dependabot[bot]  Bump github/codeql-action from 2.1.37 to 2.1.38 (#3304)
  a43a6c2b  2023-01-14  Parker Lougheed  Remove search code debug prints (#3300)
  cf28572a  2023-01-14  Parker Lougheed  Remove obsolete doc_packages tool (#3301)
  fcbadcd7  2023-01-14  Parker Lougheed  Remove null safety badge (#3295)
  263ea617  2023-01-14  Parker Lougheed  Use spread syntax instead of add (#3296)
  820b5ba5  2023-01-14  Parker Lougheed  Use equal signs to set default parameter values (#3298)
  31e9c797  2023-01-14  Parker Lougheed  Fix build by removing test opting out of null safety (#3297)
  d4495c2c  2023-01-11  dependabot[bot]  Bump actions/checkout from 3.2.0 to 3.3.0 (#3292)
  3ae8eef5  2023-01-11  dependabot[bot]  Bump actions/upload-artifact from 3.1.1 to 3.1.2 (#3291)
  8a9e4691  2023-01-11  dependabot[bot]  Bump actions/cache from 3.2.2 to 3.2.3 (#3290)

http (https://github.com/dart-lang/http/compare/d434d42..c955c7e):
  c955c7e  2023-01-13  Brian Quinlan  Add consistent implementations for `close`. (#851)

intl (https://github.com/dart-lang/intl/compare/c61fdd1..6140b60):
  6140b60  2023-01-12  Googler  Internal change

mime (https://github.com/dart-lang/mime/compare/273d454..034471a):
  034471a  2023-01-11  Kevin Moore  Prepare to release v1.0.4 (#80)

string_scanner (https://github.com/dart-lang/string_scanner/compare/c58618d..0454980):
  0454980  2023-01-17  Kevin Moore  dependabot: monthly is plenty (#54)

sync_http (https://github.com/dart-lang/sync_http/compare/8622614..36a1bd0):
  36a1bd0  2023-01-11  Kevin Moore  Bump min SDK, enable and fix new lints (#34)

test (https://github.com/dart-lang/test/compare/932a652..43fd928):
  43fd9284  2023-01-17  Jacob MacDonald  delete some old integration test helper files that were opted out (#1850)
  2c59fb6c  2023-01-17  Kevin Moore  Run no response daily (#1849)
  8ea50552  2023-01-12  joshualitt  Update wasm integration test to use generated JS runtime for Dart2Wasm. (#1844)
  9a23b72a  2023-01-11  Nate Bosch  Prepare to publish (#1843)
  d887825a  2023-01-11  Derek Xu  Update vm_service constraints to >=6.0.0 <11.0.0 (#1842)

webdev (https://github.com/dart-lang/webdev/compare/094ee97..f978b90):
  f978b90  2023-01-13  Elliott Brooks (she/her)  [MV3] Debug session persists across closing and opening Chrome DevTools (#1894)
  b1b4eff  2023-01-13  Anna Gringauze  Prepare for dart 3.0 alpha changes: generate assets (#1887)
  969f41f  2023-01-13  Elliott Brooks (she/her)  Save encoded URI for ACX DevTools (#1890)
  8384a11  2023-01-13  Elliott Brooks (she/her)  Skip flaky test on windows (#1893)
  8224045  2023-01-13  Elliott Brooks (she/her)  Ignore `illegal_language_version_override` for non null-safe fixtures (#1891)
  e42a030  2023-01-13  Elliott Brooks (she/her)  Re-enable most test cases in `devtools_test` (#1881)
  e134e5b  2023-01-11  Elliott Brooks (she/her)  [MV3] Dart debug extension supports DWDS versions < `17.0.0` (#1882)
  ed80c94  2023-01-11  Elliott Brooks (she/her)  [MV3] Prepare extension for release (#1886)
  be616cd  2023-01-10  Anna Gringauze  Return error from expression evaluation if the evaluator is closed. (#1884)
  18b3277  2023-01-10  Elliott Brooks (she/her)  [MV3] Fix late initialization error on debugger detach (#1879)
  03d4035  2023-01-10  Elliott Brooks (she/her)  Check if storage object exists before trying to read properties(#1883)
  ae55fec  2023-01-10  Elliott Brooks (she/her)  Format manifest.json (#1885)
  3743293  2023-01-10  Anna Gringauze  Fix race condition on simultaneous hot restarts (#1870)

Change-Id: I2bddd015f1e054eb9e24afb247f9c470257560a9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279073
Auto-Submit: Devon Carew <devoncarew@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue
Projects
None yet
Development

No branches or pull requests

4 participants