Skip to content

Instantly share code, notes, and snippets.

@michaeldorner
Last active March 1, 2017 11:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save michaeldorner/9b2fa6eb3711db2ab92c to your computer and use it in GitHub Desktop.
Save michaeldorner/9b2fa6eb3711db2ab92c to your computer and use it in GitHub Desktop.
//Type.java
public enum Type { A, B, C, D, E, F, G, H, I, J };
// Base.java
public abstract class Base {
int i = 1;
final Type type;
public Base(Type type) {
this.type = type;
}
public abstract void doSomething();
}
// A.java
public class A extends Base {
public A() {
super(Type.A);
}
public boolean isA() {
return true;
}
public void doA() {
i = i * -1;
}
@Override
public void doSomething() {
i = i * -1;
}
}
//B.java
public class B extends Base {
public B() {
super(Type.B);
}
public boolean isB() {
return true;
}
public void doB() {
i = i * -1;
}
@Override
public void doSomething() {
i = i * -1;
}
}
// classes C to J follow the same logic.
//JavaPerformance.java
import java.util.Random;
public class JavaPerformance {
private final static long iterations = 1000000000;
private static Base createClassRandomly(final int numberOfClasses) {
if (numberOfClasses < 2 || numberOfClasses > 10) {
throw new Error("Only 10 different classes (A, ..., J) are implemented. ");
}
Random r = new Random();
int randomPositiveNumber = Math.abs(r.nextInt(numberOfClasses));
switch (randomPositiveNumber) {
case 0:
return new A();
case 1:
return new B();
case 2:
return new C();
case 3:
return new D();
case 4:
return new E();
case 5:
return new F();
case 6:
return new G();
case 7:
return new H();
case 8:
return new I();
case 9:
return new J();
default:
throw new Error("Only 10 different classes (A, ..., J) are implemented. ");
}
}
public static long measureINSTANCEOF(final int numberOfClasses) {
long start = System.currentTimeMillis();
for (long i = 0; i<iterations; i++) {
Base base = createClassRandomly(numberOfClasses);
if (base instanceof A) {
((A) base).doA();
} else if (base instanceof B) {
((B) base).doB();
} else if (base instanceof C) {
((C) base).doC();
} else if (base instanceof D) {
((D) base).doD();
} else if (base instanceof E) {
((E) base).doE();
} else if (base instanceof F) {
((F) base).doF();
} else if (base instanceof G) {
((G) base).doG();
} else if (base instanceof H) {
((H) base).doH();
} else if (base instanceof I) {
((I) base).doI();
} else if (base instanceof J) {
((J) base).doJ();
}
}
return (System.currentTimeMillis() - start);
}
public static long measureOO(final int numberOfClasses) {
long start = System.currentTimeMillis();
for (long i = 0; i<iterations; i++) {
Base base = createClassRandomly(numberOfClasses);
base.doSomething();
}
return (System.currentTimeMillis() - start);
}
public static long measureTYPE(final int numberOfClasses) {
long start = System.currentTimeMillis();
for (long i = 0; i<iterations; i++) {
Base base = createClassRandomly(numberOfClasses);
switch (base.type) {
case A:
((A)base).doA();
break;
case B:
((B)base).doB();
break;
case C:
((C)base).doC();
break;
case D:
((D)base).doD();
break;
case E:
((E)base).doE();
break;
case F:
((F)base).doF();
break;
case G:
((G)base).doG();
break;
case H:
((H)base).doH();
break;
case I:
((I)base).doI();
break;
case J:
((J)base).doJ();
break;
default:
break;
}
}
return (System.currentTimeMillis() - start);
}
private static long measureGETCLASS(final int numberOfClasses) {
long start = System.currentTimeMillis();
for (long i = 0; i<iterations; i++) {
Base base = createClassRandomly(numberOfClasses);
if (base.getClass() == A.class) {
((A)base).doA();
} else if (base.getClass() == B.class) {
((B)base).doB();
} else if (base.getClass() == C.class) {
((C)base).doC();
} else if (base.getClass() == D.class) {
((D)base).doD();
} else if (base.getClass() == E.class) {
((E)base).doE();
} else if (base.getClass() == F.class) {
((F)base).doF();
} else if (base.getClass() == G.class) {
((G)base).doG();
} else if (base.getClass() == H.class) {
((H)base).doH();
} else if (base.getClass() == I.class) {
((I)base).doI();
} else if (base.getClass() == J.class) {
((J)base).doJ();
}
}
return (System.currentTimeMillis() - start);
}
public static void main(String[] args) {
final boolean prettyOutput = false;
if(prettyOutput) {
System.out.printf("%-12s%-12s%-12s%-12s%-12s\n", "# Classes", "INSTANCEOF", "OO", "TYPE", "GETCLASS");
System.out.println("--------------------------------------------------------");
} else {
System.out.println("# Classes; INSTANCEOF; OO; TYPE; GETCLASS");
}
for (int differentClasses = 2; differentClasses<=10; differentClasses++) {
final long timeINSTANCEOF = measureINSTANCEOF(differentClasses);
final long timeOO = measureOO(differentClasses);
final long timeTYPE = measureTYPE(differentClasses);
final long timeGETCLASS = measureGETCLASS(differentClasses);
if (prettyOutput) {
System.out.printf("%-12d%-12d%-12d%-12d%-12d\n", differentClasses, timeINSTANCEOF, timeOO, timeTYPE, timeGETCLASS);
} else {
System.out.printf("%d; %d; %d; %d; %d\n", differentClasses, timeINSTANCEOF, timeOO, timeTYPE, timeGETCLASS);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment