Preallocate the vector if you can guesstimate the size
Use a vector library that won’t reallocate the entire vector on every single addition (like Rust, whose Vec doubles in size every time it runs out of space)
Memory is fairly cheap. Allocation time not so much.
Which is why you should:
Vec
doubles in size every time it runs out of space)Memory is fairly cheap. Allocation time not so much.