[Numpy-discussion] Why is numpy.abs so much slower on complex64 than complex128 under windows 32-bit?

Henry Gomersall heng at cantab.net
Tue Apr 10 12:43:14 EDT 2012


On 10/04/2012 17:57, Francesc Alted wrote:
>> I'm using numexpr in the end, but this is slower than numpy.abs under linux.
> Oh, you mean the windows version of abs(complex64) in numexpr is slower
> than a pure numpy.abs(complex64) under linux?  That's weird, because
> numexpr has an independent implementation of the complex operations from
> NumPy machinery.  Here it is how abs() is implemented in numexpr:
>
> static void
> nc_abs(cdouble *x, cdouble *r)
> {
>       r->real = sqrt(x->real*x->real + x->imag*x->imag);
>       r->imag = 0;
> }
>
> [as I said, only the double precision version is implemented, so you
> have to add here the cost of the cast too]
hmmm, I can't seem to reproduce that assertion, so ignore it.

> Hmm, considering all of these facts, it might be that sqrtf() on windows
> is under-performing?  Can you try this:
>
> In [68]: a = numpy.linspace(0, 1, 1e6)
>
> In [69]: b = numpy.float32(a)
>
> In [70]: timeit c = numpy.sqrt(a)
> 100 loops, best of 3: 5.64 ms per loop
>
> In [71]: timeit c = numpy.sqrt(b)
> 100 loops, best of 3: 3.77 ms per loop
>
> and tell us the results for windows?

In [18]: timeit c = numpy.sqrt(a)
100 loops, best of 3: 21.4 ms per loop

In [19]: timeit c = numpy.sqrt(b)
100 loops, best of 3: 12.5 ms per loop

So, all sensible there it seems.

Taking this to the next stage...

In [95]: a = numpy.random.randn(256,2048) + 1j*numpy.random.randn(256,2048)

In [96]: b = numpy.complex64(a)

In [97]: timeit numpy.sqrt(a*numpy.conj(a))
10 loops, best of 3: 61.9 ms per loop

In [98]: timeit numpy.sqrt(b*numpy.conj(b))
10 loops, best of 3: 27.2 ms per loop

In [99]: timeit numpy.abs(a)  # for comparison
10 loops, best of 3: 30 ms per loop

In [100]: timeit numpy.abs(b)  # and again (slow slow slow)
1 loops, best of 3: 153 ms per loop

I can confirm the results are correct. So, it really is in numpy.abs.

> PD: if you are using numexpr on windows, you may want to use the MKL
> linked version, which uses the abs of MKL, that should have considerably
> better performance.
I'd love to - I presume this would mean me buying an MKL license? If 
not, where do I find the MKL linked version?

Cheers,

Henry



More information about the NumPy-Discussion mailing list