Bug 18398 – std.datetime.stopwatch documented examples could be better
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-08T09:54:40Z
Last change time
2018-05-31T08:16:59Z
Assigned to
No Owner
Creator
Nathan S.
Comments
Comment #0 by n8sh.secondary — 2018-02-08T09:54:40Z
Someone using `std.datetime.stopwatch` probably wants to use it to obtain a time in milliseconds or some other unit of time but none of the example code on the page involves converting a core.time.Duration. This makes the examples less helpful to a newcomer than they could be.
Proposed addition:
```d
/// Measure a time in milliseconds, microseconds, or nanoseconds
@safe nothrow @nogc unittest
{
auto sw = StopWatch(AutoStart.no);
sw.start();
// ... Insert operations to be timed here ...
sw.stop();
long msecs = sw.peek().total!"msecs";
long usecs = sw.peek().total!"usecs";
long nsecs = sw.peek().total!"nsecs";
assert(usecs >= msecs * 1000);
assert(nsecs >= usecs * 1000);
}
```
Comment #1 by n8sh.secondary — 2018-02-08T10:00:29Z