Memory vs Compute
struct Elem {
int input;
int cached_result; // precomputed
};
std::vector<Elem> arr(10'000'000);
for (auto &e : arr) {
sum += e.cached_result;
}
^ This is Faster?
struct Elem {
int input;
};
std::vector<Elem> arr(10'000'000);
for (auto &e : arr) {
int x = e.input;
x = x * 7 + 13;
x = x ^ (x >> 3);
x = x * 31 + 7;
x = x ^ (x >> 5);
x = x * 127 + 63;
x = x ^ (x >> 7);
x = x * 17 + 5;
sum += x;
}
^ This is Faster?
* The benchmark is run under AMD Ryzen 9.
* For the full benchmark code, please refer here.
* For illustration purposes only, see FAQ for more details.