numpy, do multiple operations cause intermediate arrays to be created?

Viewed 70

I'm wondering if these two are equivalent:

import numpy as np

a = np.arange(100000) + 1

# 1
b = 10 * np.log10(a)

# 2
c = np.empty_like(a)
c = np.multiply(10, np.log10(a, out=c), out=c)

More precisely, I wonder if numpy found some way to create array b without having an intermediate array that needs to be allocated and thrown away later for the result of the log operation. Of course, this only matters for very big arrays.

1 Answers

In terms of computing time, they seem to be roughly similar, although the first version is a bit better:

In [1]: import numpy as np

In [2]: a = np.arange(100_000, dtype=float) + 1

In [3]: def f(a):
   ...:     b = 10 * np.log10(a)
   ...:

In [4]: def g(a):
   ...:     c = np.empty_like(a)
   ...:     c = np.multiply(10, np.log10(a, out=c), out=c)
   ...:

In [5]: %timeit f(a)
759 µs ± 52.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [6]: %timeit g(a)
877 µs ± 39.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Related