Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a very large array in Python

Tags:

python

arrays

For image processing, I need an array (array type, not numpy array) for 2 million 32 bit words. If I use something like:

tb = array.array( 'i', ,(0,)*2000000)

it requires 126 msec. It's large and I don't even need to initialize the array. I don't know Python internals but I assume that the statement generate tons on malloc() (memory allocator) and free() (memory deallocator).

Is there another way to create a very large Python array?

like image 651
user3435121 Avatar asked May 02 '26 08:05

user3435121


1 Answers

This is much faster, because it doesn't create a long, temporary tuple:

tb = array.array('i', (0,)) * 2000000
like image 200
pts Avatar answered May 04 '26 22:05

pts



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!