Skip to content

Instantly share code, notes, and snippets.

@dannas
Created April 10, 2020 10:27
Show Gist options
  • Select an option

  • Save dannas/29a2239d5d964d8c53413160aa9c0530 to your computer and use it in GitHub Desktop.

Select an option

Save dannas/29a2239d5d964d8c53413160aa9c0530 to your computer and use it in GitHub Desktop.
#include <vector>
#include <numeric>
using namespace std;
// Compare lowering of loops for -Og, -O2 and -O3
// https://godbolt.org/z/mNtLqi
int f1(const vector<int> &v) {
int sum = 0;
for (int val : v) {
sum += val;
}
return sum;
}
int f2(const vector<int> &v) {
int sum = 0;
for (auto p = begin(v); p != end(v); p++) {
sum += *p;
}
return sum;
}
int f3(const vector<int> &v) {
int sum = 0;
for (size_t i = 0; i < v.size(); i++) {
sum += v[i];
}
return sum;
}
int f4(const vector<int> &v) {
return accumulate(begin(v), end(v), 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment