Here's a general principle: when a library provides both do_x_once
and do_x_in_batch
, then the latter should be at least as fast as calling do_x_once
in a simple loop. If it isn't, then the library is very badly implemented since a simple loop is enough to get a faster version. Often, such batch functions/methods can perform additional optimizations because they have knowledge of data structure internals.
So, insert
should be at least as fast as push_back
in a loop. In this particular case, a smart implementation of insert
can do a single reserve
for all the elements you want to insert. push_back
would have to check the vector's capacity every time. Don't try to outsmart the library :)