· 01:22
Welcome to today's podcast! In this episode, we explore the intriguing debate between using raw loops and modern approaches in C++, particularly focusing on performance and readability.
Sandor Dargo recently shared his experiences on a new project, where he noticed a raw loop that sparked his curiosity. Traditionally, many have adhered to Sean Parent’s perspective from 2013 about avoiding raw loops. However, Sandor saw an opportunity to leverage C++20 features, specifically std::ranges::transform
, to make the code cleaner.
He highlights, "We have no more raw loops, no more initialized then modified vectors, and the result is the same." But the crux of the matter came when he faced a challenge from a colleague regarding whether using ranges results in unnecessary copying of elements.
Interestingly, benchmarks revealed a twist: the enhanced raw loop version outperformed the range version by 20% on Clang and 10% on GCC. Sandor concludes, "If this happens to be a bottleneck, use the emplace_back version without hesitation."
In summary, the choice between readability and performance isn't black and white. As Sandor succinctly puts it, "Use the version that you find the most readable," because often, the differences in performance can be negligible depending on the context of use.
Thanks for tuning in!
Link to Article
Listen to jawbreaker.io using one of many popular podcasting apps or directories.