Created
June 28, 2026 01:25
-
-
Save Lightnet/0f583f99028a54d323f9eb4a4d8349d1 to your computer and use it in GitHub Desktop.
sample zig 0.16.0 time build test for build.zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| pub fn build(b: *std.Build) void { | |
| std.debug.print("build script running...\n", .{}); | |
| const target = b.standardTargetOptions(.{}); | |
| const optimize = b.standardOptimizeOption(.{}); | |
| const exe = b.addExecutable(.{ | |
| .name = "ztimestartend", | |
| .root_module = b.createModule(.{ | |
| .root_source_file = b.path("src/main.zig"), | |
| .target = target, | |
| .optimize = optimize, | |
| }), | |
| }); | |
| const shared_state = b.allocator.create(TimerState) catch @panic("OOM"); | |
| shared_state.* = .{ .start_ns = 0 }; | |
| const start_timer = TimerStep.create(b, "start_timer", true, shared_state); | |
| const end_timer = TimerStep.create(b, "end_timer", false, shared_state); | |
| exe.step.dependOn(&start_timer.step); | |
| end_timer.step.dependOn(&exe.step); | |
| b.installArtifact(exe); | |
| b.getInstallStep().dependOn(&end_timer.step); | |
| const run_cmd = b.addRunArtifact(exe); | |
| run_cmd.step.dependOn(b.getInstallStep()); | |
| const run_step = b.step("run", "Run the app"); | |
| run_step.dependOn(&run_cmd.step); | |
| } | |
| const TimerState = struct { | |
| // FIX: Change field type to i96 to natively match the output of now.toNanoseconds() | |
| start_ns: i96, | |
| }; | |
| const TimerStep = struct { | |
| step: std.Build.Step, | |
| is_start: bool, | |
| state: *TimerState, | |
| pub fn create(b: *std.Build, name: []const u8, is_start: bool, state: *TimerState) *TimerStep { | |
| const self = b.allocator.create(TimerStep) catch @panic("OOM"); | |
| self.* = .{ | |
| .step = std.Build.Step.init(.{ | |
| .id = .custom, | |
| .name = b.dupe(name), | |
| .owner = b, | |
| .makeFn = TimerStep.make, | |
| }), | |
| .is_start = is_start, | |
| .state = state, | |
| }; | |
| return self; | |
| } | |
| fn make(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void { | |
| _ = options; | |
| const self: *TimerStep = @fieldParentPtr("step", step); | |
| var threaded = std.Io.Threaded.global_single_threaded; | |
| const io = threaded.io(); | |
| const now = std.Io.Clock.awake.now(io); | |
| const current_ns = now.toNanoseconds(); | |
| if (self.is_start) { | |
| self.state.start_ns = current_ns; | |
| std.debug.print(">>> BUILD START\n", .{}); | |
| } else { | |
| const start_ns = self.state.start_ns; | |
| if (start_ns != 0) { | |
| const elapsed_ns = current_ns - start_ns; | |
| // Safely coerce the i96 delta to a float for division | |
| const elapsed_ms = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000.0; | |
| std.debug.print(">>> BUILD END\n", .{}); | |
| std.debug.print(">>> TOTAL ELAPSED TIME: {d:.2} ms ({d:.3} s)\n", .{ elapsed_ms, elapsed_ms / 1000.0 }); | |
| } else { | |
| std.debug.print(">>> BUILD END (Start time missing)\n", .{}); | |
| } | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment