aboutsummaryrefslogtreecommitdiff
path: root/src/bench/string-handling.zig
blob: 23f558eb8a3634f68987cc19b0a92c67d1cde3a0 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
const strings = @import("root").bun.strings;
const std = @import("std");

pub fn main() anyerror!void {
    const args = try std.process.argsAlloc(std.heap.c_allocator);
    const filepath = args[args.len - 3];
    const find = args[args.len - 2];
    const amount = try std.fmt.parseInt(usize, args[args.len - 1], 10);
    var file = try std.fs.cwd().openFile(filepath, .{ .mode = .read_only });
    var contents = try file.readToEndAlloc(std.heap.c_allocator, std.math.maxInt(usize));
    var list = try std.ArrayList(u8).initCapacity(std.heap.c_allocator, contents.len);
    var duped = list.items.ptr[0..contents.len];
    {
        var timer = try std.time.Timer.start();
        var index: usize = std.math.maxInt(usize);
        var j: usize = 0;
        var i: usize = 0;
        while (j < amount) : (j += 1) {
            i = 0;
            strings.copy(duped, contents);
        }

        if (index == std.math.maxInt(usize)) {
            std.debug.print("manual        [{d} byte file] {s} NOT found in {}\n", .{ contents.len, find, std.fmt.fmtDuration(timer.read()) });
        } else {
            std.debug.print("manual        [{d} byte file] {s} found at {d} in {}\n", .{ contents.len, find, index, std.fmt.fmtDuration(timer.read()) });
        }
    }

    {
        var timer = try std.time.Timer.start();
        var index: usize = std.math.maxInt(usize);
        var j: usize = 0;
        var i: usize = 0;
        while (j < amount) : (j += 1) {
            i = 0;
            @memcpy(duped[0..contents.len], contents);
        }

        if (index == std.math.maxInt(usize)) {
            std.debug.print("memcpy      [{d} byte file] {s} NOT found in {}\n", .{ contents.len, find, std.fmt.fmtDuration(timer.read()) });
        } else {
            std.debug.print("memcpy      [{d} byte file] {s} found at {d} in {}\n", .{ contents.len, find, index, std.fmt.fmtDuration(timer.read()) });
        }
    }

    {
        var timer = try std.time.Timer.start();
        var index: usize = std.math.maxInt(usize);
        var j: usize = 0;
        var i: usize = 0;
        while (j < amount) : (j += 1) {
            i = 0;
            list.clearRetainingCapacity();
            list.appendSliceAssumeCapacity(contents);
        }

        if (index == std.math.maxInt(usize)) {
            std.debug.print("ArrayList  [{d} byte file] {s} NOT found in {}\n", .{ contents.len, find, std.fmt.fmtDuration(timer.read()) });
        } else {
            std.debug.print("ArrayList  [{d} byte file] {s} found at {d} in {}\n", .{ contents.len, find, index, std.fmt.fmtDuration(timer.read()) });
        }
    }
}