-
Notifications
You must be signed in to change notification settings - Fork 10
/
run.py
84 lines (71 loc) · 2.47 KB
/
run.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import timeit
from pathlib import Path
import pytomlpp
_parsers = [('pytomlpp', pytomlpp.loads)]
try:
import rtoml
_parsers.append(('rtoml', rtoml.loads))
except ImportError:
print("Couldn't import 'rtoml'")
try:
import tomli
_parsers.append(('tomli', tomli.loads))
except ImportError:
print("Couldn't import 'tomli'")
try:
import toml
_parsers.append(('toml', toml.loads))
except ImportError:
print("Couldn't import 'toml'")
try:
import qtoml
_parsers.append(('qtoml', qtoml.loads))
except ImportError:
print("Couldn't import 'qtoml'")
try:
import tomlkit
_parsers.append(('tomlkit', tomlkit.parse))
except ImportError:
print("Couldn't import 'tomlkit'")
try:
import tomllib # PEP 680
_parsers.append(('tomllib', tomllib.loads))
except ImportError:
# doesn't make sense to report this one since it will eventually be a part of the standard lib and can't be installed separately
pass
def run(run_count = 1000):
test_data = ''
with open(str(Path(Path(__file__).parent, 'data.toml').resolve()), 'r', encoding='utf-8') as f:
test_data = f.read()
print(f'Parsing data.toml {run_count} times:')
durations = []
for name, func in _parsers:
# pylint: disable=cell-var-from-loop
# pylint: disable=bare-except
run_str = f' Running {name}...'
print(run_str, end='', flush=True)
try:
durations.append((name, timeit.timeit(lambda: func(test_data), number=run_count), True))
except:
durations.append((name, 9999999999999999999999999999999, False))
print('\b'*len(run_str), end='')
print(' '*len(run_str), end='')
print('\b'*len(run_str), end='')
durations.sort(key=lambda dur: dur[1])
baseline = durations[0][1]
for name, duration, ok in durations:
if ok:
res = str(duration).split('.')
if len(res) == 1:
res.append('000')
print(f'{name:>10}: {res[0]:>3}.{res[1][:3]} s', end='')
if duration != baseline:
delta = str(duration / baseline).split('.')
if len(delta) == 1:
delta.append('00')
print(rf' ({delta[0]:>2}.{delta[1][:2]}x)', end='')
print('')
else:
print(f'{name:>10}: Parsing failed. Likely not TOML 1.0.0-compliant.')
if __name__ == '__main__':
run()