Last active
August 29, 2015 14:23
-
-
Save AdamStelmaszczyk/e943966785be551585f4 to your computer and use it in GitHub Desktop.
Declaring/initializing variables inside or outside of a double (or multiple) loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiled from "Test.java" | |
public class Test { | |
public Test(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method java/lang/Object."<init>":()V | |
4: return | |
public void testA(); | |
Code: | |
0: dconst_0 | |
1: dstore_1 | |
2: iconst_0 | |
3: istore_3 | |
4: iload_3 | |
5: sipush 1000 | |
8: if_icmpge 57 | |
11: iconst_0 | |
12: istore 4 | |
14: iload 4 | |
16: sipush 1000 | |
19: if_icmpge 51 | |
22: dconst_0 | |
23: dstore_1 | |
24: iconst_0 | |
25: istore 5 | |
27: iload 5 | |
29: sipush 1000 | |
32: if_icmpge 45 | |
35: dload_1 | |
36: dconst_1 | |
37: dadd | |
38: dstore_1 | |
39: iinc 5, 1 | |
42: goto 27 | |
45: iinc 4, 1 | |
48: goto 14 | |
51: iinc 3, 1 | |
54: goto 4 | |
57: return | |
public void testB(); | |
Code: | |
0: iconst_0 | |
1: istore_3 | |
2: iload_3 | |
3: sipush 1000 | |
6: if_icmpge 55 | |
9: iconst_0 | |
10: istore 4 | |
12: iload 4 | |
14: sipush 1000 | |
17: if_icmpge 49 | |
20: dconst_0 | |
21: dstore_1 | |
22: iconst_0 | |
23: istore 5 | |
25: iload 5 | |
27: sipush 1000 | |
30: if_icmpge 43 | |
33: dload_1 | |
34: dconst_1 | |
35: dadd | |
36: dstore_1 | |
37: iinc 5, 1 | |
40: goto 25 | |
43: iinc 4, 1 | |
46: goto 12 | |
49: iinc 3, 1 | |
52: goto 2 | |
55: return | |
public void testC(); | |
Code: | |
0: iconst_0 | |
1: istore_1 | |
2: iload_1 | |
3: sipush 1000 | |
6: if_icmpge 53 | |
9: iconst_0 | |
10: istore_2 | |
11: iload_2 | |
12: sipush 1000 | |
15: if_icmpge 47 | |
18: dconst_0 | |
19: dstore_3 | |
20: iconst_0 | |
21: istore 5 | |
23: iload 5 | |
25: sipush 1000 | |
28: if_icmpge 41 | |
31: dload_3 | |
32: dconst_1 | |
33: dadd | |
34: dstore_3 | |
35: iinc 5, 1 | |
38: goto 23 | |
41: iinc 2, 1 | |
44: goto 11 | |
47: iinc 1, 1 | |
50: goto 2 | |
53: return | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sample; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.util.concurrent.TimeUnit; | |
@OutputTimeUnit(TimeUnit.SECONDS) | |
@BenchmarkMode({Mode.Throughput}) | |
@Warmup(iterations = 10) | |
@Fork(value = 1) | |
@State(Scope.Benchmark) | |
public class MyBenchmark { | |
private static final int LOOPS = 100; | |
@Benchmark | |
public double testA() { | |
double b = 0; | |
double a = 0.0; | |
for (int i1 = 0; i1 < LOOPS; i1++) { | |
for (int im = 0; im < LOOPS; im++) { | |
a = 0; // a is used for the first time | |
for (int in = 0; in < LOOPS; in++) { | |
a++; // calculate something with a | |
b += a; | |
} | |
} | |
} | |
return b; | |
} | |
@Benchmark | |
public double testB() { | |
double b = 0; | |
double a; | |
for (int i1 = 0; i1 < LOOPS; i1++) { | |
for (int im = 0; im < LOOPS; im++) { | |
a = 0; // a is used for the first time | |
for (int in = 0; in < LOOPS; in++) { | |
a++; // calculate something with a | |
b += a; | |
} | |
} | |
} | |
return b; | |
} | |
@Benchmark | |
public double testC() { | |
double b = 0; | |
for (int i1 = 0; i1 < LOOPS; i1++) { | |
for (int im = 0; im < LOOPS; im++) { | |
double a = 0; // a is used for the first time | |
for (int in = 0; in < LOOPS; in++) { | |
a++; // calculate something with a | |
b += a; | |
} | |
} | |
} | |
return b; | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(MyBenchmark.class.getSimpleName()) | |
.forks(1) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
private static final int LOOPS = 1_000; | |
public void testA() { | |
double a = 0.0; | |
for (int i1 = 0; i1 < LOOPS; i1++) { | |
for (int im = 0; im < LOOPS; im++) { | |
a = 0; // a is used for the first time | |
for (int in = 0; in < LOOPS; in++) { | |
a++; // calculate something with a | |
} | |
} | |
} | |
} | |
public void testB() { | |
double a; | |
for (int i1 = 0; i1 < LOOPS; i1++) { | |
for (int im = 0; im < LOOPS; im++) { | |
a = 0; // a is used for the first time | |
for (int in = 0; in < LOOPS; in++) { | |
a++; // calculate something with a | |
} | |
} | |
} | |
} | |
public void testC() { | |
for (int i1 = 0; i1 < LOOPS; i1++) { | |
for (int im = 0; im < LOOPS; im++) { | |
double a = 0; // a is used for the first time | |
for (int in = 0; in < LOOPS; in++) { | |
a++; // calculate something with a | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment