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.

benchmarkPwhash

benchmark.benchmarkPwhash
fn benchmarkPwhash(
    allocator: mem.Allocator,
    comptime ty: anytype,
    comptime params: *const anyopaque,
    comptime count: comptime_int,
    io: std.Io,
) !f64

File

lib/std/crypto/benchmark.zig:440

Code

fn benchmarkPwhash(
    allocator: mem.Allocator,
    comptime ty: anytype,
    comptime params: *const anyopaque,
    comptime count: comptime_int,
    io: std.Io,
) !f64 {
    const password = "testpasstestpass";
    const opts = ty.HashOptions{
        .allocator = allocator,
        .params = @as(*const ty.Params, @ptrCast(@alignCast(params))).*,
        .encoding = .phc,
    };
    var buf: [256]u8 = undefined;

    const strHash = ty.strHash;
    const strHashFnInfo = @typeInfo(@TypeOf(strHash)).@"fn";
    const needs_io = strHashFnInfo.params.len == 4 and strHashFnInfo.params[3].type == std.Io;
    const needs_salt = strHashFnInfo.params.len == 4 and strHashFnInfo.params[3].type != std.Io;
    const salt: [16]u8 = @splat(0);

    const start = benchTime(io);
    {
        var i: usize = 0;
        while (i < count) : (i += 1) {
            if (needs_io) {
                _ = try strHash(password, opts, &buf, io);
            } else if (needs_salt) {
                _ = try strHash(password, opts, &buf, &salt);
            } else {
                _ = try strHash(password, opts, &buf);
            }
            mem.doNotOptimizeAway(&buf);
        }
    }
    const end = benchTime(io);

    const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s;
    const throughput = elapsed_s / count;

    return throughput;
}