Skip to content

Commit

Permalink
std.time.Timer.lap: only read system time once (#4533)
Browse files Browse the repository at this point in the history
Calling Timer.lap queried the system time twice; once to compute the lap
time and once to reset the timer. This can lead to time discrepancies
between actual and computed durations when summing the result of
Timer.lap in a loop. This commit fixes that.

also fix Timer.read to not require a pointer
  • Loading branch information
heidezomp authored Feb 23, 2020
1 parent cfe1fba commit 907c558
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/std/time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,9 @@ pub const Timer = struct {
}

/// Reads the timer value since start or the last reset in nanoseconds
pub fn read(self: *Timer) u64 {
pub fn read(self: Timer) u64 {
var clock = clockNative() - self.start_time;
if (builtin.os == .windows) {
return @divFloor(clock * ns_per_s, self.frequency);
}
if (comptime std.Target.current.isDarwin()) {
return @divFloor(clock * self.frequency.numer, self.frequency.denom);
}
return clock;
return self.nativeDurationToNanos(clock);
}

/// Resets the timer value to 0/now.
Expand All @@ -172,7 +166,7 @@ pub const Timer = struct {
/// Returns the current value of the timer in nanoseconds, then resets it
pub fn lap(self: *Timer) u64 {
var now = clockNative();
var lap_time = self.read();
var lap_time = self.nativeDurationToNanos(now - self.start_time);
self.start_time = now;
return lap_time;
}
Expand All @@ -188,6 +182,16 @@ pub const Timer = struct {
os.clock_gettime(monotonic_clock_id, &ts) catch unreachable;
return @intCast(u64, ts.tv_sec) * @as(u64, ns_per_s) + @intCast(u64, ts.tv_nsec);
}

fn nativeDurationToNanos(self: Timer, duration: u64) u64 {
if (builtin.os == .windows) {
return @divFloor(duration * ns_per_s, self.frequency);
}
if (comptime std.Target.current.isDarwin()) {
return @divFloor(duration * self.frequency.numer, self.frequency.denom);
}
return duration;
}
};

test "sleep" {
Expand Down

0 comments on commit 907c558

Please sign in to comment.