对于小数组,比scipy. nimage .filters.laplace更快的离散拉普拉斯
A faster discrete Laplacian than scipy.ndimage.filters.laplace for small arrays
My在scipy.ndimage.filters.laplace()
中花费了大量的计算时间
scipy
和numpy
的主要优点是将C/C++
的矢量化计算包裹在python
中。scipy.ndimage.filters.laplace()
由_nd_image.correlate1d
衍生而来部分优化库nd_image.h
是否有更快的方法在大小为10-100
的数组中执行此操作?
定义 拉普拉斯滤波器 -忽略除法
-
a[i-1] - 2*a[i] + a[i+1]
-
可选可以理想地绕过
n=a.shape[0]
边界a[n-1] - 2*a[n-1] + a[0]
问题的根源在于scipy
出色的错误处理和调试。然而,在用户知道他们在做什么的情况下,它只是提供了额外的开销。
下面的代码剥离了scipy
后端的所有python
混乱,并直接访问C++
函数以获得~6x
加速!
laplace == Mine ? True
testing timings...
array size 10
100000 loops, best of 3: 12.7 µs per loop
100000 loops, best of 3: 2.3 µs per loop
array size 100
100000 loops, best of 3: 12.7 µs per loop
100000 loops, best of 3: 2.5 µs per loop
array size 100000
1000 loops, best of 3: 413 µs per loop
1000 loops, best of 3: 404 µs per loop
from scipy import ndimage
from scipy.ndimage import _nd_image
import numpy as np
laplace_filter = np.asarray([1, -2, 1], dtype=np.float64)
def fastLaplaceNd(arr):
output = np.zeros(arr.shape, 'float64')
if arr.ndim > 0:
_nd_image.correlate1d(arr, laplace_filter, 0, output, 1, 0.0, 0)
if arr.ndim == 1: return output
for ax in xrange(1, arr.ndim):
output += _nd_image.correlate1d(arr, laplace_filter, ax, output, 1, 0.0, 0)
return output
if __name__ == '__main__':
arr = np.random.random(10)
test = (ndimage.filters.laplace(arr, mode='wrap') == fastLaplace(arr)).all()
assert test
print "laplace == Mine ?", test
print 'testing timings...'
print "array size 10"
%timeit ndimage.filters.laplace(arr, mode='wrap')
%timeit fastLaplace(arr)
print 'array size 100'
arr = np.random.random(100)
%timeit ndimage.filters.laplace(arr, mode='wrap')
%timeit fastLaplace(arr)
print "array size 100000"
arr = np.random.random(100000)
%timeit ndimage.filters.laplace(arr, mode='wrap')
%timeit fastLaplace(arr)
from scipy import ndimage
from scipy.ndimage import _nd_image
import numpy as np
laplace_filter = np.asarray([1, -2, 1], dtype=np.float64)
def fastLaplaceNd(arr):
output = np.zeros(arr.shape, 'float64')
if arr.ndim > 0:
_nd_image.correlate1d(arr, laplace_filter, 0, output, 1, 0.0, 0)
if arr.ndim == 1: return output
for ax in xrange(1, arr.ndim):
output += _nd_image.correlate1d(arr, laplace_filter, ax, output, 1, 0.0, 0)
return output
if __name__ == '__main__':
arr = np.random.random(10)
test = (ndimage.filters.laplace(arr, mode='wrap') == fastLaplace(arr)).all()
assert test
print "laplace == Mine ?", test
print 'testing timings...'
print "array size 10"
%timeit ndimage.filters.laplace(arr, mode='wrap')
%timeit fastLaplace(arr)
print 'array size 100'
arr = np.random.random(100)
%timeit ndimage.filters.laplace(arr, mode='wrap')
%timeit fastLaplace(arr)
print "array size 100000"
arr = np.random.random(100000)
%timeit ndimage.filters.laplace(arr, mode='wrap')
%timeit fastLaplace(arr)
相关文章:
- 毕达哥拉斯三重嵌套循环误解
- 康斯特指针C++斯特劳斯特鲁普
- C++:根据斯特劳斯特鲁普的例子播种随机数
- C++:斯特劳斯特鲁普iterator_traits示例不编译?
- 东康斯泰克普/康斯蒂尼特/康斯特瓦尔在C++20 中允许吗?
- 如何修复我的 c++ 毕达哥拉斯三重查找器中的'access violation reading location'错误?
- 用于双向迭代器的高德纳-莫里斯-普拉特算法
- 链接链接器/加载器错误时"undefined reference to ..."与拉斯皮康库链接时
- 使用C++程序中的拉斯皮斯蒂尔读取相机图像
- 欧拉斯(Eulers Phi)数量很大
- 使用欧几里得公式求全毕达哥拉斯三元组
- 所有毕达哥拉斯的三元组都小于500
- 毕达哥拉斯恒等式:如何计算正确的cos符号
- 高德纳-莫里斯-普拉特前缀表生成与通配符
- 寻找毕达哥拉斯三元组:欧几里得公式
- 埃拉斯托特尼筛子
- c++中的毕达哥拉斯定理错误
- 卡普·拉宾的素数和块长度
- 在给定范围内查找毕达哥拉斯三元组的个数
- 查找毕达哥拉斯三元组,总和为 1000,没有错误但不运行