Skip to content

Instantly share code, notes, and snippets.

@newville
Last active August 29, 2015 14:16
Show Gist options
  • Save newville/b36b54eb47637979e256 to your computer and use it in GitHub Desktop.
Save newville/b36b54eb47637979e256 to your computer and use it in GitHub Desktop.
Numpy interp() benchmark
from __future__ import print_function
import sys
import timeit
import numpy as np
np.random.seed(0)
ndata=20000
# data to ne interpolated
xp = np.linspace(0, 10, 1+ndata)
fp = np.sin(xp / 2.0)
# test arrays
arr0 = np.linspace(2.0, 7.0, 1+ndata*5)
arr1 = np.linspace(2.0, 7.0, 1+ndata)
arr2 = np.linspace(2.1, 6.8, 1+ndata/2)
arr3 = np.linspace(2.1, 7.5, 1+ndata/2)
arr4 = np.linspace(1.1, 9.5, 1+ndata/5)
arr5 = np.linspace(3.1, 5.3, 1+ndata) * 1.09
arr6 = np.linspace(3.1, 8.3, 1+ndata/2) * 1.09
arr7 = np.linspace(3.1, 5.3, 1+ndata) * 0.91
arr8 = np.linspace(3.1, 8.3, 1+ndata/2) * 0.91
arr9 = np.linspace(3.1, 5.3, 1+ndata/2) + 0.3 * np.sin(np.arange(1+ndata/2)*np.pi/(1+ndata/2))
arr10 = np.linspace(3.1, 5.3, 1+ndata) + np.random.normal(size=1+ndata, scale=0.5/ndata)
arr11 = np.linspace(3.1, 5.3, 1+ndata) + np.random.normal(size=1+ndata, scale=2.0/ndata)
arr12 = np.linspace(3.1, 5.3, 1+ndata) + np.random.normal(size=1+ndata, scale=5.0/ndata)
arr13 = np.linspace(3.1, 5.3, 1+ndata) + np.random.normal(size=1+ndata, scale=20.0/ndata)
arr14 = np.linspace(3.1, 5.3, 1+ndata) + np.random.normal(size=1+ndata, scale=50.0/ndata)
arr15 = np.linspace(3.1, 5.3, 1+ndata) + np.random.normal(size=1+ndata, scale=200.0/ndata)
arr16 = np.random.rand(1+ndata)*9.0 + 0.6
arr17 = np.random.rand(1+ndata*2)*4.0 + 1.3
def much_finer_grid():
out = np.interp(arr0, xp, fp)
def finer_grid():
out = np.interp(arr1, xp, fp)
def similar_grid():
out = np.interp(arr2, xp, fp)
def coarser_grid():
out = np.interp(arr3, xp, fp)
def much_coarser_grid():
out = np.interp(arr4, xp, fp)
def finer_stretched_grid():
out = np.interp(arr5, xp, fp)
def similar_stretched_grid():
out = np.interp(arr5, xp, fp)
def finer_compressed_grid():
out = np.interp(arr7, xp, fp)
def similar_compressed_grid():
out = np.interp(arr8, xp, fp)
def warped_grid():
out = np.interp(arr9, xp, fp)
def very_low_noise_grid():
out = np.interp(arr10, xp, fp)
def low_noise_grid():
out = np.interp(arr11, xp, fp)
def med_noise_grid():
out = np.interp(arr12, xp, fp)
def high_noise_grid():
out = np.interp(arr13, xp, fp)
def very_high_noise_grid():
out = np.interp(arr14, xp, fp)
def extreme_noise_grid():
out = np.interp(arr15, xp, fp)
def random_fine_grid():
out = np.interp(arr16, xp, fp)
def random_grid():
out = np.interp(arr17, xp, fp)
if __name__ == '__main__':
print(" Python Version: ", sys.version)
print(" Numpy Version: ", np.__version__)
funcs = ('much_finer_grid', 'finer_grid', 'similar_grid',
'coarser_grid', 'much_coarser_grid', 'finer_stretched_grid',
'similar_stretched_grid', 'finer_compressed_grid',
'similar_compressed_grid', 'warped_grid',
'very_low_noise_grid', 'low_noise_grid', 'med_noise_grid',
'high_noise_grid', 'very_high_noise_grid',
'extreme_noise_grid', 'random_fine_grid', 'random_grid')
for func in funcs:
out = timeit.repeat('%s()' % func,
"from __main__ import %s" % func, number=200,
repeat=3)
print(" %s %.5f" % ((func+24*' ')[:24], min(out)))
Tests used a Linux x86-64 machine with Python 2.7.8 (default, Nov 10 2014, 08:19:18) [GCC 4.9.2 20141101 (Red Hat 4.9.2-1)],
and compared timeit results using interp() from numpy 1.9.1 and from 2b60f5b,
with patched version of numpy/core/src/multiarray/compiled_base.c, as at https://github.com/numpy/numpy/pull/5594
+--------------------+------------+------------------+---------+
| Test Name | Numpy1.9.1 | Patched(2b60f5b) | speedup |
| (_grid) | time (s) | time (s) | |
+====================+============+==================+=========+
| much_finer | 1.12595 | 0.20020 | 5.62 |
| finer | 0.36595 | 0.06899 | 5.30 |
| similar | 0.18936 | 0.02556 | 7.41 |
| coarser | 0.19231 | 0.02892 | 6.65 |
| much_coarser | 0.08912 | 0.02614 | 3.41 |
| finer_stretched | 0.31864 | 0.06798 | 4.69 |
| similar_stretched | 0.31841 | 0.06797 | 4.68 |
| finer_compressed | 0.30684 | 0.06562 | 4.68 |
| similar_compressed | 0.18934 | 0.02665 | 7.10 |
| warped | 0.16816 | 0.02425 | 6.93 |
| very_low_noise | 0.31264 | 0.06921 | 4.52 |
| low_noise | 0.31194 | 0.07759 | 4.02 |
| med_noise | 0.31677 | 0.09811 | 3.23 |
| high_noise | 0.33832 | 0.18548 | 1.82 |
| very_high_noise | 0.36188 | 0.26636 | 1.36 |
| extreme_noise | 0.40684 | 0.39503 | 1.03 |
| random_fine | 0.69047 | 0.76608 | 0.90 |
| random | 1.21906 | 1.36019 | 0.90 |
+====================+============+==================+=========+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment