Itamar
Itamar

Reputation: 11

Unrolling loop for a given number

Is there any function/utility/tool that unroles a loop (of n iterations) for a given number. (Java language)

For example: the utilityhas to unroll 3 times the next loop:

 for(int i=0; i<10; i++){  
        int k = k + 1; 
   }  

The tansformed code has to be:

   k = k + 1; 
   k = k + 1;  
   k = k + 1;  
   for(int i=3; i<10; i++){  
        int k = k + 1;  
   } 

Thanks Itamar.

Upvotes: 1

Views: 216

Answers (2)

daniel
daniel

Reputation: 3174

It is possible with Acpect-Oriented Programming.

http://en.wikipedia.org/wiki/Aspect-oriented_programming

But it is tricky and I suggest it is a job for experienced Programmers.

With AOP you can define Rules which where applied to the source-code at the beginning of the compilation process, like your manual unrolling.

But it is also possible that the JIT-Compiler will do this type of optimization of unrolling on the fly at runtime.

Upvotes: 0

fge
fge

Reputation: 121840

There probably are not; and even if they were it wouldn't have any use.

The JIT will perform loop unrolling (at least, Oracle JVM's JIT does) when it estimates that it is beneficial. Among other optimizations.

Write code that is obviously correct and let the JVM handle optimization. The JIT is smarter than you anyway. Repeat after me: the JIT is smarter than you ;)

Upvotes: 2

Related Questions