-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.mojo
65 lines (56 loc) · 1.95 KB
/
benchmark.mojo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from base64 import b64encode, b64decode
from fast_base64 import encode, decode
from time import now
from testing import assert_equal
from samples import mobi_dick_plain
from pathlib import Path
fn main() raises:
var text = Path("/usr/share/dict/words").read_text()
# var text = String(mobi_dick_plain)
var std_min_d_enc = 10000000
var h0 = String("")
for _ in range(10):
var tik = now()
h0 = b64encode(text)
var tok = now()
if std_min_d_enc > tok - tik:
std_min_d_enc = tok - tik
print("Std b64 encode:", (std_min_d_enc) / len(text), len(text))
var h1 = String("")
var fast_min_d_enc = 10000000
for _ in range(10):
var tik = now()
h1 = encode(text)
var tok = now()
if fast_min_d_enc > tok - tik:
fast_min_d_enc = tok - tik
print("Fast b64 encode:", (fast_min_d_enc) / len(text), len(h1), len(text))
print("Encoding speedup:", Float64(std_min_d_enc) / (fast_min_d_enc))
for i in range(len(h0)):
if h0[i] != h1[i]:
print("std Error:", h0[i], "chrome:", h1[i], "on", i)
break
var std_min_d_dec = 1000000
for _ in range(10):
var p: DTypePointer[DType.uint8]
var l: Int
var tik = now()
var s = b64decode(h0)
var tok = now()
assert_equal(text, s)
if std_min_d_dec > tok - tik:
std_min_d_dec = tok - tik
print("Std Decode:", std_min_d_dec / len(h1))
var fast_min_d_dec = 1000000
for _ in range(10):
var p: DTypePointer[DType.uint8]
var l: Int
var tik = now()
p, l = decode[zero_terminated=True](h1)
var tok = now()
assert_equal(text, String(p.bitcast[DType.int8](), l))
if fast_min_d_dec > tok - tik:
fast_min_d_dec = tok - tik
print("Fast Decode:", fast_min_d_dec / len(h1))
print("Decoding speedup:", Float64(std_min_d_dec) / (fast_min_d_dec))
_ = text