Skip to content

Commit

Permalink
add more benchmarks, but scanner needs to be fixed now
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Aug 28, 2024
1 parent fff4eea commit de8b662
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 151 deletions.
19 changes: 19 additions & 0 deletions benchmarks/scanner.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import benchmark
import gojo.bufio
import gojo.bytes
import testing


fn benchmark_scan_runes() -> None:
var input = String("Hello, World!").as_bytes()
var buf = bytes.Buffer(buf=input^)
var scanner = bufio.Scanner[split = bufio.scan_runes](buf^)

while scanner.scan():
print(scanner.current_token())


fn main():
print("Running benchmark_scan_runes")
var report = benchmark.run[benchmark_scan_runes](max_iters=20)
report.print(benchmark.Unit.ms)
17 changes: 17 additions & 0 deletions benchmarks/std_writer.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# fn test_std_writer_speed() raises:
# """STDWriter is roughly 6-7x faster currently."""
# var print_start_time = now()
# for i in range(1, 10000):
# print(i)
# var print_execution_time = now() - print_start_time

# # Create stdout writer
# var writer = STDWriter(1)
# var writer_start_time = now()
# for i in range(1, 10000):
# _ = writer.write_string(str(i))
# var writer_execution_time = now() - writer_start_time

# print("\n\nprint execution time (s): " + str((print_execution_time) / 1e9))
# print("writer execution time (s): " + str((writer_execution_time) / 1e9))
# print("Writer is ", str(print_execution_time // writer_execution_time) + "x faster")
92 changes: 92 additions & 0 deletions benchmarks/string_builder.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import benchmark
from gojo.strings import StringBuilder
from gojo.bytes.buffer import Buffer


fn benchmark_concat[batches: Int = 10000]():
var vec = List[String]()
for _ in range(batches):
vec.append(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod"
" tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
" veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
" commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
" velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
" occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
" mollit anim id est laborum."
)

var concat_output: String = ""
for i in range(len(vec)):
concat_output += vec[i]
_ = concat_output


fn benchmark_string_builder[batches: Int = 10000]():
var new_builder = StringBuilder()
for _ in range(batches):
_ = new_builder.write_string(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod"
" tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
" veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
" commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
" velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
" occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
" mollit anim id est laborum."
)
_ = str(new_builder)


fn benchmark_bytes_buffer[batches: Int = 10000]():
var buffer = Buffer()
for _ in range(batches):
_ = buffer.write_string(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod"
" tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
" veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
" commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
" velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
" occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
" mollit anim id est laborum."
)
_ = str(buffer)


fn main():
# There's a performance penalty for benchmark concat bc it also includes
# the building of the list of strings it concatenates. Trying to build it at comptime takes a loooong time.
print("Running benchmark_concat - 100 batches")
var report = benchmark.run[benchmark_concat[100]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_string_builder - 100 batches")
report = benchmark.run[benchmark_string_builder[100]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_bytes_buffer - 100 batches")
report = benchmark.run[benchmark_bytes_buffer[100]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_concat - 1000 batches")
report = benchmark.run[benchmark_concat[1000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_string_builder - 1000 batches")
report = benchmark.run[benchmark_string_builder[1000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_bytes_buffer - 1000 batches")
report = benchmark.run[benchmark_bytes_buffer[1000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_concat - 10000 batches")
report = benchmark.run[benchmark_concat[10000]](max_iters=2)
report.print(benchmark.Unit.ms)

print("Running benchmark_string_builder - 10000 batches")
report = benchmark.run[benchmark_string_builder[10000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_bytes_buffer - 10000 batches")
report = benchmark.run[benchmark_bytes_buffer[10000]](max_iters=20)
report.print(benchmark.Unit.ms)
149 changes: 0 additions & 149 deletions benchmarks/test_performance.mojo

This file was deleted.

4 changes: 2 additions & 2 deletions examples/scanner/scan_text.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from gojo.bufio import Reader, Scanner, scan_words

fn print_words(owned text: String):
# Create a reader from a string buffer
var buf = buffer.new_buffer(text^)
var buf = buffer.Buffer(buf=text.as_bytes())
var r = Reader(buf^)

# Create a scanner from the reader
Expand All @@ -16,7 +16,7 @@ fn print_words(owned text: String):

fn print_lines(owned text: String):
# Create a reader from a string buffer
var buf = buffer.new_buffer(text^)
var buf = buffer.Buffer(buf=text.as_bytes())
var r = Reader(buf^)

# Create a scanner from the reader
Expand Down

0 comments on commit de8b662

Please sign in to comment.