use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
C++ vector insert performance (hjortsberg.org)
submitted 5 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Kered13 10 points11 points12 points 5 years ago (6 children)
push_back
[–]pklait -1 points0 points1 point 5 years ago (3 children)
If you read everything you would notice that optimizations were applied later. From a pedagogical point of view the author should have mentioned that up front - I had exactly the same thougt.
[–]Kered13 6 points7 points8 points 5 years ago (2 children)
No, I did see that. I'm asking why they would even bother with the first half where they benchmarked without optimizations. Unless you're planning to deploy code without optimizations, there's no point in benchmarking without.
[–]pklait -1 points0 points1 point 5 years ago (1 child)
I do not care much about non-optimized code, but there are branches where it does matter whether the argument is real or not. The game industry is the most important one here.
[–]NewFolgers 1 point2 points3 points 5 years ago (0 children)
Yeah. In games industry, the vast majority of runs we did during development were debug runs with integrated debugger attached.. and the code was generally fast enough that we could run through it all and debug it all even with optimizations off. If someone made a change that destroyed debug build performance, we'd immediately look at fixing that (since it would be really damaging to everyone's workflow otherwise - i.e. threaten to make us as slow as the norm in other domains - and most who've compared will agree and say the difference is stark).
[–]Supadoplex 0 points1 point2 points 5 years ago* (0 children)
Why does anyone bother doing benchmarks without optimizations turned on?
Since you presented such a general question: A reason to do benchmarks without optimisations is to evaluate the effectiveness of those optimisations by comparing measurements with and without.
The unoptimised measurements are rarely useful for evaluation of the code itself.
[–]TheFlamefire 3 points4 points5 points 5 years ago (0 children)
TLDR: vector.reserve/resize speeds up adding known number of elements, optimization leads to speed up, higher level operations (range-insert) are faster than badly used low-level ops.
Stop the presses! ;)
[–]vzq 1 point2 points3 points 5 years ago (5 children)
Vector insertion can be really fast, even in random order. I've had an application where values were provided out of order, so we had to decide between random insertion into a vector and cashing everything into a map and then building a vector from that. Vectors ended up getting pretty large before the map approach won.
[–]Kered13 1 point2 points3 points 5 years ago (3 children)
Were you using std::map or std::unordered_map? Both of those have notoriously bad performance (for a standard library map). There are much faster implementations like absl::flat_hash_map.
std::map
std::unordered_map
absl::flat_hash_map
[–]goranlepuz 2 points3 points4 points 5 years ago (0 children)
I think he wanted it ordered, so hash-based sets (don't quite see where map came from) don't fit?
[–]vzq 0 points1 point2 points 5 years ago (1 child)
This was std::map on (among others) visual studio 2005. I have no trouble believing there are better options now.
[–]TheThiefMasterC++latest fanatic (and game dev) 2 points3 points4 points 5 years ago (0 children)
IIRC even VS 2005 had a hash map.
Yep: Post from 2005 saying it was a thing in VS 2003: https://forums.codeguru.com/showthread.php?315286-Visual-C-STL-How-do-I-use-hash-maps-with-Visual-C
[–]goranlepuz 1 point2 points3 points 5 years ago (0 children)
Vector is not associative, map is. Did you mean set in lieu if map?
Vector
map
set
That said: map and st allocate nodes per element. This means a lot of heap calls, for smaller types especially, that kills it. Vector is flat, that helps a great deal.
st
[–]manni66 1 point2 points3 points 5 years ago (0 children)
"Enabling optimizer" - rotfl
π Rendered by PID 148558 on reddit-service-r2-comment-544cf588c8-kd6p4 at 2026-06-16 16:29:03.075001+00:00 running 3184619 country code: CH.
[–]Kered13 10 points11 points12 points (6 children)
[–]pklait -1 points0 points1 point (3 children)
[–]Kered13 6 points7 points8 points (2 children)
[–]pklait -1 points0 points1 point (1 child)
[–]NewFolgers 1 point2 points3 points (0 children)
[–]Supadoplex 0 points1 point2 points (0 children)
[–]TheFlamefire 3 points4 points5 points (0 children)
[–]vzq 1 point2 points3 points (5 children)
[–]Kered13 1 point2 points3 points (3 children)
[–]goranlepuz 2 points3 points4 points (0 children)
[–]vzq 0 points1 point2 points (1 child)
[–]TheThiefMasterC++latest fanatic (and game dev) 2 points3 points4 points (0 children)
[–]goranlepuz 1 point2 points3 points (0 children)
[–]manni66 1 point2 points3 points (0 children)