Skip to content

Instantly share code, notes, and snippets.

@bhawkins
Last active December 15, 2016 22:02
Show Gist options
  • Save bhawkins/7cdbd5b9372cb798e34e21f92279d2dc to your computer and use it in GitHub Desktop.
Save bhawkins/7cdbd5b9372cb798e34e21f92279d2dc to your computer and use it in GitHub Desktop.
#!python
# Adapted from StackOverflow user mab
# http://stackoverflow.com/a/33672015/112699
import timeit
import numpy as np
import pandas as pd
from IPython.display import display
def profile_this(methods, setup='', n=100, niter=10**4):
print('n: {0}, niter: {1:.0e}'.format(n, niter))
timings = np.array([timeit.timeit(method, setup=setup, number=niter) for method in methods])
ranking = np.argsort(timings)
timings = np.array(timings)[ranking]
methods = np.array(methods)[ranking]
speedups = np.max(timings) / timings
pd.set_option('html', False)
data = {'time (s)': timings, 'speedup': ['{0:0.2f}x'.format(s) if 1 != s else '' for s in speedups], 'methods': methods}
data_frame = pd.DataFrame(data, columns=['time (s)', 'speedup', 'methods'])
display(data_frame)
print()
setup_template = '''import numpy as np; n={n}; x = np.random.random(n)'''
methods = (
'''y = np.zeros(n, dtype=x.dtype); y[:] = x''',
'''y = np.zeros_like(x); y[:] = x''',
'''y = np.empty(n, dtype=x.dtype); y[:] = x''',
'''y = np.empty_like(x); y[:] = x''',
'''y = np.copy(x)''',
'''y = x.astype(x.dtype)''',
'''y = 1*x''',
'''y = np.empty_like(x); np.copyto(y, x)''',
'''y = np.empty_like(x); np.copyto(y, x, casting='no')''',
'''y = np.empty(n)\nfor i in range(x.size):\n\ty[i] = x[i]'''
)
for logn, logit in ((2, 6), (3, 6), (3.8, 6), (4, 6), (5, 5), (6, 4.5)):
n, niter = int(10**logn), int(10**logit)
setup = setup_template.format(n=n)
if n > 100:
profile_this(methods[:-1], setup, n, niter)
else:
profile_this(methods, setup, n, niter)
n: 100, niter: 1e+06
time (s) speedup methods
0 0.377211 35.91x y = x.astype(x.dtype)
1 0.440208 30.77x y = np.empty_like(x); y[:] = x
2 0.449129 30.16x y = np.empty_like(x); np.copyto(y, x)
3 0.647460 20.92x y = np.empty_like(x); np.copyto(y, x, casting=...
4 0.722250 18.75x y = np.copy(x)
5 0.836623 16.19x y = 1*x
6 1.062869 12.74x y = np.empty(n, dtype=x.dtype); y[:] = x
7 1.103768 12.27x y = np.zeros(n, dtype=x.dtype); y[:] = x
8 2.268195 5.97x y = np.zeros_like(x); y[:] = x
9 13.545740 y = np.empty(n)\nfor i in range(x.size):\n\ty[...
()
n: 1000, niter: 1e+06
time (s) speedup methods
0 0.651657 4.44x y = x.astype(x.dtype)
1 0.749732 3.86x y = np.empty_like(x); np.copyto(y, x)
2 0.773912 3.74x y = np.empty_like(x); y[:] = x
3 0.988279 2.93x y = np.empty_like(x); np.copyto(y, x, casting=...
4 1.123788 2.58x y = np.copy(x)
5 1.410723 2.05x y = 1*x
6 1.475579 1.96x y = np.empty(n, dtype=x.dtype); y[:] = x
7 1.508836 1.92x y = np.zeros(n, dtype=x.dtype); y[:] = x
8 2.895328 y = np.zeros_like(x); y[:] = x
()
n: 6309, niter: 1e+06
time (s) speedup methods
0 2.141714 3.00x y = x.astype(x.dtype)
1 2.203533 2.91x y = np.empty_like(x); y[:] = x
2 2.231979 2.88x y = np.empty_like(x); np.copyto(y, x)
3 2.502381 2.57x y = np.copy(x)
4 2.665101 2.41x y = np.empty_like(x); np.copyto(y, x, casting=...
5 2.822849 2.27x y = np.empty(n, dtype=x.dtype); y[:] = x
6 2.892670 2.22x y = 1*x
7 4.391657 1.46x y = np.zeros(n, dtype=x.dtype); y[:] = x
8 6.421201 y = np.zeros_like(x); y[:] = x
()
n: 10000, niter: 1e+06
time (s) speedup methods
0 3.263873 2.81x y = x.astype(x.dtype)
1 3.274130 2.80x y = np.empty_like(x); np.copyto(y, x)
2 3.412645 2.68x y = np.empty_like(x); np.copyto(y, x, casting=...
3 3.607340 2.54x y = np.empty_like(x); y[:] = x
4 3.851676 2.38x y = np.copy(x)
5 4.292577 2.13x y = np.empty(n, dtype=x.dtype); y[:] = x
6 4.457260 2.06x y = 1*x
7 6.469592 1.42x y = np.zeros(n, dtype=x.dtype); y[:] = x
8 9.160425 y = np.zeros_like(x); y[:] = x
()
n: 100000, niter: 1e+05
time (s) speedup methods
0 3.588155 2.25x y = np.empty_like(x); y[:] = x
1 3.637090 2.22x y = np.empty_like(x); np.copyto(y, x, casting=...
2 3.673425 2.19x y = np.copy(x)
3 3.706141 2.18x y = x.astype(x.dtype)
4 3.729351 2.16x y = np.empty(n, dtype=x.dtype); y[:] = x
5 3.785925 2.13x y = np.empty_like(x); np.copyto(y, x)
6 4.758041 1.69x y = 1*x
7 6.385678 1.26x y = np.zeros(n, dtype=x.dtype); y[:] = x
8 8.061876 y = np.zeros_like(x); y[:] = x
()
n: 1000000, niter: 3e+04
time (s) speedup methods
0 17.355430 1.95x y = np.empty_like(x); np.copyto(y, x, casting=...
1 17.450905 1.94x y = x.astype(x.dtype)
2 17.702589 1.91x y = np.empty_like(x); y[:] = x
3 17.876651 1.89x y = np.empty_like(x); np.copyto(y, x)
4 18.069141 1.87x y = np.empty(n, dtype=x.dtype); y[:] = x
5 18.179504 1.86x y = np.copy(x)
6 22.717311 1.49x y = 1*x
7 29.783388 1.14x y = np.zeros(n, dtype=x.dtype); y[:] = x
8 33.870803 y = np.zeros_like(x); y[:] = x
()
[Finished in 338.375s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment