A benchmarking investigation comparing two common C++ vector population strategies: `resize` + assign vs `reserve` + `emplace_back`. Tests are run with both trivial `size_t` and a 4-member struct type, across two scenarios: fresh vectors per iteration and growing vectors over time. Results show `resize` + assign is marginally faster (~1.1x) for small types like `size_t`, while `reserve` + `emplace_back` is faster (~1.2x) for larger structs in the fresh-vector scenario. When vectors grow over time, differences largely even out. The post also touches on a nuance with `if constexpr` outside template contexts and the use of `std::conditional` to switch types at compile time.

5m read timeFrom meetingcpp.com
Post cover image

Sort: