What -O and -O2 optimization do?
Posted: Tue May 27, 2008 7:23 pm
I wonder what kind of optimization does GNU C/C++ do with -O1 and -O2. I do some googling but still can't get the clear idea about the optimization detail.
If you compile the code using GCC with optimization (-O) and run it, you will get the result in instant! no matter how many nested loop are there. This kinda amazing 
I wonder how do they work (the optimization)?
and why most of (if not all) online-judges that I know use -O2 optimization? is there any reason for this?
Code: Select all
int p = 0;
for ( int i = 0; i < 1000000000; i++ )
for ( int j = 0; j < 1000000000; j++ )
for ( int k = 0; k < 1000000000; k++ ) p += i * 5;

I wonder how do they work (the optimization)?
and why most of (if not all) online-judges that I know use -O2 optimization? is there any reason for this?