Thanks to "Ry-", this is probably a duplicate of Empty loop is slower than a non-empty one in C
I wrote following c++ code:
#include <iostream>
#include <ctime>
void step() {
return;
}
void loop(int n) {
for (int i = 0; i < n; i++) {
step();
}
return;
}
int main(int argc, char** argv) {
clock_t tic = clock();
loop(int(1e9));
clock_t toc = clock();
std::cout << "elapsed in " << double(toc-tic)/CLOCKS_PER_SEC << " " << "seconds" << std::endl;
return 0;
}
Then I compiled it with -O0 flag. The printout is
elapsed in 1.23771 seconds
If I remove the "step" function, leaving an empty loop, the printout becomes
elapsed in 2.72871 seconds
It's curious to see that an empty loop is slower than a loop that "does nothing". Does anyone know what's happening here?