Differences between Numpy divide and Python divide? Differences between Numpy divide and Python divide? numpy numpy

Differences between Numpy divide and Python divide?


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.

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

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.


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.

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.