An investigation into performance differences between C++'s traditional `accumulate` and C++23's new `fold_left` algorithm reveals surprising results. Initial benchmarks showed `fold_left` being 1.1x slower, but changing optimization levels reversed this, making it 1.1x faster. The root cause was identified as an accidental copy in the lambda (missing reference parameter). After fixing this, both algorithms performed identically across most optimization levels, though O1 still showed variations due to test ordering and potential cache effects. The analysis demonstrates how small implementation details and compiler optimization levels can significantly impact benchmark results.
Sort: