Hi all,

I recently noted that absolute values of complex arrays are very slow
(about a factor of five compared to some straightforward
implementations). I would like to understand the origin, but could not
trace the code.

Please, consider these timings:

In [67]: z = np.random.random(10000) + 1j*np.random.random(10000)

In [68]: %timeit np.abs(z)**2
215 µs ± 2.09 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [69]: %timeit ((np.sqrt((z.real**2 + z.imag**2)))**2)
78.2 µs ± 193 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [70]: %timeit (z.real**2 + z.imag**2)
40.1 µs ± 196 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [71]: %timeit (z.conjugate()*z).real
43.7 µs ± 230 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)


Even considering the square root and/or additional square, does not
account for the time spent

In [72]: %timeit np.abs(z)
206 µs ± 970 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [86]: %timeit np.sqrt(z.real**2 + z.imag**2)
70.1 µs ± 303 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [73]: %timeit np.sqrt((z.conjugate()*z).real)
105 µs ± 2.23 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

I could not follow the code to understand what calculations are taking
place for `abs()`. Any ideas?

Regards, Juan
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion

Reply via email to