r/cpp • u/Little-Reflection986 • Feb 16 '26
Favorite optimizations ??
I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!
135
Upvotes
View all comments
19
u/Wargon2015 Feb 16 '26
std::vector::reserve
The first few insertions are surprisingly expensive due to reallocation. Its a trade-off but if you know roughly how many elements will be inserted, reserving can safe quite a bit of overhead.