Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def alt_cosine(x,y):
- return 1 - np.inner(x,y)/np.sqrt(np.dot(x,x)*np.dot(y,y))
- from libc.math cimport sqrt
- def alt_cosine_2(x,y):
- return 1 - np.inner(x,y)/sqrt(np.dot(x,x)*np.dot(y,y))
- >>> cosine() # ... make some timings
- 5.27526156300155e-05 # mean calculation time for one loop
- >>> alt_cosine()
- 9.913400815003115e-06
- >>> alt_cosine_2()
- 7.0269494536660205e-06
- import numpy as np
- def f1(x, y):
- return 1 - np.inner(x,y)/np.sqrt(np.dot(x,x)*np.dot(y,y))
- from numpy import inner, sqrt, dot
- def f2(x, y):
- return 1 - inner(x,y)/sqrt(dot(x,x)*dot(y,y))
- x = np.ones(100000)
- %timeit f1(x,x)
- # 58.1 µs ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
- %timeit f2(x,x)
- # 36.3 µs ± 3.32 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
- def f3(x, y, inner=inner, sqrt=sqrt, dot=dot):
- return 1 - inner(x,y)/sqrt(dot(x,x)*dot(y,y))
- %timeit f3(x,x)
- # 36.5 µs ± 3.42 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Add Comment
Please, Sign In to add comment