When I read about optimization, I covered topic about loop unfolding. By doing some small search on Google, I didnt found if Java's compiler do this or not.
So the best way was to try if by my self.
Actually i was quite suprised of fact, that actually by doing this loop unfolding, i managed to speed it up, since I was quite sure modern compilers do this for me.
public static void folded() {
System.out.println("Folded:");
long c1 = System.currentTimeMillis();
for (int r = 0; r < 10; r++) {
for (int i = 0; i < 500000; i++) {
Math.sin(i);
}
}
System.out.println(System.currentTimeMillis() - c1);
}
public static void unFolded() {
System.out.println("Unfolded:");
long c1 = System.currentTimeMillis();
for (int r = 0; r < 10; r++) {
for (int i = 0; i < 500000; i += 10) {
Math.sin(i);
Math.sin(i + 1);
Math.sin(i + 2);
Math.sin(i + 3);
Math.sin(i + 4);
Math.sin(i + 5);
Math.sin(i + 6);
Math.sin(i + 7);
Math.sin(i + 8);
Math.sin(i + 9);
}
}
System.out.println(System.currentTimeMillis() - c1);
}
RESULT(COUNTER 500'000):
Folded:453
Unfolded:114
RESULT(COUNTER 5'000'000):
Folded: 13850
Unfolded: 11929
So what should i trust? Manual optimization or compilers? Since in this test, my result shows that manual optimization seems to be better.