C++ under the hood – range-based for loops

Este é um artigo publicado originalmente no blog do Tiago Loureiro que pode encontrar aqui (link). Dado ser resultado do envolvimento profissional do Tiago com a PeopleWare, e pelo seu interesse técnico, reproduzimos este e possiveis futuros na “língua-franca” do desenvolvimento em TI.

This may be your bread and butter but today I fell into a rabbit hole of optimization questions and found a few things that I wanted to share with you. As someone who did Assembly programming “back in the days” (haven’t touched it in a while now) I am always very distrustful of modern, ease-of-use, generalizations and high(er)-level approaches to foundational concepts. As such whenever presented with something like:

std::vector<int> a = {6, 2, 3, 8, 1, 4};

If I want to go about and get the lowest value I’d typically avoid doing:

std::min_element(a.begin(), a.end()); 

and opt for a traditional for on n-1 elements:

int minVal = a[0]; for (size_t i = 1; i < a.size(); ++i) { if (a[i] < minVal) minVal = a[i]; }

However, since C++ 11 one could also do something like:

int minVal = a[0]; for (int v : a) { if (v < minVal) minVal = v; }

And this would put me off immediately as, without looking, I’d assume worse performance out of lack of knowledge of the proposed abstraction (albeit being cleanest to read – the typical benefits of modern revisions). However today I dug a bit deeper and found that for the majority of cases it just expands to this for the compiler:

auto && __range = a;
auto __begin = std::begin(__range);
auto __end = std::end(__range);
for (; __begin != __end; ++__begin)
{
auto v = *__begin;
if (v < minVal) minVal = v;
}

Therefore, marginal difference (aka pretty much the same for all cases where you don’t need an index).

Now, could it ever be a difference whilst using a range based for? I read that debug builds don’t inline so aggressively, and therefore one could potentially see some better performance on index-based iterations.

So where does this leaves us? For the majority of cases where you have simple data types, the sample data is not the size of the universe and there’s a simple atomic way to do comparison, then just go with what you feel like it works best for you and your team, as performance differences will be potentially negligible.

Deixe um comentário