Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 3d8958d

Browse files
committed
update scanner benchmark
1 parent b340a2a commit 3d8958d

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

benchmarks/scanner.mojo

+93
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import testing
66

77

88
alias FIRE = "🔥"
9+
alias NEWLINE = "\n"
10+
alias CARRIAGE_RETURN = "\r"
11+
alias SPACE = " "
912

1013

1114
fn benchmark_scan_runes[batches: Int]() -> None:
@@ -19,6 +22,55 @@ fn benchmark_scan_runes[batches: Int]() -> None:
1922
_ = scanner.current_token()
2023

2124

25+
fn benchmark_scan_words[batches: Int]() -> None:
26+
var builder = strings.StringBuilder(capacity=batches)
27+
for _ in range(batches):
28+
_ = builder.write_string(FIRE)
29+
_ = builder.write_string(SPACE)
30+
31+
var buf = bytes.Buffer(str(builder))
32+
var scanner = bufio.Scanner[split = bufio.scan_words, capacity=batches](buf^)
33+
while scanner.scan():
34+
_ = scanner.current_token()
35+
36+
37+
fn benchmark_scan_lines[batches: Int]() -> None:
38+
var builder = strings.StringBuilder(capacity=batches)
39+
for _ in range(batches):
40+
_ = builder.write_string(FIRE)
41+
_ = builder.write_string(NEWLINE)
42+
43+
var buf = bytes.Buffer(str(builder))
44+
var scanner = bufio.Scanner[capacity=batches](buf^)
45+
while scanner.scan():
46+
_ = scanner.current_token()
47+
48+
49+
fn benchmark_scan_bytes[batches: Int]() -> None:
50+
var builder = strings.StringBuilder(capacity=batches)
51+
for _ in range(batches):
52+
_ = builder.write_string(SPACE)
53+
54+
var buf = bytes.Buffer(str(builder))
55+
var scanner = bufio.Scanner[split = bufio.scan_bytes, capacity=batches](buf^)
56+
while scanner.scan():
57+
_ = scanner.current_token()
58+
59+
60+
fn benchmark_newline_split[batches: Int]() -> None:
61+
var builder = strings.StringBuilder(capacity=batches)
62+
for _ in range(batches):
63+
_ = builder.write_string(FIRE)
64+
_ = builder.write_string(NEWLINE)
65+
66+
try:
67+
var lines = str(builder).split(NEWLINE)
68+
for line in lines:
69+
_ = line
70+
except e:
71+
pass
72+
73+
2274
fn main():
2375
# There's a time penalty for building the input text, for now.
2476
print("Running benchmark_scan_runes - 100")
@@ -32,3 +84,44 @@ fn main():
3284
print("Running benchmark_scan_runes - 10000")
3385
report = benchmark.run[benchmark_scan_runes[10000]](max_iters=20)
3486
report.print(benchmark.Unit.ms)
87+
88+
print("Running benchmark_scan_words - 100")
89+
report = benchmark.run[benchmark_scan_words[100]](max_iters=20)
90+
report.print(benchmark.Unit.ms)
91+
92+
print("Running benchmark_scan_words - 1000")
93+
report = benchmark.run[benchmark_scan_words[1000]](max_iters=20)
94+
report.print(benchmark.Unit.ms)
95+
96+
print("Running benchmark_scan_words - 10000")
97+
report = benchmark.run[benchmark_scan_words[10000]](max_iters=20)
98+
report.print(benchmark.Unit.ms)
99+
100+
print("Running benchmark_scan_lines - 100")
101+
report = benchmark.run[benchmark_scan_lines[100]](max_iters=20)
102+
report.print(benchmark.Unit.ms)
103+
104+
print("Running benchmark_scan_lines - 1000")
105+
report = benchmark.run[benchmark_scan_lines[1000]](max_iters=20)
106+
report.print(benchmark.Unit.ms)
107+
108+
print("Running benchmark_scan_lines - 10000")
109+
report = benchmark.run[benchmark_scan_lines[10000]](max_iters=20)
110+
report.print(benchmark.Unit.ms)
111+
112+
# To compare against scan lines
113+
print("Running benchmark_newline_split - 10000")
114+
report = benchmark.run[benchmark_newline_split[10000]](max_iters=20)
115+
report.print(benchmark.Unit.ms)
116+
117+
print("Running benchmark_scan_bytes - 100")
118+
report = benchmark.run[benchmark_scan_bytes[100]](max_iters=20)
119+
report.print(benchmark.Unit.ms)
120+
121+
print("Running benchmark_scan_bytes - 1000")
122+
report = benchmark.run[benchmark_scan_bytes[1000]](max_iters=20)
123+
report.print(benchmark.Unit.ms)
124+
125+
print("Running benchmark_scan_bytes - 10000")
126+
report = benchmark.run[benchmark_scan_bytes[10000]](max_iters=20)
127+
report.print(benchmark.Unit.ms)

0 commit comments

Comments
 (0)