Heres a few tests to see which of the two things is faster.
Myth: For-each loops are faster then regular For loops
Code:
public static void runForLoopBenchMarks() {
double time;
long start;
int tests = 10;
int[] testArray = new int[tests];
start = System.nanoTime();
for(int a = 0; a < testArray.length; a++)
for(int b = 0; b < testArray.length; b++)
for(int c = 0; c < testArray.length; c++)
for(int d = 0; d < testArray.length; d++)
for(int e = 0; e < testArray.length; e++);
time = System.nanoTime() - start;
System.out.println("Time for test 1: " + time);
start = System.nanoTime();
for(int a : testArray)
for(int b : testArray)
for(int c : testArray)
for(int d : testArray)
for(int e : testArray);
time = System.nanoTime() - start;
System.out.println("Time for test 2: " + time);
}
Code:
Time for test 1: 1507376.0
Time for test 2: 158769.0
Result: Standard for loops are faster then for-each loops.
Myth: Using a variable is the same speed as using a number
Code:
public static void runForLoopBenchMarks() {
double time;
long start;
int tests = 10;
int[] testArray = new int[tests];
start = System.nanoTime();
for(int a = 0; a < 10; a++)
for(int b = 0; b < 10; b++)
for(int c = 0; c < 10; c++)
for(int d = 0; d < 10; d++)
for(int e = 0; e < 10; e++);
time = System.nanoTime() - start;
System.out.println("Time for test 1: " + time);
start = System.nanoTime();
for(int a = 0; a < testArray.length; a++)
for(int b = 0; b < testArray.length; b++)
for(int c = 0; c < testArray.length; c++)
for(int d = 0; d < testArray.length; d++)
for(int e = 0; e < testArray.length; e++);
time = System.nanoTime() - start;
System.out.println("Time for test 2: " + time);
}
Code:
Time for test 1: 1207958.0
Time for test 2: 168002.0
Result: Using an actual number in place of a variable will make the code faster
Myth: Switch statements are the fastest way to check values (Below are sevaral ways compared, but note the array[] index way would be faster in the case of there being more possible number values)
Code:
public static void runIntegerBenchMarks() throws Exception {
Test t = new Test();
double time;
long start, tests = 1000;
int arg = 3;
start = System.nanoTime();
for(int i = 0; i < tests; i++) {
if(arg == 1) {
t.choice1();
} else if(arg == 2) {
t.choice2();
} else if(arg == 3) {
t.choice3();
}
}
time = System.nanoTime() - start;
System.out.println("Time for test 1: " + time);
System.out.println("time for one : " + (double)(time / tests) + "\n\n");
start = System.nanoTime();
for(int i = 0; i < tests; i++) {
switch(arg) {
case 1: t.choice1(); break;
case 2: t.choice2(); break;
case 3: t.choice3(); break;
}
}
time = System.nanoTime() - start;
System.out.println("Time for test 2: " + time);
System.out.println("time for one : " + (double)(time / tests) + "\n\n");
start = System.nanoTime();
for(int i = 0; i < tests; i++) {
t.getClass().getMethod("choice" + arg).invoke(t);
}
time = System.nanoTime() - start;
System.out.println("Time for test 3: " + time);
System.out.println("time for one : " + (double)(time / tests) + "\n\n");
Option[] options = new Option[4];
options[1] = new One();
options[2] = new Two();
options[3] = new Three();
start = System.nanoTime();
for(int i = 0; i < tests; i++) {
options[arg].choice();
}
time = System.nanoTime() - start;
System.out.println("Time for test 4: " + time);
System.out.println("time for one : " + (double)(time / tests) + "\n\n");
}
Code:
Time for test 1: 48826.0
time for one : 48.826
Time for test 2: 35589.0
time for one : 35.589
Time for test 3: 2.0072777E7
time for one : 20072.777
Time for test 4: 40004.0
time for one : 40.004
Result: Switch statements are the fastest way for situations with very small possible choices, but otherwise array[] indexes would be the fastest hands down, because the time takes never increases with possible awnsers.
Myth: List.get(int) method is slower then using an array
Code:
public static void testListGet() {
double time;
long start;
int tests = 10;
List<Object> list = new ArrayList<Object>();
Object[] arry = new Object[100];
for(int i = 0; i < arry.length; i++) {
list.add(new Object());
arry[i] = new Object();
}
start = System.nanoTime();
for(int i = 0; i < tests; i++) {
Object o = list.get(list.size()-2);
}
time = System.nanoTime() - start;
System.out.println("Time for test 1: " + time);
start = System.nanoTime();
for(int i = 0; i < tests; i++) {
Object o = arry[arry.length-2];
}
time = System.nanoTime() - start;
System.out.println("Time for test 2: " + time);
}
Code:
Time for test 1: 7839.0
Time for test 2: 1744.0
Result: Using an array is a LOT faster than using a list
Some misc stuff:
Code:
interface Option {
public void choice();
}
class One implements Option {
@Override
public void choice() {}
}
class Two implements Option {
@Override
public void choice() {}
}
class Three implements Option {
@Override
public void choice() {}
}
class Test {
public void choice1() {}
public void choice2() {}
public void choice3() {}
}
I will add more to this later.