Using Visual C++ 2008 (express), the following program will finish on my CPU in either 1 second or around 4 seconds according to whether the scanf("%i",&tt) line is quoted or not.

If it is enabled, I enter 0 (representing false), and one would then also expect the program to finish in 1 second, but unfortunately, it takes four.

I did a little research and it turns out that there's a type of loop optimization called "loop unswitching". This means that at compile time, the loop is essentially duplicated, and the if(tt) u=sin((double)n); is removed in one of the versions.

Unfortunately, Visual C++ isn't doing this optimization. Is there a way to force it to?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <math.h>


void test(bool tt) {
	double d=0;	double u=0;
	timeb ti; ftime( &ti );	double l = ti.time*1000.0 + ti.millitm;		// Start the clock.
	for(int n=0; n<1000000000; n++) {
		d += u;

		// The sin math part will never execute, but the 'If' will be looked at every time
		// if the "scanf("%i",&tt)" line was used further below, and this will waste CPU cycles.
		// I want the compiler optimization called "loop unswitching" to
		// duplicate the entire For loop, with and without this line below.
		if(tt) 	u=sin((double)n);		
	}	
	ftime( &ti );	double l2 = ti.time*1000.0 + ti.millitm;	// End the clock!
	printf("Time taken: %f\n",l2-l);
	printf("\n%f\n",d);
}

int main()
{
	bool tt=0;
	scanf("%i",&tt);	// User enters 0 if this line is unquoted.
	test(tt);
	system("PAUSE");
}