Import std.time.clock
std.time.clock
The monotonic clock, for measuring how long things take, and Sleep.
The monotonic clock, which never goes back or jumps as the OS's clock can, for how long things take: Instant, a reading of it; Stopwatch and Measure over it; and Sleep. The real clock's time is Timestamp's (in timestamp.fb). What reads the clock here, and in timers.fb, also takes an Instant, so code, and its tests, can give the time as a value.
std/time/clock.fb
Types
Instant
struct
Public Struct Instant Implements Comparable, Hashable, Printable, Defaultable
A reading of the monotonic clock: nanoseconds since a moment of its own, which only means something beside the program's other Instants.
Examples
Local start = Instant.FromNanoseconds(1_000)
Local later = start + 2.Seconds
Expect(later - start == 2.Seconds && start < later)
Expect(Instant.Now().Elapsed >= Duration.Zero())-
Now, as the clock counts it.
-
The Instant that many nanoseconds after the clock's start, for code that gives the time.
-
The clock's reading of 0 nanoseconds.
-
How many nanoseconds after the clock's start it is.
-
How long it's been since it, now.
-
baftera. Past what an Int64 of nanoseconds holds, about 292 years, it overflows as an Int64 does. -
bbeforea. Past what an Int64 of nanoseconds holds, about 292 years, it overflows as an Int64 does. -
How long it is from
btoa. Past what an Int64 of nanoseconds holds, about 292 years, it overflows as an Int64 does. -
Whether
ais beforeb. -
How long after the clock's start it is, like "1h2m3.5s".
Stopwatch
class
Public Class Stopwatch
Measures how long goes by while it runs, over as many starts and stops as it has.
Examples
Local start = Instant.FromNanoseconds(0)
Local watch = Stopwatch.StartNew(start)
Expect(watch.ElapsedAt(Instant.FromNanoseconds(1_500_000)) == 1500.Microseconds)
watch.Stop(Instant.FromNanoseconds(2_000_000))
Expect(watch.Elapsed == 2.Milliseconds && !watch.IsRunning)-
A Stopwatch at 0 that isn't running.
-
A Stopwatch running from now, or from the Instant.
-
A Stopwatch running from
now. -
Whether it's running.
-
What it's measured, now or at the Instant.
-
What it's measured at
now. -
Starts it, now or at the Instant, unless it's running.
-
Starts it at
now, unless it's running. -
Stops it, now or at the Instant, keeping what it measured, unless it's stopped.
-
Stops it at
now, keeping what it measured, unless it's stopped. -
Stops it at 0.
-
Starts it again from 0, now or at the Instant.
-
Starts it again from 0, at
now.
Functions
Measure
function
Public Function Measure(work: Func()): Duration
How long the work takes, on the monotonic clock.
Examples
Local nap = Function()
Sleep(2.Milliseconds)
End Function
Expect(Measure(nap) >= 2.Milliseconds)
Sleep
function
Public Function Sleep(time: Duration)
Blocks the thread for that long, and returns at once for a Duration that isn't above 0. In Async code, Await Wait or Timer.After waits without blocking.
Examples
Local before = Instant.Now()
Sleep(1.Milliseconds)
Expect(before.Elapsed >= 1.Milliseconds)Report an issue with this page or with FutureBlitz on GitHub.