Reputation: 19
I was playing around in code and i thought of switching the for loops wondering about which would be the best way to implement this loop when it comes to storing data? I'm curious if anyone has an opinion as to which would they prefer to choose and why even when they give the same result, i am assuming it has to do with performance but I am not sure how . Thank you.
char data[1024ull*1024][1024];
for (uint64_t i = 0, endi = 1024ull*1024; i < endi; ++i) {
for (uint64_t j = 0, endj = 1024; j < endj; ++j) {
work(data[i][j]);
}
}
OR
char data[1024ull*1024][1024];
for (uint64_t j = 0, endj = 1024; j < endj; ++j) {
for (uint64_t i = 0, endi = 1024ull*1024; i < endi; ++i) {
work(data[i][j]);
}
}
Upvotes: 0
Views: 96
Reputation: 10936
Neither. Flatten the array instead:
constexpr std::size_t gibibytes = 1024 * 1024 * 1024;
constexpr std::size_t data_size = 1 * gibibytes;
uint8_t data[data_size];
for (std::size_t i = 0u; i < data_size; ++i) {
work(data[i]);
}
If you don't plan on modifying the array size in place (I don't know what work
does...) you can simplify the array with a ranged-base for loop
:
for (auto& byte : data) {
work(byte);
}
Flattening the array makes it so you don't have to worry about cache misses at all.
Upvotes: 2
Reputation: 217245
The first loop should have better cache locality, and can even be written simpler with for-range:
char data[1024ull*1024][1024];
for (auto& arr : data) {
for (auto& e : arr) {
work(elem);
}
}
Upvotes: 2