Skip to content

Commit

Permalink
add std.AutoHashMap benchmark
Browse files Browse the repository at this point in the history
see #2
  • Loading branch information
andrewrk committed Jul 3, 2020
1 parent a76a2d8 commit 15cf093
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions benchmarks/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@
"dir": "arena-allocator",
"mainPath": "main.zig",
"baselinePath": "main.zig"
},
"std-hash-map": {
"description": "std.AutoHashMap - Project Euler 14",
"kind": "zig-bench",
"baseline": "feade9ef0010b1b47d7216e786ed964d09612c2b",
"dir": "std-hash-map",
"mainPath": "main.zig",
"baselinePath": "main.zig"
}
}
2 changes: 2 additions & 0 deletions benchmarks/std-hash-map/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run
run.o
44 changes: 44 additions & 0 deletions benchmarks/std-hash-map/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const std = @import("std");
const bench = @import("root");

pub fn setup(gpa: *std.mem.Allocator, options: *bench.Options) !void { }

pub fn run(gpa: *std.mem.Allocator, context: void) !void {
var cache = std.AutoHashMap(u64, u64).init(gpa);
defer cache.deinit();

try cache.ensureCapacity(2000000);

var x: u64 = 0;
var maxx: u64 = 0;
var maxl: u64 = 0;
while (x < 1000000) : (x += 1) {
const l = length(&cache, x);
if (l > maxl) {
maxl = l;
maxx = x;
}
}
if (maxx != 837799) @panic("bad maxx");
if (maxl != 524) @panic("bad maxl");
}

fn step(x: u64) u64 {
if (x & 1 > 0) {
return 3 * x + 1;
} else {
return x / 2;
}
}

fn length(cache: *std.AutoHashMap(u64, u64), x: u64) u64 {
if (x <= 1) return 0;
if (cache.getValue(x)) |e| {
return e;
} else {
const next = step(x);
const len = 1 + length(cache, next);
cache.putAssumeCapacityNoClobber(x, len);
return len;
}
}

0 comments on commit 15cf093

Please # to comment.