Making up for the long delay between my last two posts, here's another one mere hours after the last, and this time with code!
Still it was a good interesting exercise... I challenge you all to implement an unrolled loop in your language of choice! I'd love to see a lisp version, actually on second thoughts...
Anyhow, here it is:
Hope you enjoyed reading, I especially liked the embedded conditional statement as the switch control statement... writing that bit really got my heart racing haha
:D
So, I've been reading about Duff's device and loop unrolling, and wanted to have a crack at it in Java... well of course there is absolutely no point implementing this in Java, and the results are just as I expected... the JVM can optimize a normal loop better than it can an unrolled loop :p ... I've even heard that every time a developer unwinds a loop in Java, James Gosling gets a headache... sorry James.Still it was a good interesting exercise... I challenge you all to implement an unrolled loop in your language of choice! I'd love to see a lisp version, actually on second thoughts...
Anyhow, here it is:
//(c)me & GPL3:
public class DuffsDevice
{
public static void main(String[] args)
{
int demoSize = 80;//woot... 0 works
System.out.println("Normal loop: " );
long start = System.nanoTime();
for(int i = 0; i < demoSize; i++)// one partial two full for demo
{
System.out.print(" Bit:" + i);
}//normal loop
System.out.println("\nNormal end: " + (System.nanoTime()-start));
final int winding = 5;//up to 6
System.out.println("Duffs device in Java loop: ");
start = System.nanoTime();
for(int i = 0; i < demoSize; i = i)// one partial two full for demo
{
System.out.print("\n" );//System.out.println("size%winding:" + demoSize%winding + " i:" + i );
switch( (i + winding <= demoSize) ? 0 : winding-(demoSize%winding) )
{
//case (winding-6): { System.out.print(" Bit:" + (i) +" -a" ); i++; }
case (winding-5): { System.out.print(" Bit:" + (i) +" -b" ); i++; }
case (winding-4): { System.out.print(" Bit:" + (i) +" -c" ); i++; }
case (winding-3): { System.out.print(" Bit:" + (i) +" -d" ); i++; }
case (winding-2): { System.out.print(" Bit:" + (i) +" -e" ); i++; }
case (winding-1): { System.out.print(" Bit:" + (i) +" -f" ); i++; }
}//switch
}//Duffs device
System.out.println("\nDuffs device in Java end: " + (System.nanoTime()-start));
//usually arround 1803034 in the normal loop
//usually arround 2272790(winding 3) 2065498(winding 6) for unrolled loop... ymmv of course.
//Duff was right... this is even pretty ugly in Java :p ... ugly, but fun :D
}//main
}//class
Hope you enjoyed reading, I especially liked the embedded conditional statement as the switch control statement... writing that bit really got my heart racing haha
No comments:
Post a Comment