Numpy除法和Python除法之间的区别? [英] Differences between Numpy divide and Python divide?

查看:213
本文介绍了Numpy除法和Python除法之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy.divide 和Python斜杠/运算符之间有何异同?据我所知,它们的行为相同,均实现了按元素划分. Numpy文档提到:

What are the similarities and differences between numpy.divide and the Python slash / operator? As far as I can tell they behave the same, both implementing an element-wise division. The Numpy documentation mentions:

numpy.divide(x1,x2)...就数组广播而言,等效于x1/x2. ...

numpy.divide(x1, x2) ... Equivalent to x1 / x2 in terms of array-broadcasting. ...

暗示np.divide(x1,x2)并不完全等同于x1/x2. 我已经运行了以下代码片段来比较它们的速度:

Implying that np.divide(x1, x2) is not completely equivalent to x1 / x2. I have run the following snippet to compare their speed:

import numpy as np
import time

a = np.random.rand(10000, 10000)
b = np.random.rand(10000, 10000)

tic = time.time()
c = a / b
toc = time.time()
print("Python divide took: ", toc - tic)

tic = time.time()
c = np.divide(a, b)
toc = time.time()
print("Numpy divide took: ", toc - tic)

Python分隔通常运行得更快,这使我相信Numpy分隔实现了一些额外的功能.

It appears that the Python divide generally runs faster which leads me to believe the Numpy divide implements some additional bells and whistles.

非常感谢您的帮助!

推荐答案

此处似乎没有任何实际的性能差异.

There doesn't appear to be any actual performance difference here.

当我运行您的代码并交换两个测试时,以秒为准的速度更快.

When I run your code, and swap the two tests, whichever one comes second goes faster.

当我使用timeit进行适当的基准测试时,它们花费的时间大约相同(/ 540ms与divide 539ms).

When I use timeit for proper benchmarking, they take about the same time (540ms for / vs. 539ms for divide).

我的猜测是,您所测量的差异是对数组进行malloc的时间-第一个需要这样做,第二个可以重复使用刚刚释放的内存.

My guess would be that the difference you measured was the time to malloc the array—the first one needs to do that, the second one can reuse the memory that just got freed.

但是让我们看一下源代码. generate_umath.py 中的代码创建实际代码,并将相同的Ufunc(命名为numpy.core.umath.divide)分配给np.floor_dividenp.ndarrayPyNumber_FloorDivide插槽. (如果您想知道为什么在使用divide/而不是floor_divide//时我抬起floor_divide,请参阅后面的注释,其中它删除了Python 3的divide,因为它将其别名为true_divide.)IIRC,实际代码是对类型和大小的切换,最终导致最终出现在

But let's look at the source. The code in generate_umath.py creates the actual code, and it's assigning the same Ufunc (named numpy.core.umath.divide) to np.floor_divide and to the PyNumber_FloorDivide slot for np.ndarray. (If you're wondering why I looked up floor_divide when you're using divide and / instead of floor_divide and //, see the comment later where it deletes divide for Python 3 because it will be aliased it to true_divide.) IIRC, the actual code is a switch on types and sizes that ultimately ends up in one of the loop templates in loops.c.src.

因此,除了显式的Ufunc包装器代码与内置的method-wrapper包装器代码(对于任何不是很小的数组都无关紧要)之间的差异之外,它们最终都位于同一位置.

So, beyond the differences in explicit Ufunc wrapper code vs. builtin method-wrapper wrapper code (which is going to be irrelevant for any array that isn't tiny), they end up in the same place.

这篇关于Numpy除法和Python除法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆