python - 哪种方法更快,为什么 np.sum(arr) vs arr.sum()?
问题描述
哪种方法更快?好像他们不是一样的吗?
start = time.time()
arr = np.array([1,2,3,4,5,6,7,8,9,0,12])
total_price = np.sum(arr[arr < 7])* 2.14
print(total_price)
print('Duration: {} seconds'.format(time.time() - start))
start = time.time()
arr = np.array([1,2,3,4,5,6,7,8,9,0,12])
total_price = (arr[arr<7]).sum()* 2.14
print(total_price)
print('Duration: {} seconds'.format(time.time() - start))
在一次又一次地运行代码时,它们都给出了不同的执行时间。有时前一种方法更快,有时更晚。
解决方案
的代码np.sum
是
def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
initial=np._NoValue, where=np._NoValue):
if isinstance(a, _gentype):
# 2018-02-25, 1.15.0
warnings.warn(
"Calling np.sum(generator) is deprecated, and in the future will give a different result. "
"Use np.sum(np.fromiter(generator)) or the python sum builtin instead.",
DeprecationWarning, stacklevel=3)
res = _sum_(a)
if out is not None:
out[...] = res
return out
return res
return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
initial=initial, where=where)
所以它会检查一些参数,然后将任务传递给add.reduce
. 该sum
方法是“内置”的,但在编译代码中必须执行类似的操作。
在这些测试中,计算时间本身足够小,调用方法会产生影响:
In [607]: timeit np.sum(np.arange(1000))
15.4 µs ± 42.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [608]: timeit np.arange(1000).sum()
12.2 µs ± 29.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [609]: timeit np.add.reduce(np.arange(1000))
9.19 µs ± 17.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
numpy
有许多这样的函数/方法对。使用最方便的 - 并且在您的代码中看起来最漂亮!
推荐阅读
- razor - 剃刀视图中无法识别“模型”一词
- python - 如何将大数据流式传输到 Google Cloud Storage?
- python - 当系数在 Python 中以 26 为模时,求行列式和逆矩阵
- java - Tomcat9 上的 java.lang.LinkageError:java.net.URLClassLoader 尝试了 DefaultTokenServices 的重复类定义
- bash - 比较新旧目录并重新压缩更改的文件
- reactjs - 在axios回调TypeError中反应typeerror:this.setState不是函数
- javascript - 重定向到同一个组件重新渲染
- c - C 程序未能将先前读取的内容插入文件内容
- r - 如何在同一轴上绘制两个单独的箱线图?
- python - 带有空格的回文递归和函数不起作用