| ... | @@ -1,17 +1,13 @@ | ... | @@ -1,17 +1,13 @@ |
| 1 | // Modify the HashFunction variable to the one wanted to test. | 1 | // Modify the HashFunction variable to the one wanted to test. |
| 2 | // | 2 | // |
| 3 | // NOTE: The throughput measurement may be slightly lower than other measurements since we run | | |
| 4 | // through our block alignment functions as well. Be aware when comparing against other tests. | | |
| 5 | // | | |
| 6 | // ``` | 3 | // ``` |
| 7 | // zig build-exe --release-fast --library c throughput_test.zig | 4 | // zig build-exe --release-fast throughput_test.zig |
| 8 | // ./throughput_test | 5 | // ./throughput_test |
| 9 | // ``` | 6 | // ``` |
| 10 | | 7 | |
| 11 | const std = @import("std"); | 8 | const std = @import("std"); |
| 12 | const c = @cImport({ | 9 | const time = std.os.time; |
| 13 | @cInclude("time.h"); | 10 | const Timer = time.Timer; |
| 14 | }); | | |
| 15 | const HashFunction = @import("md5.zig").Md5; | 11 | const HashFunction = @import("md5.zig").Md5; |
| 16 | | 12 | |
| 17 | const MiB = 1024 * 1024; | 13 | const MiB = 1024 * 1024; |
| ... | @@ -28,13 +24,14 @@ pub fn main() !void { | ... | @@ -28,13 +24,14 @@ pub fn main() !void { |
| 28 | var h = HashFunction.init(); | 24 | var h = HashFunction.init(); |
| 29 | var offset: usize = 0; | 25 | var offset: usize = 0; |
| 30 | | 26 | |
| 31 | const start = c.clock(); | 27 | var timer = try Timer.start(); |
| | 28 | const start = timer.lap(); |
| 32 | while (offset < BytesToHash) : (offset += block.len) { | 29 | while (offset < BytesToHash) : (offset += block.len) { |
| 33 | h.update(block[0..]); | 30 | h.update(block[0..]); |
| 34 | } | 31 | } |
| 35 | const end = c.clock(); | 32 | const end = timer.read(); |
| 36 | | 33 | |
| 37 | const elapsed_s = f64(end - start) / f64(c.CLOCKS_PER_SEC); | 34 | const elapsed_s = f64(end - start) / time.ns_per_s; |
| 38 | const throughput = u64(BytesToHash / elapsed_s); | 35 | const throughput = u64(BytesToHash / elapsed_s); |
| 39 | | 36 | |
| 40 | try stdout.print("{}: {} MiB/s\n", @typeName(HashFunction), throughput / (1 * MiB)); | 37 | try stdout.print("{}: {} MiB/s\n", @typeName(HashFunction), throughput / (1 * MiB)); |