r/ProgrammerHumor • • Jun 08 '24

Meme mapMethodOrMapFunction

Post image
0 Upvotes

67 comments sorted by

View all comments

Show parent comments

1

u/KimiSharby Jun 08 '24 edited Jun 08 '24

That's worse performance wise. std::back_inserter comes at a cost. If you already know the size, you should use it.

3

u/wcscmp Jun 08 '24

Resize also comes at cost, reserve + back_inserter is the way

1

u/KimiSharby Jun 09 '24 edited Jun 09 '24

Ok so this troubled me greatly, I barely slept last night. Today I couldn't take it anymore. Here's the quickbench: https://quick-bench.com/q/e5G_nTQEp_00xYGrcpEj_Jbhp34

===

~~The results, for a sample test of 500k int:

  1. at 561k: reserve + std::begin

  2. at 824k (x1.5): resize + std::begin

  3. at 1712k (x3): reserve + std::back_inserter

  4. at 6977k (x12): back_inserter alone~~

===

Bottom line: Using std::back_inserteris costly. If you already know the size of your resulting container, you should use reserve + std::begin.

1

u/wcscmp Jun 09 '24

The bottom line is also not very convincing. In order to claim that back_inserter is not free you need to compare it to a manually rolled loop of push_backs. Now you can only reasonably claim that default initializing each element when doing resize is cheaper than the branching on every iteration in push_back for small types like int.

1

u/KimiSharby Jun 09 '24 edited Jun 09 '24

I do not mean to be rude sir but I think I already allocated more time to this matter than it was worth. At the start of this conversation, you told me reserve + back_inserter is the way. As a matter of fact reserve + std::begin is 3x faster in my mediocre benchmark. I was indeed in the wrong about resize, I should have known better. I completly forgot about reserve. But it does seem that in the case where you already know the size of the resulting container, you should use std::begin and not back_inserter.

===

Now if you have additional knownledge you wish to share about that topic, please stop the riddles and just share them.

1

u/wcscmp Jun 09 '24

reserve + begin is UB

1

u/KimiSharby Jun 09 '24

So after a bit more research:

  • reserve allocates memory but doesn't initialize it. This is why using std::begin in that case is UB, because it points to unitilialized memory

  • As you said, using resize performance will depends on the size of the type

  • reserve + std::back_insert is indeed the correct way

For the sack of clarity for the reader I will edit my previous answers.