-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathulid_benchmark.zig
61 lines (55 loc) · 2.04 KB
/
ulid_benchmark.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const std = @import("std");
const ulid = @import("ulid");
pub fn main() !void {
var factory = ulid.Factory{};
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const num = 1_000_000;
var ulids = try allocator.alloc(ulid.Ulid, num);
{
var timer = try std.time.Timer.start();
for (0..num) |i| {
ulids[i] = try factory.next();
}
const dur = timer.read();
std.debug.print("ids/s={d:.2}\n", .{@as(f64, @floatFromInt(num)) * std.time.ns_per_s / @as(f64, @floatFromInt(dur))});
}
var strs = try allocator.alloc([ulid.text_length]u8, num);
{
var timer = try std.time.Timer.start();
timer.reset();
for (0..num) |i| {
ulids[i].encodeBuf(&strs[i]) catch unreachable;
}
const dur = timer.read();
std.debug.print("encodes/s={d:.2}\n", .{@as(f64, @floatFromInt(num)) * std.time.ns_per_s / @as(f64, @floatFromInt(dur))});
}
var bytes = try allocator.alloc([ulid.binary_length]u8, num);
{
var timer = try std.time.Timer.start();
timer.reset();
for (0..num) |i| {
bytes[i] = ulids[i].bytes();
}
const dur = timer.read();
std.debug.print("binencodes/s={d:.2}\n", .{@as(f64, @floatFromInt(num)) * std.time.ns_per_s / @as(f64, @floatFromInt(dur))});
}
{
var timer = try std.time.Timer.start();
timer.reset();
for (0..num) |i| {
ulids[i] = try ulid.Ulid.decode(&strs[i]);
}
const dur = timer.read();
std.debug.print("decodes/s={d:.2}\n", .{@as(f64, @floatFromInt(num)) * std.time.ns_per_s / @as(f64, @floatFromInt(dur))});
}
{
var timer = try std.time.Timer.start();
timer.reset();
for (0..num) |i| {
ulids[i] = try ulid.Ulid.fromBytes(&bytes[i]);
}
const dur = timer.read();
std.debug.print("bindecodes/s={d:.2}\n", .{@as(f64, @floatFromInt(num)) * std.time.ns_per_s / @as(f64, @floatFromInt(dur))});
}
}