3 ms·
But you can be sure of when those "sometimes" will be with e.g. std::vector::reserve http://en.cppreference.com/w/cpp/container/vector/reserve http://en.cppref
by cma 13y ago
But you can be sure of when those "sometimes" will be with e.g. std::vector::reserve
http://en.cppreference.com/w/cpp/container/vector/reserve http://en.cppreference.com/w/cpp/container/vector/reserve
If you want realtime guarantees, you have a lot more unpredictable problems, like CPUs having cache.
- pkolaczk 13y agoYep, but exactly the same applies to Java. Just preallocate all your objects statically and you're never going to have GC problems :P
- cma 13y agoIf you don't want the occasional O(n) copy on growth, use an stl list with a custom allocator.
- yongjik 13y agoreserve can be very easily misused. For example, let's assume a function append_10_items_to_vector. Since we're adding 10 items, we may get a better performance if we call reserve() before 10 push_back()s. Right? Not always. If we have a loop that calls our append_10_items_to_vector N times (on the same vector), then your reserve() just transformed the loop into O(N^2) code. Without reserve, it would be O(N). As a rule of thumb, use reserve only if you are really sure that nobody's ever going to add anything more to your vector in the future.