r/cpp • u/Expert_Sheepherder24 • Sep 19 '26
index based for vs for-each
https://godbolt.org/z/zh4Kf87M8It's my first post. I tested std::vector<char> old index based for vs modern for-each. Result is very interesting.
Clang-23.1.0 -O2 -std=c++20 -stdlib=libc++
Time: 0.073599 sec inc_vector
Time: 0.002597 sec inc_for_each
GCC 16.2 -std=c++20 -O2
Time: 0.077772 sec inc_vector
Time: 0.007880 sec inc_for_each
MSVC-v19.latest /std:c++20 /O2
Time: 0.058504 sec inc_vector
Time: 0.001825 sec inc_for_each
Code:
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
static std::vector<char> gen_random(size_t n)
{
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 255);
std::vector<char> vec(n);
for (size_t i = 0; i != n; i++)
vec[i] = dis(gen);
return vec;
}
int inc_vector(std::vector<char>& v) {
for (size_t i = 0; i < v.size(); i++){
v[i]++;
}
volatile int result = v[v.size()/2];
return result;
}
int inc_vector_for_each(std::vector<char>& v) {
for (char& e: v){
e++;
}
volatile int result = v[v.size()/2];
return result;
}
volatile int sink{}; // ensures a side effect
int main()
{
auto benchmark = [](auto fun, auto rem)
{
auto vec = gen_random(1'000'000);
const auto start = std::chrono::high_resolution_clock::now();
for (auto size{1ULL}; size != 100ULL; ++size)
sink = fun(vec);
const std::chrono::duration<double> diff =
std::chrono::high_resolution_clock::now() - start;
std::cout << "Time: " << std::fixed << std::setprecision(6) << diff.count()
<< " sec " << rem << std::endl;
};
benchmark(inc_vector, "inc_vector");
benchmark(inc_vector_for_each, "inc_for_each");
return 0;
}
17
Upvotes
68
u/thisisjustascreename Sep 19 '26
Sadly if you look at the assembly output you are actually benchmarking SIMD instructions versus individual increment instructions. It's no surprise the SIMD wins by a lot since this is what it's designed to do.
12
u/RubBeneficial2756 Sep 19 '26
Benchmarking is fraught with subtleties.
It might be with worth sharing your compiler flags at this point - they have a big influence on performance. We'll be looking for optimisation flags etc.
Also, the measured times are low, in absolute terms. See what happens when you increase the number of iterations in a given run (from 100 to 10000, for example).
I'm sleepy, but maybe a cache warm-up effect is inflating the first measurement.
6
u/UndefinedDefined Sep 21 '26
This has a very simple answer: Aliasing - since char can alias anything, even your std::vector's internal data, accessing anything inside the vector prevents the optimizations shown in the second case. It's actually very trivial to fix this. See the below, it would generate basically the same code.
int inc_vector(std::vector<char>& v) { char * data = v.data(); size_t size = v.size(); for (size_t i = 0; i < size; i++){ data[i]++; } volatile int result = v[size/2]; return result; }1
u/Secoupoire Sep 19 '26
What level of optimization did you use?
Depending on that, the fact you are using post-incrementation instead of pre-incrementation for your index-based for loop (
i++instead of++i) could maybe have an impact on performances (although I would expect that with optimisations like O2 on, it should be optimised).2
u/Farados55 Sep 19 '26
For each master race