- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_script.py
More file actions
Latest commit
33 lines (28 loc) · 1.2 KB
/
Copy pathexample_script.py
File metadata and controls
33 lines (28 loc) · 1.2 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
""" Executable script used to demonstrate performance benefits of using cython
for calculation of pairwise sums.
"""
importnumpyasnp
fromtimeimporttime
frompairwise_sum_pythonimportpairwise_sum_python
# Import cython module, catching the case where you forgot to compile the code
try:
frompairwise_sum_cythonimportpairwise_sum_cython
exceptImportError:
msg="The cython module must be compiled first via ``python setup.py build_ext --inplace``"
raiseImportError(msg)
# Catch optional npts command-line argument using argparse
importargparse
parser=argparse.ArgumentParser()
parser.add_argument("-npts", help="Number of elements in the dummy input arrays x and y",
default=int(2e3), type=int)
args=parser.parse_args()
# run the timing tests and print the results
x, y=np.arange(args.npts).astype('f8'), np.arange(args.npts).astype('f8')
start=time()
serial_python_result=pairwise_sum_python(x, y)
end=time()
print("\n\nTotal runtime for serial pairwise_sum_python = {0:.1f} ms".format((end-start)*1000.))
start=time()
serial_cython_result=pairwise_sum_cython(x, y)
end=time()
print("Total runtime for serial pairwise_sum_cython = {0:.1f} ms\n\n".format((end-start)*1000.))