Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

benchmarkHash

benchmark.benchmarkHash
pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Allocator, io: Io) !Result

File

lib/std/hash/benchmark.zig:117

Code

pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Allocator, io: Io) !Result {
    var blocks = try allocator.alloc(u8, bytes);
    defer allocator.free(blocks);
    random.bytes(blocks);

    const block_count = bytes / block_size;

    var h: H.ty = blk: {
        if (H.init_u8s) |init| {
            break :blk .init(init[0..H.ty.key_length]);
        }
        if (H.init_u64) |init| {
            break :blk .init(init);
        }
        if (H.init_default) {
            break :blk .{};
        }
        break :blk .init();
    };

    const start = benchTime(io);
    for (0..block_count) |i| {
        h.update(blocks[i * block_size ..][0..block_size]);
    }
    const final = if (H.has_struct_api) |field_name|
        @field(h, field_name)
    else if (H.has_crypto_api)
        @as(u64, @truncate(h.finalInt()))
    else
        h.final();
    std.mem.doNotOptimizeAway(final);

    const elapsed_ns = benchTime(io) - start;

    const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s;
    const size_float: f64 = @floatFromInt(block_size * block_count);
    const throughput: u64 = @intFromFloat(size_float / elapsed_s);

    return Result{
        .hash = final,
        .throughput = throughput,
    };
}