|
3 | 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed.
|
4 | 4 | // The MIT license requires this copyright notice to be included in all copies
|
5 | 5 | // and substantial portions of the software.
|
6 |
| -const builtin = @import("builtin"); |
7 | 6 | const std = @import("std");
|
| 7 | +const builtin = std.builtin; |
| 8 | +const time = std.time; |
| 9 | +const unicode = std.unicode; |
| 10 | + |
| 11 | +const Timer = time.Timer; |
| 12 | + |
| 13 | +const N = 1_000_000; |
| 14 | + |
| 15 | +const KiB = 1024; |
| 16 | +const MiB = 1024 * KiB; |
| 17 | +const GiB = 1024 * MiB; |
| 18 | + |
| 19 | +const ResultCount = struct { |
| 20 | + count: usize, |
| 21 | + throughput: u64, |
| 22 | +}; |
| 23 | + |
| 24 | +fn benchmarkCodepointCount(buf: []const u8) !ResultCount { |
| 25 | + var timer = try Timer.start(); |
| 26 | + |
| 27 | + const bytes = N * buf.len; |
| 28 | + |
| 29 | + const start = timer.lap(); |
| 30 | + var i: usize = 0; |
| 31 | + var r: usize = undefined; |
| 32 | + while (i < N) : (i += 1) { |
| 33 | + r = try @call( |
| 34 | + .{ .modifier = .never_inline }, |
| 35 | + std.unicode.utf8CountCodepoints, |
| 36 | + .{buf}, |
| 37 | + ); |
| 38 | + } |
| 39 | + const end = timer.read(); |
| 40 | + |
| 41 | + const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; |
| 42 | + const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s); |
| 43 | + |
| 44 | + return ResultCount{ .count = r, .throughput = throughput }; |
| 45 | +} |
8 | 46 |
|
9 | 47 | pub fn main() !void {
|
10 | 48 | const stdout = std.io.getStdOut().outStream();
|
11 | 49 |
|
12 | 50 | const args = try std.process.argsAlloc(std.heap.page_allocator);
|
13 | 51 |
|
14 |
| - // Warm up runs |
15 |
| - var buffer0: [32767]u16 align(4096) = undefined; |
16 |
| - _ = try std.unicode.utf8ToUtf16Le(&buffer0, args[1]); |
17 |
| - _ = try std.unicode.utf8ToUtf16Le_better(&buffer0, args[1]); |
18 |
| - |
19 |
| - @fence(.SeqCst); |
20 |
| - var timer = try std.time.Timer.start(); |
21 |
| - @fence(.SeqCst); |
22 |
| - |
23 |
| - var buffer1: [32767]u16 align(4096) = undefined; |
24 |
| - _ = try std.unicode.utf8ToUtf16Le(&buffer1, args[1]); |
25 |
| - |
26 |
| - @fence(.SeqCst); |
27 |
| - const elapsed_ns_orig = timer.lap(); |
28 |
| - @fence(.SeqCst); |
29 |
| - |
30 |
| - var buffer2: [32767]u16 align(4096) = undefined; |
31 |
| - _ = try std.unicode.utf8ToUtf16Le_better(&buffer2, args[1]); |
32 |
| - |
33 |
| - @fence(.SeqCst); |
34 |
| - const elapsed_ns_better = timer.lap(); |
35 |
| - @fence(.SeqCst); |
36 |
| - |
37 |
| - std.debug.warn("original utf8ToUtf16Le: elapsed: {} ns ({} ms)\n", .{ |
38 |
| - elapsed_ns_orig, elapsed_ns_orig / 1000000, |
39 |
| - }); |
40 |
| - std.debug.warn("new utf8ToUtf16Le: elapsed: {} ns ({} ms)\n", .{ |
41 |
| - elapsed_ns_better, elapsed_ns_better / 1000000, |
42 |
| - }); |
43 |
| - asm volatile ("nop" |
44 |
| - : |
45 |
| - : [a] "r" (&buffer1), |
46 |
| - [b] "r" (&buffer2) |
47 |
| - : "memory" |
48 |
| - ); |
| 52 | + try stdout.print("short ASCII strings\n", .{}); |
| 53 | + { |
| 54 | + const result = try benchmarkCodepointCount("abc"); |
| 55 | + try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 56 | + } |
| 57 | + |
| 58 | + try stdout.print("short Unicode strings\n", .{}); |
| 59 | + { |
| 60 | + const result = try benchmarkCodepointCount("ŌŌŌ"); |
| 61 | + try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 62 | + } |
| 63 | + |
| 64 | + try stdout.print("pure ASCII strings\n", .{}); |
| 65 | + { |
| 66 | + const result = try benchmarkCodepointCount("hello" ** 16); |
| 67 | + try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 68 | + } |
| 69 | + |
| 70 | + try stdout.print("pure Unicode strings\n", .{}); |
| 71 | + { |
| 72 | + const result = try benchmarkCodepointCount("こんにちは" ** 16); |
| 73 | + try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 74 | + } |
| 75 | + |
| 76 | + try stdout.print("mixed ASCII/Unicode strings\n", .{}); |
| 77 | + { |
| 78 | + const result = try benchmarkCodepointCount("Hyvää huomenta" ** 16); |
| 79 | + try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 80 | + } |
49 | 81 | }
|
0 commit comments