Python的总和与NumPy的numpy.sum [英] Python's sum vs. NumPy's numpy.sum
问题描述
使用Python的本机sum
函数和NumPy的numpy.sum
在性能和行为上有什么区别? sum
适用于NumPy的数组,而numpy.sum
适用于Python列表,它们都返回相同的有效结果(未经测试的边缘情况,如溢出),但类型不同.
What are the differences in performance and behavior between using Python's native sum
function and NumPy's numpy.sum
? sum
works on NumPy's arrays and numpy.sum
works on Python lists and they both return the same effective result (haven't tested edge cases such as overflow) but different types.
>>> import numpy as np
>>> np_a = np.array(range(5))
>>> np_a
array([0, 1, 2, 3, 4])
>>> type(np_a)
<class 'numpy.ndarray')
>>> py_a = list(range(5))
>>> py_a
[0, 1, 2, 3, 4]
>>> type(py_a)
<class 'list'>
# The numerical answer (10) is the same for the following sums:
>>> type(np.sum(np_a))
<class 'numpy.int32'>
>>> type(sum(np_a))
<class 'numpy.int32'>
>>> type(np.sum(py_a))
<class 'numpy.int32'>
>>> type(sum(py_a))
<class 'int'>
我认为我的实际问题是,在Python整数列表上使用numpy.sum
会比使用Python自己的sum
快吗?
I think my practical question here is would using numpy.sum
on a list of Python integers be any faster than using Python's own sum
?
此外,使用Python整数与标量numpy.int32
有什么关系(包括性能)?例如,对于a += 1
,如果a
的类型是Python整数或numpy.int32
,是否在行为或性能上有所不同?我很好奇,如果将NumPy标量数据类型(例如numpy.int32
)用于在Python代码中增加或减少很多的值,是否更快.
Additionally, what are the implications (including performance) of using a Python integer versus a scalar numpy.int32
? For example, for a += 1
, is there a behavior or performance difference if the type of a
is a Python integer or a numpy.int32
? I am curious if it is faster to use a NumPy scalar datatype such as numpy.int32
for a value that is added or subtracted a lot in Python code.
为澄清起见,我正在进行生物信息学模拟,其中一部分包括将多维numpy.ndarray
分解为单个标量和,然后对其进行额外处理.我正在使用Python 3.2和NumPy 1.6.
For clarification, I am working on a bioinformatics simulation which partly consists of collapsing multidimensional numpy.ndarray
s into single scalar sums which are then additionally processed. I am using Python 3.2 and NumPy 1.6.
提前谢谢!
推荐答案
我很好奇并定时了. numpy.sum
对于numpy数组似乎快得多,但在列表上要慢得多.
I got curious and timed it. numpy.sum
seems much faster for numpy arrays, but much slower on lists.
import numpy as np
import timeit
x = range(1000)
# or
#x = np.random.standard_normal(1000)
def pure_sum():
return sum(x)
def numpy_sum():
return np.sum(x)
n = 10000
t1 = timeit.timeit(pure_sum, number = n)
print 'Pure Python Sum:', t1
t2 = timeit.timeit(numpy_sum, number = n)
print 'Numpy Sum:', t2
x = range(1000)
时的结果:
Pure Python Sum: 0.445913167735
Numpy Sum: 8.54926219673
x = np.random.standard_normal(1000)
时的结果:
Pure Python Sum: 12.1442425643
Numpy Sum: 0.303303771848
我正在使用Python 2.7.2和Numpy 1.6.1
I am using Python 2.7.2 and Numpy 1.6.1
这篇关于Python的总和与NumPy的numpy.sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!