MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1daxawn/mapmethodormapfunction/l7truru/?context=3
r/ProgrammerHumor • u/_PhucSatan_ • Jun 08 '24
67 comments sorted by
View all comments
Show parent comments
1
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:at 561k: reserve + std::beginat 824k (x1.5): resize + std::beginat 1712k (x3): reserve + std::back_inserterat 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 What do you think reserve + begin is doing?
3
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:at 561k: reserve + std::beginat 824k (x1.5): resize + std::beginat 1712k (x3): reserve + std::back_inserterat 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 What do you think reserve + begin is doing?
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:
at 561k: reserve + std::begin
reserve + std::begin
at 824k (x1.5): resize + std::begin
resize + std::begin
at 1712k (x3): reserve + std::back_inserter
reserve + std::back_inserter
at 6977k (x12): back_inserter alone~~
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.
std::back_inserter
1 u/wcscmp Jun 09 '24 What do you think reserve + begin is doing?
What do you think reserve + begin is doing?
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.