I have been benchmarking this program
const std = @import("std");
const boolGenerate = @import("bool_generate.zig").boolGenerate;
const ourRng = @import("utils.zig").ourRng;
pub fn main() !void {
const allocator = std.heap.page_allocator;
var prng: std.rand.DefaultPrng = try ourRng();
const rndGen = prng.random();
var argsIterator = try std.process.argsWithAllocator(allocator);
defer argsIterator.deinit();
_ = argsIterator.next(); // First argument is the program name
const stringLenArg = argsIterator.next() orelse "512";
const stringLength = try std.fmt.parseInt(u16, stringLenArg, 10);
const numStrings = 40000;
const output = try boolGenerate(allocator, rndGen, stringLength, numStrings);
std.debug.print("Generated {} strings of length {}\n", .{ numStrings, stringLength });
defer {
for (output) |str| allocator.free(str);
allocator.free(output);
}
const stdout = std.io.getStdOut().writer();
try stdout.print("{} \n", .{output.len});
}
Don’t include the whole thing, essentially boolGenerate
uses the random generator to flip a random number of bits in the chromosome of length stringLength
. The thing is that I have noticed a 20% performance hit all across the lengths that I had been measuring.
A previous version of this code used std.random.crypto so I thought the change in performance was due to that; but actually when I changed to std.posix.getRandom as opposed to std.rand.getRandom I got a huge performance penalty too.
The main bottleneck should be the random number generator here. Maybe the default RNG has changed, and this one is better, but slower?