# [Numpy-discussion] timing of abs(complex)

fiolj fiolj at yahoo.com
Mon Apr 19 11:22:31 EDT 2021

```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.

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

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

In : %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 : %timeit (z.real**2 + z.imag**2)
40.1 µs ± 196 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In : %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 : %timeit np.abs(z)
206 µs ± 970 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In : %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 : %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
```