/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Hoge
{
public static int n = 0;
public static int m = 0;
public static int h = 0;
}
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static final int NUM = 15;
static Foo[] sFoos;
Foo[] iFoos;
{
Ideone io = new Ideone();
io.test();
}
void test() {
long t0, t1;
Ideone io = new Ideone();
Foo[] foos = new Foo[NUM];
foos[0] = new Foo0();
foos[1] = new Foo1();
foos[2] = new Foo2();
foos[3] = new Foo3();
foos[4] = new Foo4();
foos[5] = new Foo5();
foos[6] = new Foo6();
foos[7] = new Foo7();
foos[8] = new Foo8();
foos[9] = new Foo9();
foos[10] = new Foo10();
for (int i = 11; i < NUM; i++) {
foos[i] = foos[0];
}
sFoos = iFoos = io.iFoos = foos;
int[] indexes = new int[20000000];
for (int i = 0; i < indexes.length; i++) {
indexes[i] = rand.nextInt(NUM);
}
///////////////////////////////////////////
System.
out.
println("Local Variable Array"); t0
= System.
currentTimeMillis(); for (int i = 0; i < indexes.length; i++) {
foos[indexes[i]].doit();
}
t1
= System.
currentTimeMillis(); System.
out.
println(t1
+ "-" + t0
+ "=" + (t1
- t0
));
///////////////////////////////////////////
System.
out.
println("Class Field Array"); Hoge.
n = 0; t0
= System.
currentTimeMillis(); for (int i = 0; i < indexes.length; i++) {
sFoos[indexes[i]].doit();
}
t1
= System.
currentTimeMillis(); System.
out.
println(t1
+ "-" + t0
+ "=" + (t1
- t0
));
///////////////////////////////////////////
System.
out.
println("Instance(this) Field Array"); Hoge.
n = 0; t0
= System.
currentTimeMillis(); for (int i = 0; i < indexes.length; i++) {
iFoos[indexes[i]].doit();
}
t1
= System.
currentTimeMillis(); System.
out.
println(t1
+ "-" + t0
+ "=" + (t1
- t0
));
///////////////////////////////////////////
System.
out.
println("Instance(local) Field Array"); Hoge.
n = 0; t0
= System.
currentTimeMillis(); for (int i = 0; i < indexes.length; i++) {
io.iFoos[indexes[i]].doit();
}
t1
= System.
currentTimeMillis(); System.
out.
println(t1
+ "-" + t0
+ "=" + (t1
- t0
));
///////////////////////////////////////////
System.
out.
println("tableswitch"); t0
= System.
currentTimeMillis(); for (int i = 0; i < indexes.length; i++) {
switch (indexes[i]) {
case 0:
case 11:
case 12:
case 13:
case 14:
Hoge.m++;
break;
case 1:
Hoge.m = (Hoge.m + 1) * 2;
break;
case 2:
Hoge.m += 13;
break;
case 3:
Hoge.m -= 4;
break;
case 4:
Hoge.m *= 3;
break;
case 5:
Hoge.m += Hoge.m % 17;
break;
case 6:
Hoge.m /= 100;
break;
case 7:
Hoge.m--;
break;
case 8:
Hoge.m += 30;
break;
case 9:
Hoge.m >>= 1;
break;
case 10:
Hoge.m -= Hoge.m % 73;
break;
}
}
t1
= System.
currentTimeMillis(); System.
out.
println(t1
+ "-" + t0
+ "=" + (t1
- t0
));
///////////////////////////////////////////
for (int i = 0; i < indexes.length; i++) {
switch (indexes[i]) {
case 0: indexes[i] = 5; break;
case 1: indexes[i] = 101; break;
case 2: indexes[i] = 93; break;
case 3: indexes[i] = 4016; break;
case 4: indexes[i] = 880888; break;
case 5: indexes[i] = 41; break;
case 6: indexes[i] = 22; break;
case 7: indexes[i] = 330; break;
case 8: indexes[i] = 1010; break;
case 9: indexes[i] = 171; break;
case 10: indexes[i] = 999; break;
case 11: indexes[i] = 10010; break;
case 12: indexes[i] = 7521; break;
case 13: indexes[i] = 8; break;
case 14: indexes[i] = 76; break;
}
}
///////////////////////////////////////////
System.
out.
println("lookupswitch"); t0
= System.
currentTimeMillis(); for (int i = 0; i < indexes.length; i++) {
switch (indexes[i]) {
case 5:
case 10010:
case 7521:
case 8:
case 76:
Hoge.h++;
break;
case 101:
Hoge.h = (Hoge.h + 1) * 2;
break;
case 93:
Hoge.h += 13;
break;
case 4016:
Hoge.h -= 4;
break;
case 880888:
Hoge.h *= 3;
break;
case 41:
Hoge.h += Hoge.h % 17;
break;
case 22:
Hoge.h /= 100;
break;
case 330:
Hoge.h--;
break;
case 1010:
Hoge.h += 30;
break;
case 171:
Hoge.h >>= 1;
break;
case 999:
Hoge.h -= Hoge.h % 73;
break;
}
}
t1
= System.
currentTimeMillis(); System.
out.
println(t1
+ "-" + t0
+ "=" + (t1
- t0
));
///////////////////////////////////////////
}
}
interface Foo
{
void doit();
}
class Foo0 implements Foo
{
public void doit() { Hoge.n++; }
}
class Foo1 implements Foo
{
public void doit() { Hoge.n = (Hoge.n + 1) * 2; }
}
class Foo2 implements Foo
{
public void doit() { Hoge.n += 13; }
}
class Foo3 implements Foo
{
public void doit() { Hoge.n -= 4; }
}
class Foo4 implements Foo
{
public void doit() { Hoge.n *= 3; }
}
class Foo5 implements Foo
{
public void doit() { Hoge.n += Hoge.n % 17; }
}
class Foo6 implements Foo
{
public void doit() { Hoge.n /= 100; }
}
class Foo7 implements Foo
{
public void doit() { Hoge.n--; }
}
class Foo8 implements Foo
{
public void doit() { Hoge.n += 30; }
}
class Foo9 implements Foo
{
public void doit() { Hoge.n >>= 1; }
}
class Foo10 implements Foo
{
public void doit() { Hoge.n -= Hoge.n % 73; }
}