I am reading this recently, which states:
Don’t assume that complicated code is necessarily faster than simple code.
The code is copied as following:
Example, good
// clear expression of intent, fast execution
vector<uint8_t> v(100000);
for (auto& c : v)
c = ~c;
Example, bad
// intended to be faster, but is often slower
vector<uint8_t> v(100000);
for (size_t i = 0; i < v.size(); i += sizeof(uint64_t)) {
uint64_t& quad_word = *reinterpret_cast<uint64_t*>(&v[i]);
quad_word = ~quad_word;
}
I am not sure what the purpose of the bad example is, why is it intended to be faster?
And why is it in fact often slower?