Add performance info to output, HumanTime util
This commit is contained in:
parent
3ac598100c
commit
2d18112a9a
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
zig-cache
|
zig-cache
|
||||||
zig-out
|
zig-out
|
||||||
|
main
|
||||||
|
main.o
|
||||||
|
22
src/main.zig
22
src/main.zig
@ -1,16 +1,29 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const day01 = @import("day01.zig");
|
const day01 = @import("day01.zig");
|
||||||
|
const utils = @import("utils.zig");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
const stdout_file = std.io.getStdOut().writer();
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var bw = std.io.bufferedWriter(stdout_file);
|
||||||
const stdout = bw.writer();
|
const stdout = bw.writer();
|
||||||
|
var timer = std.time.Timer.start() catch @panic("need timer to benchmark solutions");
|
||||||
|
|
||||||
try stdout.print("Day 1\nPart 1: {}\nPart 2: {}\n\n", .{
|
{
|
||||||
try day01.solve_part1(day01.input),
|
try stdout.print("Day 1\n", .{});
|
||||||
try day01.solve_part2(day01.input),
|
|
||||||
});
|
const part1_start = timer.read();
|
||||||
|
var part1 = try day01.solve_part1(day01.input);
|
||||||
|
const part1_end = timer.read();
|
||||||
|
const part1_time = utils.HumanTime.new(part1_end - part1_start);
|
||||||
|
try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
|
||||||
|
|
||||||
|
const part2_start = timer.read();
|
||||||
|
var part2 = try day01.solve_part2(day01.input);
|
||||||
|
const part2_end = timer.read();
|
||||||
|
const part2_time = utils.HumanTime.new(part2_end - part2_start);
|
||||||
|
try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
|
||||||
|
}
|
||||||
|
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
}
|
}
|
||||||
@ -19,4 +32,5 @@ test {
|
|||||||
std.testing.refAllDecls(@This());
|
std.testing.refAllDecls(@This());
|
||||||
|
|
||||||
_ = day01;
|
_ = day01;
|
||||||
|
_ = utils;
|
||||||
}
|
}
|
||||||
|
@ -8,3 +8,71 @@ pub fn read_file(allocator: std.mem.Allocator, filename: []const u8) ![]const u8
|
|||||||
|
|
||||||
return try input_file.readToEndAlloc(allocator, MAX_BYTES);
|
return try input_file.readToEndAlloc(allocator, MAX_BYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const TimeUnit = enum {
|
||||||
|
nanoseconds,
|
||||||
|
microseconds,
|
||||||
|
milliseconds,
|
||||||
|
seconds,
|
||||||
|
minutes,
|
||||||
|
hours,
|
||||||
|
pub fn abbr(self: TimeUnit) []const u8 {
|
||||||
|
return switch (self) {
|
||||||
|
TimeUnit.nanoseconds => "ns",
|
||||||
|
TimeUnit.microseconds => "μs",
|
||||||
|
TimeUnit.milliseconds => "ms",
|
||||||
|
TimeUnit.seconds => "s",
|
||||||
|
TimeUnit.minutes => "m",
|
||||||
|
TimeUnit.hours => "h",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const HumanTime = struct {
|
||||||
|
value: f64,
|
||||||
|
unit: TimeUnit,
|
||||||
|
pub fn new(nanoseconds: u64) HumanTime {
|
||||||
|
@setFloatMode(.Optimized);
|
||||||
|
const time: f64 = @intToFloat(f64, nanoseconds);
|
||||||
|
if (time > 1_000_000_000 * 60 * 60) {
|
||||||
|
return HumanTime{
|
||||||
|
.value = time / (1_000_000_000 * 60 * 60),
|
||||||
|
.unit = TimeUnit.hours,
|
||||||
|
};
|
||||||
|
} else if (time > 1_000_000_000 * 60) {
|
||||||
|
return HumanTime{
|
||||||
|
.value = time / (1_000_000_000 * 60),
|
||||||
|
.unit = TimeUnit.minutes,
|
||||||
|
};
|
||||||
|
} else if (time > 1_000_000_000) {
|
||||||
|
return HumanTime{
|
||||||
|
.value = time / 1_000_000_000,
|
||||||
|
.unit = TimeUnit.seconds,
|
||||||
|
};
|
||||||
|
} else if (time > 1_000_000) {
|
||||||
|
return HumanTime{
|
||||||
|
.value = time / 1_000_000,
|
||||||
|
.unit = TimeUnit.milliseconds,
|
||||||
|
};
|
||||||
|
} else if (time > 1_000) {
|
||||||
|
return HumanTime{
|
||||||
|
.value = time / 1000,
|
||||||
|
.unit = TimeUnit.microseconds,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return HumanTime{
|
||||||
|
.value = time,
|
||||||
|
.unit = TimeUnit.nanoseconds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "HumanTime.new" {
|
||||||
|
try std.testing.expectEqual(HumanTime.new(100), HumanTime{ .value = 100, .unit = TimeUnit.nanoseconds });
|
||||||
|
try std.testing.expectEqual(HumanTime.new(2_000), HumanTime{ .value = 2, .unit = TimeUnit.microseconds });
|
||||||
|
try std.testing.expectEqual(HumanTime.new(2_000_000), HumanTime{ .value = 2, .unit = TimeUnit.milliseconds });
|
||||||
|
try std.testing.expectEqual(HumanTime.new(2_000_000_000), HumanTime{ .value = 2, .unit = TimeUnit.seconds });
|
||||||
|
try std.testing.expectEqual(HumanTime.new(2_000_000_000 * 60), HumanTime{ .value = 2, .unit = TimeUnit.minutes });
|
||||||
|
try std.testing.expectEqual(HumanTime.new(2_000_000_000 * 60 * 60), HumanTime{ .value = 2, .unit = TimeUnit.hours });
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user