Skip to content

Commit

Permalink
fix all problems
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Oct 21, 2024
1 parent 2f43689 commit 352aca8
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 79 deletions.
112 changes: 33 additions & 79 deletions stdlib/benchmarks/collections/bench_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,44 @@ from random import random_si64, seed
from pathlib import cwd
from collections import Optional
from utils._utf8_validation import _is_valid_utf8
from os import abort


# ===----------------------------------------------------------------------===#
# Benchmark Data
# ===----------------------------------------------------------------------===#
fn make_string[
length: UInt = 0, filename: StringLiteral = "UN charter EN.txt"
]() -> String:
length: UInt = 0
](filename: StringLiteral = "UN_charter_EN.txt") -> String:
"""Make a `String` made of items in the `./data` directory or random bytes
(ASCII value range) in case opening the file fails.
Parameters:
length: The length in bytes of the resulting `String`. If == 0 -> the
whole file content.
Args:
filename: The name of the file inside the `./data` directory.
"""

try:
var f = open(cwd() / "data" / filename, "rb")
directory = cwd() / "stdlib" / "benchmarks" / "collections" / "data"
var f = open(directory / filename, "rb")

@parameter
if length > 0:
var items = f.read_bytes(length)
for i in range(length - len(items)):
i = 0
while length > len(items):
items.append(items[i])
i = i + 1 if i < len(items) - 1 else 0
items.append(0)
return String(items^)
else:
return String(f.read_bytes())
except:
pass
print("open file failed, reverting to random bytes")
var items = List[UInt8, hint_trivial_type=True](capacity=length + 1)
for i in range(length):
items[i] = random_si64(0, 0b0111_1111).cast[DType.uint8]()
items[length] = 0
return String(items^)
except e:
print(e)
return abort[String]()


# ===----------------------------------------------------------------------===#
Expand All @@ -77,10 +78,10 @@ fn bench_string_init(inout b: Bencher) raises:
@parameter
fn bench_string_count[
length: UInt = 0,
filename: StringLiteral = "UN charter EN",
filename: StringLiteral = "UN_charter_EN",
sequence: StringLiteral = "a",
](inout b: Bencher) raises:
var items = make_string[length, filename + ".txt"]()
var items = make_string[length](filename + ".txt")

@always_inline
@parameter
Expand All @@ -98,10 +99,10 @@ fn bench_string_count[
@parameter
fn bench_string_split[
length: UInt = 0,
filename: StringLiteral = "UN charter EN",
filename: StringLiteral = "UN_charter_EN",
sequence: Optional[StringLiteral] = None,
](inout b: Bencher) raises:
var items = make_string[length, filename + ".txt"]()
var items = make_string[length](filename + ".txt")

@always_inline
@parameter
Expand All @@ -124,9 +125,9 @@ fn bench_string_split[
# ===----------------------------------------------------------------------===#
@parameter
fn bench_string_splitlines[
length: UInt = 0, filename: StringLiteral = "UN charter EN"
length: UInt = 0, filename: StringLiteral = "UN_charter_EN"
](inout b: Bencher) raises:
var items = make_string[length, filename + ".txt"]()
var items = make_string[length](filename + ".txt")

@always_inline
@parameter
Expand All @@ -143,9 +144,9 @@ fn bench_string_splitlines[
# ===----------------------------------------------------------------------===#
@parameter
fn bench_string_lower[
length: UInt = 0, filename: StringLiteral = "UN charter EN"
length: UInt = 0, filename: StringLiteral = "UN_charter_EN"
](inout b: Bencher) raises:
var items = make_string[length, filename + ".txt"]()
var items = make_string[length](filename + ".txt")

@always_inline
@parameter
Expand All @@ -162,9 +163,9 @@ fn bench_string_lower[
# ===----------------------------------------------------------------------===#
@parameter
fn bench_string_upper[
length: UInt = 0, filename: StringLiteral = "UN charter EN"
length: UInt = 0, filename: StringLiteral = "UN_charter_EN"
](inout b: Bencher) raises:
var items = make_string[length, filename + ".txt"]()
var items = make_string[length](filename + ".txt")

@always_inline
@parameter
Expand All @@ -182,11 +183,11 @@ fn bench_string_upper[
@parameter
fn bench_string_replace[
length: UInt = 0,
filename: StringLiteral = "UN charter EN",
filename: StringLiteral = "UN_charter_EN",
old: StringLiteral = "a",
new: StringLiteral = "A",
](inout b: Bencher) raises:
var items = make_string[length, filename + ".txt"]()
var items = make_string[length](filename + ".txt")

@always_inline
@parameter
Expand All @@ -203,9 +204,9 @@ fn bench_string_replace[
# ===----------------------------------------------------------------------===#
@parameter
fn bench_string_is_valid_utf8[
length: UInt = 0, filename: StringLiteral = "UN charter EN"
length: UInt = 0, filename: StringLiteral = "UN_charter_EN"
](inout b: Bencher) raises:
var items = make_string[length, filename + ".html"]()
var items = make_string[length](filename + ".html")

@always_inline
@parameter
Expand All @@ -225,62 +226,15 @@ def main():
var m = Bench(BenchConfig(num_repetitions=1))
m.bench_function[bench_string_init](BenchId("bench_string_init"))
alias filenames = (
"UN charter EN",
"UN charter ES",
"UN charter AR",
"UN charter RU",
"UN charter zh-CN",
"UN_charter_EN",
"UN_charter_ES",
"UN_charter_AR",
"UN_charter_RU",
"UN_charter_zh-CN",
)
alias old_chars = ("a", "ó", "ل", "и", "")
alias new_chars = ("A", "Ó", "ل", "И", "")
alias lengths = (
10,
20,
30,
40,
50,
60,
70,
80,
90,
100,
200,
300,
400,
500,
600,
700,
800,
900,
1000,
2000,
3000,
4000,
5000,
6000,
7000,
8000,
9000,
10_000,
20_000,
30_000,
40_000,
50_000,
60_000,
70_000,
80_000,
90_000,
100_000,
200_000,
300_000,
400_000,
500_000,
600_000,
700_000,
800_000,
900_000,
1_000_000,
)
alias lengths = (10, 30, 50, 100, 1000, 10_000, 100_000, 1_000_000)

@parameter
for i in range(len(lengths)):
Expand Down

0 comments on commit 352aca8

Please sign in to comment.