Nipun Talukdar
Nipun Talukdar

Reputation: 5387

How to preallocate a buffer of certain size in Python?

I have an use case where I will want to combine multiple chunk of buffers into a single buffer. In case of C/C++/Java I will preallocate a buffer whose size is the same as the combined size of the source buffers, then copy the source buffers to it. How can it be done in Python in similar way. I want to avoid creating multiple smaller intermediate buffers that may have a bad impact on performance.

Upvotes: 1

Views: 1869

Answers (1)

Amaury Medeiros
Amaury Medeiros

Reputation: 2233

I think the best way to do what you wanna do is:

initial_value = None # or 0, False, etc.
size = 10 # Or the size of your buffer
buffer = size * [initial_value]

Upvotes: 2

Related Questions