How does an optimizing compiler react to a program with nested loops?

Posted by D.Singh on Programmers See other posts from Programmers or by D.Singh
Published on 2014-05-29T10:55:07Z Indexed on 2014/05/31 3:52 UTC
Read the original article Hit count: 528

Say you have a bunch of nested loops.

public void testMethod() {
    for(int i = 0; i<1203; i++){
                //some computation
        for(int k=2; k<123; k++){
                        //some computation
            for(int j=2; j<12312; j++){
                                //some computation
                for(int l=2; l<123123; l++){
                                        //some computation
                    for(int p=2; p<12312; p++){
                            //some computation
                    }
                }
            }
        }
    }

}

When the above code reaches the stage where the compiler will try to optimize it (I believe it's when the intermediate language needs to converted to machine code?), what will the compiler try to do? Is there any significant optimization that will take place?

I understand that the optimizer will break up the loops by means of loop fission. But this is only per loop isn't it? What I mean with my question is will it take any action exclusively based on seeing the nested loops? Or will it just optimize the loops one by one?

If the Java VM complicates the explanation then please just assume that it's C or C++ code.

© Programmers or respective owner

Related posts about java

Related posts about optimization