A follow-up benchmarking study comparing two strategies for populating std::vector in C++: resize+assign vs reserve+emplace_back. Using GCC 13.2, resize+assign is consistently faster — up to 1.9x for size_t and 1.6x for a struct of four size_t values. With Clang+libc++, similar gains appear, with up to 2.1x speedup for size_t. The advantage persists even in growing-vector scenarios for smaller data types, though it diminishes for larger structs. The conclusion is that resize+assign is a viable optimization strategy worth testing in code that fills vectors in bulk.

2m read timeFrom meetingcpp.com
Post cover image

Sort: