fork download
/* 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;
	
	public static void main (String[] args) throws java.lang.Exception
	{
		Ideone io = new Ideone();
		
		io.test();
	}
	
	void test() {
		long t0, t1;
		
		Ideone io = new Ideone();
		
		Random rand = new Random();
		
		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:
				foo0();
				break;
			case 1:
				foo1();
				break;
			case 2:
				foo2();
				break;
			case 3:
				foo3();
				break;
			case 4:
				foo4();
				break;
			case 5:
				foo5();
				break;
			case 6:
				foo6();
				break;
			case 7:
				foo7();
				break;
			case 8:
				foo8();
				break;
			case 9:
				foo9();
				break;
			case 10:
				foo10();
				break;
			}
		}
		t1 = System.currentTimeMillis();
		System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
		Hoge.m = Hoge.h;

		///////////////////////////////////////////
		
		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"); Hoge.h = 0;
		t0 = System.currentTimeMillis();
		for (int i = 0; i < indexes.length; i++) {
			switch (indexes[i]) {
			case 5:
			case 10010:
			case 7521:
			case 8:
			case 76:
				foo0();
				break;
			case 101:
				foo1();
				break;
			case 93:
				foo2();
				break;
			case 4016:
				foo3();
				break;
			case 880888:
				foo4();
				break;
			case 41:
				foo5();
				break;
			case 22:
				foo6();
				break;
			case 330:
				foo7();
				break;
			case 1010:
				foo8();
				break;
			case 171:
				foo9();
				break;
			case 999:
				foo10();
				break;
			}
		}
		t1 = System.currentTimeMillis();
		System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));

		///////////////////////////////////////////
		
		System.out.println("check");
		
		System.out.println(Hoge.n);
		System.out.println(Hoge.m);
		System.out.println(Hoge.h);
	}
	
	void foo0() { Hoge.h++; }
	void foo1() { Hoge.h = (Hoge.h + 1) * 2; }
	void foo2() { Hoge.h += 13; }
	void foo3() { Hoge.h -= 4; }
	void foo4() { Hoge.h *= 3; }
	void foo5() { Hoge.h += Hoge.h % 17; }
	void foo6() { Hoge.h /= 100; }
	void foo7() { Hoge.h--; }
	void foo8() { Hoge.h += 30; }
	void foo9() { Hoge.h >>= 1; }
	void foo10() { Hoge.h -= Hoge.h % 73; }
}

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; }
}
Success #stdin #stdout 4.17s 380992KB
stdin
Standard input is empty
stdout
Local Variable Array
1414264900276-1414264899800=476
Class Field Array
1414264900748-1414264900276=472
Instance(this) Field Array
1414264901233-1414264900749=484
Instance(local) Field Array
1414264901721-1414264901234=487
tableswitch
1414264902048-1414264901721=327
lookupswitch
1414264902687-1414264902320=367
check
84
84
84