Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/silversquirl/benchmark.zig
Tiny Zig package for writing microbenchmarks
https://github.com/silversquirl/benchmark.zig
Last synced: 8 days ago
JSON representation
Tiny Zig package for writing microbenchmarks
- Host: GitHub
- URL: https://github.com/silversquirl/benchmark.zig
- Owner: silversquirl
- License: 0bsd
- Created: 2023-03-14T17:00:14.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-05T22:22:26.000Z (5 months ago)
- Last Synced: 2024-10-15T06:25:01.387Z (21 days ago)
- Language: Zig
- Size: 5.86 KB
- Stars: 16
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `benchmark.zig`
This is a tiny microbenchmark library for Zig, designed to be easy to use and very small.
## Usage
You can either manually copy `benchmark.zig` into your project and reference it with `@import("benchmark.zig")`, or add it as a dependency using the Zig package manager and reference as below.
A basic usage example is shown below:
```zig
const std = @import("std");
const benchmark = @import("benchmark");
pub const main = benchmark.main(.{}, struct {
// Benchmarks are just public functions
pub fn arrayListWriter(b: *benchmark.B) !void {
// Setup is not timed
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();while (b.step()) { // Number of iterations is automatically adjusted for accurate timing
defer _ = arena.reset(.retain_capacity);var a = std.ArrayList(u8).init(arena.allocator());
try a.writer().print("Hello, {s}!", .{"world"});// `use` is a helper that calls `std.mem.doNotOptimizeAway`
b.use(a.items);
}
}pub fn allocPrint(b: *benchmark.B) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
while (b.step()) {
defer _ = arena.reset(.retain_capacity);const result = std.fmt.allocPrint(arena.allocator(), "Hello, {s}!", .{"world"});
b.use(result);
}
}
});
```Running this with `zig build run -Doptimize=ReleaseFast` produces output similar to the following:
```
BENCHMARK ITERATIONS TIME
arrayListWriter 36171528 27ns/op (1s total)
allocPrint 48251012 20ns/op (1s total)
```