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.
Asked
Active
Viewed 1,458 times
1
-
As "buffer" you mean an array? Please provide more information – Amaury Medeiros Apr 29 '15 at 14:04
-
1possible duplicate of [Reserve memory for list in Python?](http://stackoverflow.com/questions/537086/reserve-memory-for-list-in-python) – Torxed Apr 29 '15 at 14:07
-
I posted the answer. Although I think it's not a very good practice in python to allocate the size of the array beforehand. – Amaury Medeiros Apr 29 '15 at 14:08
1 Answers
2
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]

Amaury Medeiros
- 2,093
- 4
- 26
- 42