Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to reduce memory usage when splitting array

I have an array that I want to split up in two halves. Because of symmetry I am only interested in keeping the left half of the array.

I can split the array in half by saying:

[a,b] = numpy.split(c,2)

where c is also an array.

Is there a way to only return the 'a' array, or alternatively removing the 'b' array from memory immediately after splitting the array?

like image 680
Jonny Avatar asked Apr 28 '26 23:04

Jonny


1 Answers

You can copy the first half with

a = x[len(x)//2:].copy()

this would need to allocate the copy and move the content (thus temporarily needing 1.5 times the memory)

Otherwise you can just say

a = x[len(x)//2:]

to get a reference to the first half, but the other part will not be removed from memory

like image 55
6502 Avatar answered May 01 '26 13:05

6502



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!