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

Fix formatting durations with fractions with leading zeroes #59

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions util/format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ const std = @import("std");
const Color = @import("./color.zig").Color;
const log = std.log.scoped(.zbench_format);

pub fn duration(buffer: []u8, d: u64) ![]u8 {
pub fn duration(buffer: []u8, nanoseconds: u64) ![]u8 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, just using the stdlib's duration formatting is a good option too IMHO:

pub fn duration(buffer: []u8, ns: u64) ![]u8 {
    return try std.fmt.bufPrint(buffer, "{}", .{
        std.fmt.fmtDuration(ns),
    });
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Leveraging the std lib can reduce complexity and potential errors in this custom implementation.

const units = [_][]const u8{ "ns", "µs", "ms", "s" };

var scaledDuration: u64 = d;
var scaled: f64 = @floatFromInt(nanoseconds);
var unitIndex: usize = 0;

var fractionalPart: u64 = 0;

while (scaledDuration >= 1_000 and unitIndex < units.len - 1) {
fractionalPart = scaledDuration % 1_000;
scaledDuration /= 1_000;
while (scaled >= 1000.0 and unitIndex < units.len - 1) {
scaled /= 1000.0;
unitIndex += 1;
}

const formatted = try std.fmt.bufPrint(buffer, "{d}.{d}{s}", .{ scaledDuration, fractionalPart, units[unitIndex] });

return formatted;
return if (0 < unitIndex) try std.fmt.bufPrint(buffer, "{d:.3}{s}", .{
scaled,
units[unitIndex],
}) else try std.fmt.bufPrint(buffer, "{d}ns", .{nanoseconds});
}

pub fn memorySize(bytes: u64, allocator: std.mem.Allocator) ![]const u8 {
Expand Down
25 changes: 20 additions & 5 deletions util/format_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,42 @@ test "duration" {

{
const result = try format.duration(buffer[0..], 1);
try std.testing.expectEqualSlices(u8, "1.0ns", result);
try std.testing.expectEqualSlices(u8, "1ns", result);
}

{
const result = try format.duration(buffer[0..], 999);
try std.testing.expectEqualSlices(u8, "999.0ns", result);
try std.testing.expectEqualSlices(u8, "999ns", result);
}

{
const result = try format.duration(buffer[0..], 1000);
try std.testing.expectEqualSlices(u8, "1.0µs", result);
try std.testing.expectEqualSlices(u8, "1.000µs", result);
}

{
const result = try format.duration(buffer[0..], 1000000);
try std.testing.expectEqualSlices(u8, "1.0ms", result);
try std.testing.expectEqualSlices(u8, "1.000ms", result);
}

{
const result = try format.duration(buffer[0..], 1000000000);
try std.testing.expectEqualSlices(u8, "1.0s", result);
try std.testing.expectEqualSlices(u8, "1.000s", result);
}

{
const result = try format.duration(buffer[0..], 1234567890);
try std.testing.expectEqualSlices(u8, "1.235s", result);
}

{
const result = try format.duration(buffer[0..], 1023456789);
try std.testing.expectEqualSlices(u8, "1.023s", result);
}

{
const result = try format.duration(buffer[0..], 1999999999);
try std.testing.expectEqualSlices(u8, "2.000s", result);
}

{
Expand Down
Loading