-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bench): add bench step and first bench
- Loading branch information
1 parent
3da1509
commit 080b065
Showing
2 changed files
with
84 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//! Benchmarks for HyperZig. | ||
|
||
const std = @import("std"); | ||
const HyperZig = @import("hyperzig.zig").HyperZig; | ||
|
||
const Allocator = std.mem.Allocator; | ||
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator; | ||
const Timer = std.time.Timer; | ||
const comptimePrint = std.fmt.comptimePrint; | ||
const fmtDuration = std.fmt.fmtDuration; | ||
const getStdOut = std.io.getStdOut; | ||
const Writer = std.fs.File.Writer; | ||
|
||
const Hyperedge = struct { weight: usize = 1 }; | ||
const Vertex = struct {}; | ||
|
||
const Bench = struct { | ||
const Self = @This(); | ||
|
||
graph: HyperZig(Hyperedge, Vertex), | ||
start: u64, | ||
stdout: Writer, | ||
timer: Timer, | ||
|
||
pub fn init(allocator: Allocator, stdout: Writer, comptime name: []const u8) !Self { | ||
const graph = try HyperZig( | ||
Hyperedge, | ||
Vertex, | ||
).init(allocator, .{}); | ||
const msg = comptime comptimePrint("{s}...\n", .{name}); | ||
try stdout.print(msg, .{}); | ||
var timer = try Timer.start(); | ||
|
||
return .{ .timer = timer, .start = timer.lap(), .stdout = stdout, .graph = graph }; | ||
} | ||
|
||
pub fn deinit(self: *Self) !void { | ||
try self.stdout.print("Total duration: {s}\n", .{fmtDuration(self.timer.read() - self.start)}); | ||
self.graph.deinit(); | ||
} | ||
}; | ||
|
||
pub fn main() !void { | ||
var gpa = GeneralPurposeAllocator(.{}){}; | ||
const allocator = gpa.allocator(); | ||
const stdout = getStdOut().writer(); | ||
|
||
{ | ||
var bench = try Bench.init(allocator, stdout, "generate 1_000 hyperedges with 1_000 vertices each"); | ||
for (0..1_000) |_| { | ||
const h = try bench.graph.createHyperedge(.{}); | ||
|
||
for (0..1_000) |_| { | ||
const v = try bench.graph.createVertex(.{}); | ||
try bench.graph.appendVertexToHyperedge(h, v); | ||
} | ||
} | ||
try bench.deinit(); | ||
} | ||
} |