about summary refs log tree commit diff
path: root/src/benchmark.zig
blob: 4c3060949568810dcb36b394a0875b0f95970859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const std = @import("std");
const Random = std.Random;
const time = std.time;
const bubble = @import("sort/bubble.zig");
const selection = @import("sort/selection.zig");
const insertion = @import("sort/insertion.zig");
const testing = std.testing;

fn benchmark(comptime T: type, comptime sort_fn: fn (type, []T) void, n: usize, runs: usize) !u64 {
    var prng = std.rand.DefaultPrng.init(0);
    var random = prng.random();
    var total_time: u64 = 0;
    for (0..runs) |_| {
        const list = try testing.allocator.alloc(T, n);
        defer testing.allocator.free(list);
        for (list) |*item| {
            item.* = random.int(T);
        }
        const start = time.milliTimestamp();
        sort_fn(T, list);
        const end = time.milliTimestamp();
        total_time += @intCast(end - start);
    }
    return total_time / runs;
}

test "bubble sort benchmark" {
    const runs = 10;
    const sizes = [_]usize{ 100, 1000, 10000 };
    std.debug.print("\nBubble Sort Benchmark:\n", .{});
    for (sizes) |size| {
        const avg_time = try benchmark(i64, bubble.sort, size, runs);
        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
    }
}

test "selection sort benchmark" {
    const runs = 10;
    const sizes = [_]usize{ 100, 1000, 10000 };
    std.debug.print("\nSelection Sort Benchmark:\n", .{});
    for (sizes) |size| {
        const avg_time = try benchmark(i64, selection.sort, size, runs);
        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
    }
}

test "insertion sort benchmark" {
    const runs = 10;
    const sizes = [_]usize{ 100, 1000, 10000 };
    std.debug.print("\nInsertion Sort Benchmark:\n", .{});
    for (sizes) |size| {
        const avg_time = try benchmark(i64, insertion.sort, size, runs);
        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
    }
}