NumPy 的数学函数是否比 Python 的快?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3650194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are NumPy's math functions faster than Python's?
提问by Mermoz
I have a function defined by a combination of basic math functions (abs, cosh, sinh, exp, ...).
我有一个由基本数学函数(abs、cosh、sinh、exp等)组合定义的函数。
I was wondering if it makes a difference (in speed) to use, for example,
numpy.abs()instead of abs()?
我想知道使用,例如,
numpy.abs()而不是abs()?
采纳答案by Eric O Lebigot
Here are the timing results:
以下是计时结果:
lebigot@weinberg ~ % python -m timeit 'abs(3.15)'
10000000 loops, best of 3: 0.146 usec per loop
lebigot@weinberg ~ % python -m timeit -s 'from numpy import abs as nabs' 'nabs(3.15)'
100000 loops, best of 3: 3.92 usec per loop
numpy.abs()is slower than abs()because it also handles Numpy arrays: it contains additional code that provides this flexibility.
numpy.abs()慢于abs()因为它还处理 Numpy 数组:它包含提供这种灵活性的附加代码。
However, Numpy isfast on arrays:
但是,Numpy在数组上很快:
lebigot@weinberg ~ % python -m timeit -s 'a = [3.15]*1000' '[abs(x) for x in a]'
10000 loops, best of 3: 186 usec per loop
lebigot@weinberg ~ % python -m timeit -s 'import numpy; a = numpy.empty(1000); a.fill(3.15)' 'numpy.abs(a)'
100000 loops, best of 3: 6.47 usec per loop
(PS: '[abs(x) for x in a]'is slower in Python 2.7 than the better map(abs, a), which is about 30?% faster—which is still much slower than NumPy.)
(PS:'[abs(x) for x in a]'在 Python 2.7 中比 Better 慢map(abs, a),后者快了大约 30?%——仍然比 NumPy 慢得多。)
Thus, numpy.abs()does not take much more time for 1000 elements than for 1 single float!
因此,numpy.abs()1000 个元素不会比 1 个单浮点花费更多的时间!
回答by BatchyX
You should use numpy function to deal with numpy's types and use regular python function to deal with regular python types.
您应该使用 numpy 函数来处理 numpy 的类型,并使用常规的 python 函数来处理常规的 python 类型。
Worst performance usually occurs when mixing python builtins with numpy, because of types conversion. Those type conversion have been optimized lately, but it's still often better to not use them. Of course, your mileage may vary, so use profiling tools to figure out.
由于类型转换,当将 python 内置函数与 numpy 混合时,通常会出现最差的性能。这些类型转换最近得到了优化,但通常最好不要使用它们。当然,您的里程可能会有所不同,因此请使用分析工具来弄清楚。
Also consider the use of programs like cython or making a C module if you want to optimize further your program. Or consider not to use python when performances matters.
如果你想进一步优化你的程序,还可以考虑使用像 cython 这样的程序或制作 C 模块。或者考虑在性能很重要时不使用 python。
but, when your data has been put into a numpy array, then numpy can be really fast at computing bunch of data.
但是,当您的数据被放入一个 numpy 数组时,numpy 可以非常快速地计算一堆数据。
回答by colinfang
In fact, on numpy array
事实上,在 numpy 数组上
built in abscalls numpy's implementation via __abs__, see Why built-in functions like abs works on numpy array?
内置abs调用 numpy 的实现__abs__,请参阅为什么像 abs 这样的内置函数适用于 numpy 数组?
So, in theory there shouldn't be much performance difference.
所以,理论上应该不会有太大的性能差异。
import timeit
x = np.random.standard_normal(10000)
def pure_abs():
return abs(x)
def numpy_abs():
return np.absolute(x)
n = 10000
t1 = timeit.timeit(pure_abs, number = n)
print 'Pure Python abs:', t1
t2 = timeit.timeit(numpy_abs, number = n)
print 'Numpy abs:', t2
Pure Python abs: 0.435754060745
Numpy abs: 0.426516056061

