forked from paugier/piconumpy
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake_bench_piconumpy.py
More file actions
120 lines (95 loc) · 2.95 KB
/
make_bench_piconumpy.py
File metadata and controls
120 lines (95 loc) · 2.95 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
with open("bench_array1d.py") as file:
code = file.read()
code = code.split("# begin code functions (don't remove this line)")[1]
code_functions = code.split("# end code functions (don't remove this line)")[0]
def create_tmp_file(name_module):
if name_module == "_piconumpy_hpy_universal":
code_import = """
from piconumpy.util_hpy import import_ext
ext = import_ext()
array = ext.array
"""
else:
code_import = f"from piconumpy.{name_module} import array"
code = (
f"""
from math import pi, cos, sin
import numpy as np
{code_import}
"""
+ code_functions
)
if name_module.startswith("_piconumpy_"):
name = name_module[len("_piconumpy_") :]
else:
name = name_module
with open(f"tmp_{name}.py", "w") as file:
file.write(code)
create_tmp_file("_piconumpy_hpy_universal")
create_tmp_file("purepy")
create_tmp_file("purepy_array")
create_tmp_file("_piconumpy_cython")
create_tmp_file("_piconumpy_hpy")
code = (
"""
import sys
import numpy as np
from piconumpy import array
from math import pi, cos, sin
from pprint import pprint
IS_PYPY = hasattr(sys, 'pypy_version_info')
"""
+ code_functions
+ """
from piconumpy.bench import timeit_verbose
from bench_array1d import bench as bench_numpy, bench_pythran
from tmp_hpy_universal import bench as bench_hpy_universal
from tmp_purepy import bench as bench_piconumpy_purepy
from tmp_purepy_array import bench as bench_piconumpy_purepy_array
from tmp_cython import bench as bench_cython
if not IS_PYPY:
from tmp_hpy import bench as bench_hpy
pprint({key: sys.implementation.__dict__[key] for key in ("cache_tag", "version")})
# get norm from Julia benchmark
with open("tmp_result_julia.txt") as file:
norm = float(file.read())
max_length_name = len("piconumpy (CPython C-API)") + 2
fmt_name = f"{{:{max_length_name}s}}"
name = fmt_name.format("Julia")
print(f"{name}: 1 * norm = {norm:4.3g} s")
n_sleds = 100
n_time = 200
g = locals()
def timeit(name_func, name, total_duration=2):
return timeit_verbose(
name_func + "(n_sleds, n_time)",
globals=g,
name=name,
print_time=False,
norm=norm,
max_length_name=max_length_name,
total_duration=total_duration,
)
timeit("bench", name="PicoNumpy (CPython C-API)")
if not IS_PYPY:
timeit("bench_hpy", name="PicoNumpy (HPy CPy ABI)")
timeit("bench_hpy_universal", name="PicoNumpy (HPy Universal)")
timeit("bench_pythran", name="Transonic-Pythran")
try:
timeit("bench_numpy", name="Numpy", total_duration=8)
except RuntimeError:
print("Skip bench_numpy because it's too slow")
timeit(
"bench_piconumpy_purepy", name="PicoNumpy (purepy)",
)
timeit(
"bench_piconumpy_purepy_array", name="PicoNumpy (purepy_array)",
)
try:
timeit("bench_cython", name="PicoNumpy (Cython)", total_duration=8)
except RuntimeError:
print("Skip bench_cython because it's too slow")
"""
)
with open("tmp.py", "w") as file:
file.write(code)