Reputation: 8636
Why do both of these loops take the same amount of time, shouldn't the if statement make the first single loop much slower?
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int counter = 0;
#ifdef single
cout << "one for loop\n";
for(int i =0;i<10000000;i++)
{
if(i != 50000) counter+=i;
}
#else
cout << "two loops\n";
for(int i = 0;i<50000;i++)
{
counter+=i;
}
for(int i = 50001;i<10000000;i++)
{
counter+=i;
}
#endif
return 0;
}
I get, for time, the following results: time ./test one for loop
real 0m0.004s user 0m0.001s sys 0m0.002s
AND
two loops
real 0m0.004s user 0m0.001s sys 0m0.002s
I did some research and it said it is cause of branching but I'm not sure if that is the only reason, it was compiled as
g++ -std=c++11 -O3 test.cpp -Dsingle -o test
Upvotes: 0
Views: 617
Reputation: 154
I think the fact that you're not consuming the variable counter
is making the compiler to optimize it out. Try putting a cout << counter << endl;
before return 0;
.
Upvotes: 0
Reputation: 118435
The overall loop barely puts a dent, in modern, multi-gigahertz CPUs, enormous level 1 & 2 CPU caches, and blazingly fast memory.
You're going to have to come up with two alternative execution paths that have way, way, more substantive differences, than a single, puny if() statement like that, before you can see any kind of a difference.
Think of it this way: modern CPUs have all sorts of crazy pipelining, that make it possible for the CPU to execute not just the current instruction, but several instructions after that. All at the same time. When the current instruction is the loop increment, the CPU has already likely figured out what the next if() will evaluate to, and what to do after that. All during the same set of clock cycles.
Upvotes: 2
Reputation: 241861
You'll get a more accurate benchmark if you actually do something with counter
(like output it). Otherwise, gcc will figure out that the computation of counter
is unnecessary, and so it won't bother wasting time doing it.
Upvotes: 1