if we have if statements with declarations of variables like this:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
int res = 0;
clock_t begin = clock();
for(int i=0; i<500500000; i++) {
if(i%2 == 0) {int fooa; fooa = i;}
if(i%2 == 0) {int foob; foob = i;}
if(i%2 == 0) {int fooc; fooc = i;}
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << elapsed_secs << endl;
return 0;
}
the result is:
1.44
Process returned 0 (0x0) execution time : 1.463 s
Press any key to continue.
but, if it is:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
int res = 0;
clock_t begin = clock();
for(int i=0; i<500500000; i++) {
res++;
res--;
res++;
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << elapsed_secs << endl;
return 0;
}
the result is:
3.098
Process returned 0 (0x0) execution time : 3.115 s
Press any key to continue.
why does adding or subtracting takes more time to run than a if statement with a variable declaration?