about summary refs log tree commit diff
path: root/src/benchmark.zig
diff options
context:
space:
mode:
authoriurii plugatarov <[email protected]>2024-07-29 22:51:24 +0200
committeriurii plugatarov <[email protected]>2024-07-29 22:51:24 +0200
commitfeebd54e3a6546a7c8b400a31d07fe8042899c39 (patch)
tree3f614d184778f5d7030cdbba4dfb3ad2d20af758 /src/benchmark.zig
parente53093139c07804b99ac05689ebc6695b47ff919 (diff)
downloaddsazitty-feebd54e3a6546a7c8b400a31d07fe8042899c39.tar.gz
quck
Diffstat (limited to '')
-rw-r--r--src/benchmark.zig31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/benchmark.zig b/src/benchmark.zig
index 6781b5a..b264b1c 100644
--- a/src/benchmark.zig
+++ b/src/benchmark.zig
@@ -6,6 +6,7 @@ const selection = @import("sort/selection.zig");
 const insertion = @import("sort/insertion.zig");
 const shell = @import("sort/shell.zig");
 const merge = @import("sort/merge.zig");
+const quick = @import("sort/quick.zig");
 const testing = std.testing;
 const Allocator = std.mem.Allocator;
 
@@ -47,6 +48,25 @@ fn benchmark_merge(comptime T: type, n: usize, runs: usize) !u64 {
     return total_time / runs;
 }
 
+fn benchmark_quick(comptime T: type, n: usize, runs: usize) !u64 {
+    var prng = Random.DefaultPrng.init(0);
+    var random = prng.random();
+    var total_time: u64 = 0;
+    var allocator = testing.allocator;
+    for (0..runs) |_| {
+        const list = try allocator.alloc(T, n);
+        defer allocator.free(list);
+        for (list) |*item| {
+            item.* = random.int(T);
+        }
+        const start = time.milliTimestamp();
+        quick.sort(T, list, 0, list.len - 1);
+        const end = time.milliTimestamp();
+        total_time += @intCast(end - start);
+    }
+    return total_time / runs;
+}
+
 fn run_bench(comptime T: type, comptime sort_fn: fn (type, []T) void, name: []const u8) !void {
     const runs = 10;
     const sizes = [_]usize{ 100, 1000, 10000 };
@@ -57,6 +77,16 @@ fn run_bench(comptime T: type, comptime sort_fn: fn (type, []T) void, name: []co
     }
 }
 
+fn run_bench_quick(comptime T: type, name: []const u8) !void {
+    const runs = 10;
+    const sizes = [_]usize{ 100, 1000, 10000 };
+    std.debug.print("\n{s} Benchmark:\n", .{name});
+    for (sizes) |size| {
+        const avg_time = try benchmark_quick(T, size, runs);
+        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
+    }
+}
+
 fn run_bench_merge(comptime T: type, name: []const u8) !void {
     const runs = 10;
     const sizes = [_]usize{ 100, 1000, 10000 };
@@ -73,4 +103,5 @@ test "sorting benchmarks" {
     try run_bench(i64, insertion.sort, "Insertion Sort");
     try run_bench(i64, shell.sort, "Shell Sort");
     try run_bench_merge(i64, "Merge Sort");
+    try run_bench_quick(i64, "Quick Sort");
 }