Zig: Compile-Time & Runtime
Zig is a language that gives you precise control over when computations happen at compile-time or at runtime. This is central to how Zig operates and allows developers to write highly efficient & flexible programs.
Too formal lol time to learn this cool thing so get cozy and relaxed!!
What is Compile-Time known?
In Zig, compile-time known referes to values or computations that the compiler can determine while compiling/building your program. These values are fixed and embedded directly into program’s binary, offering significant performance benefits because they don’t need to be calculated again during runtime. Sounds cool? Ofc it does.
Some characteristics of compile-time known involves:
- Resolved During Compilation: Result is available before the program starts running
- Immutable: Value cannot change once compiled
- Efficient: No runtime computation is required which reduces runtime overhead
- Error Detection: You can catch logical errors at compile-time improving program correctness.
Constants (const
), Functions explicitly marked with comptime
keyword, & type information used in generic programming are compiled-time known in Zig.
wdym “Talk is Cheap, Show me the Kode”? Okay fine.
You can run your code @ codeapi[dot]org/zig
const std = @import("std");
const expect = std.testing.expect;
fn square(comptime x: i32) i32 { // define parameter with `comptime`
return x * x;
}
test "comptime square calculation" {
const compileTimeValue: i32 = 10;
const result = comptime square(compileTimeValue); // evaluate at compile time
try expect(result == 100); // result should be 100
if (std.debug.runtime_safety) {
std.debug.print("Compile-time square of {} is {}\n", .{compileTimeValue, result});
}
}
explanation:
-
square
function is declared withcomptime
for its parameter to make sure that the argument is known at compile-time and calculationx * x
happens during compilation when called with acomptime
value. -
test checks if
square
function correctly computes the square of 10 at compile-time.comptime square
call guarantees the computaiton happens during compilation. result?100
is validated asexpect
ed ofc
output:
1/1 main.test.comptime square calculation... Compile-time square of 10 is 100
OK
All 1 tests passed.
So that’s it. That’s what comptime
is. NEXT
What is Runtime Known?
Runtime known refers to values determined when program is actively running. These values are dynamic and often depend on external factors like user input or system state.
This way the value can change very time the program runs and runtime known is essential for handling input-dependent or environment-specific operations.
Runtime is flexible but costly as it requires CPU cycles during execution resulting in runtime overhead.
const std = @import("std");
const expect = std.testing.expect;
fn square(x: i32) i32 {
return x * x;
}
test "runtime square calculation" {
const runtimeValue: i32 = 10;
const result = square(runtimeValue); // evaluated at runtime
try expect(result == 100); // result should be 100
std.debug.print("Runtime square of {} is {}\n", .{runtimeValue, result});
}
explanation:
SAME AS BEFORE JUST WE’RE NOT USING comptime
YOU DUMMY
output:
1/1 main.test.runtime square calculation... Runtime square of 10 is 100
OK
All 1 tests passed.
Feature | Compile-Time | Runtime |
---|---|---|
When Computed | During the compilation process | During the execution of the program |
Input Requirements | Must be a constant or known at compile-time comptime | Can be any value, including user input |
Performance | No runtime overhead; computation is pre-done | Computation adds to runtime overhead |
Flexibility | Limited to constant inputs | Can handle dynamic inputs during program execution |
I hope most of the stuff here was self explanatory.
We use Compile-Time comptime
when inputs are constants known beforehand. To optimize performance by removing redundant runtime calculations. For build-time logic checks or validations.
We use Runtime when inputs are dynamic and not known until program runs and for scenarios requirning user input or external data.
and that’s it that’s all I know about compile-time and runtime. I’ll share more as i’ll explore more.
References: