Skip to content

Commit

Permalink
up max version
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Sep 12, 2024
1 parent 8a93da5 commit 5403b1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 45 deletions.
20 changes: 8 additions & 12 deletions benchmarks/scanner.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,33 @@ alias SPACE = " "


fn benchmark_scan_runes[batches: Int]() -> None:
var builder = strings.StringBuilder(capacity=batches)
var builder = strings.StringBuilder(capacity=batches * 4)
for _ in range(batches):
_ = builder.write_string(FIRE)

var buf = bytes.Buffer(buf=str(builder).as_bytes())
var scanner = bufio.Scanner[split = bufio.scan_runes, capacity=batches](buf^)
var scanner = bufio.Scanner[bufio.scan_runes](bytes.Buffer(buf=str(builder).as_bytes()), capacity=batches)
while scanner.scan():
_ = scanner.current_token()


fn benchmark_scan_words[batches: Int]() -> None:
var builder = strings.StringBuilder(capacity=batches)
var builder = strings.StringBuilder(capacity=batches * 5)
for _ in range(batches):
_ = builder.write_string(FIRE)
_ = builder.write_string(SPACE)

var buf = bytes.Buffer(str(builder))
var scanner = bufio.Scanner[split = bufio.scan_words, capacity=batches](buf^)
var scanner = bufio.Scanner[bufio.scan_words](bytes.Buffer(str(builder)), capacity=batches)
while scanner.scan():
_ = scanner.current_token()


fn benchmark_scan_lines[batches: Int]() -> None:
var builder = strings.StringBuilder(capacity=batches)
var builder = strings.StringBuilder(capacity=batches * 5)
for _ in range(batches):
_ = builder.write_string(FIRE)
_ = builder.write_string(NEWLINE)

var buf = bytes.Buffer(str(builder))
var scanner = bufio.Scanner[capacity=batches](buf^)
var scanner = bufio.Scanner(bytes.Buffer(str(builder)), capacity=batches)
while scanner.scan():
_ = scanner.current_token()

Expand All @@ -51,14 +48,13 @@ fn benchmark_scan_bytes[batches: Int]() -> None:
for _ in range(batches):
_ = builder.write_string(SPACE)

var buf = bytes.Buffer(str(builder))
var scanner = bufio.Scanner[split = bufio.scan_bytes, capacity=batches](buf^)
var scanner = bufio.Scanner[bufio.scan_bytes](bytes.Buffer(str(builder)), capacity=batches)
while scanner.scan():
_ = scanner.current_token()


fn benchmark_newline_split[batches: Int]() -> None:
var builder = strings.StringBuilder(capacity=batches)
var builder = strings.StringBuilder(capacity=batches * 5)
for _ in range(batches):
_ = builder.write_string(FIRE)
_ = builder.write_string(NEWLINE)
Expand Down
44 changes: 11 additions & 33 deletions benchmarks/string_builder.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,31 @@ import benchmark
from gojo.strings import StringBuilder
from gojo.bytes.buffer import Buffer

alias SAMPLE_TEXT = """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."""

fn benchmark_concat[batches: Int = 10000]():
var vec = List[String]()

fn benchmark_concat[batches: Int]():
var vec = List[String](capacity=batches * len(SAMPLE_TEXT))
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."
)
vec.append(SAMPLE_TEXT)

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()
fn benchmark_string_builder[batches: Int]():
var new_builder = StringBuilder(capacity=batches * len(SAMPLE_TEXT))
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."
)
_ = new_builder.write_string(SAMPLE_TEXT)
_ = str(new_builder)


fn benchmark_bytes_buffer[batches: Int = 10000]():
var buffer = Buffer()
fn benchmark_bytes_buffer[batches: Int]():
var buffer = Buffer(capacity=batches * len(SAMPLE_TEXT))
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."
)
_ = buffer.write_string(SAMPLE_TEXT)
_ = str(buffer)


Expand Down

0 comments on commit 5403b1e

Please sign in to comment.