[Numpy-discussion] Slow divide of int64?
Charles R Harris
charlesr.harris@gmail....
Mon Aug 13 23:32:42 CDT 2012
On Sat, Aug 11, 2012 at 6:36 PM, Matthew Brett <matthew.brett@gmail.com>wrote:
> Hi,
>
> A friend of mine just pointed out that dividing by int64 is
> considerably slower than multiplying in numpy:
>
> <script>
> from timeit import timeit
>
> import numpy as np
> import numpy.random as npr
>
> sz = (1024,)
> a32 = npr.randint(1, 5001, sz).astype(np.int32)
> b32 = npr.randint(1, 5001, sz).astype(np.int32)
> a64 = a32.astype(np.int64)
> b64 = b32.astype(np.int64)
>
> print 'Mul32', timeit('d = a32 * b32', 'from __main__ import a32, b32')
> print 'Div32', timeit('d = a32 / b32', 'from __main__ import a32, b32')
> print 'Mul64', timeit('d = a64 * b64', 'from __main__ import a64, b64')
> print 'Div64', timeit('d = a64 / b64', 'from __main__ import a64, b64')
> </script>
>
> gives (64 bit Debian Intel system, numpy trunk):
>
> Mul32 2.71295905113
> Div32 6.61985301971
> Mul64 2.78101611137
> Div64 22.8217148781
>
> with similar values for numpy 1.5.1.
>
> Crude testing with Matlab and Octave suggests they do not seem to have
> this same difference:
>
> >> divtest
> Mul32 4.300662
> Div32 5.638622
> Mul64 7.894490
> Div64 18.121182
>
> octave:2> divtest
> Mul32 3.960577
> Div32 6.553704
> Mul64 7.268324
> Div64 13.670760
>
> (files attached)
>
> Is there something specific about division in numpy that would cause
> this slowdown?
>
>
Numpy is doing an integer divide unless you are using Python 3.x. The
np.true_divide ufunc will speed things up a bit. I'm not sure what
Matlab/Octave are doing for division in this case.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120813/6e7ae5b0/attachment-0001.html
More information about the NumPy-Discussion
mailing list