为什么numpy.sum建立新生成器比仅使用范围更快?

时间:2016-07-08 07:51:30

标签: python python-3.x numpy

这让我感到惊讶。我一直在测试表演。

In [1]: import numpy as np

In [2]: %timeit a = np.sum(range(100000))
Out[2]: 100 loops, best of 3: 16.7 ms per loop

In [3]: %timeit a = np.sum([range(100000)])
Out[3]: 100 loops, best of 3: 16.7 ms per loop

In [4]: %timeit a = np.sum([i for i in range(100000)])
Out[4]: 100 loops, best of 3: 12 ms per loop

In [5]: %timeit a = np.sum((i for i in range(100000)))
Out[5]: 100 loops, best of 3: 8.43 ms per loop

我试图了解内部工作以及学习如何概括以获得最佳实践。为什么4(构建新的发电机)优于1?

我理解为什么创建列表需要更多时间。但同样,为什么3优于2?为什么2不比1差?列表是否以1?

构建

我正在使用from numpy import *

1 个答案:

答案 0 :(得分:2)

运行相同的代码,我得到了这些结果(Python 3.5.1):

%timeit a = sum(range(100000))
100 loops, best of 3: 3.05 ms per loop

%timeit a = sum([range(100000)])
>>> TypeError: unsupported operand type(s) for +: 'int' and 'range'

%timeit a = sum([i for i in range(100000)])
100 loops, best of 3: 8.12 ms per loop

%timeit a = sum((i for i in range(100000)))
100 loops, best of 3: 8.97 ms per loop

现在使用numpy的sum()实现:

from numpy import sum

%timeit a = sum(range(100000))
10 loops, best of 3: 19.7 ms per loop

%timeit a = sum([range(100000)])
10 loops, best of 3: 20.2 ms per loop

%timeit a = sum([i for i in range(100000)])
100 loops, best of 3: 16.2 ms per loop

%timeit a = sum((i for i in range(100000)))
100 loops, best of 3: 9.27 ms per loop

通过使用from numpy import *(或from numpy import sum),您正在破坏Python的内置sum()功能。

查看this SO问题,讨论两种实现之间的性能比较。