Memory use during recursion in Python
Question:
I’ve implemented a recursive function which takes as parameter a numpy array.
Here the simplified version:
def rec(arr):
rec(arr[indices])
In every recursive call I use a part of the array indexed by some indices.
My question is about the memory load: how does python handle this? Does it makes a copy of the array at each call or not?
Answers:
It depends on the nature of indices
. If it’s a slice, there is no copy. If, on the other hand, you’re using fancy indexing, then a copy is made.
I recommend reading Copies and Views in the NumPy tutorial (even though the section does not cover fancy indexing).
I’ve implemented a recursive function which takes as parameter a numpy array.
Here the simplified version:
def rec(arr):
rec(arr[indices])
In every recursive call I use a part of the array indexed by some indices.
My question is about the memory load: how does python handle this? Does it makes a copy of the array at each call or not?
It depends on the nature of indices
. If it’s a slice, there is no copy. If, on the other hand, you’re using fancy indexing, then a copy is made.
I recommend reading Copies and Views in the NumPy tutorial (even though the section does not cover fancy indexing).