@author lindamoodj
*/
public class TestBenchmark {
private final static int N = 800;
private final static int NUM_ITERATIONS = 12;
private static IVector2f[][] vecsIS;
private static IVector2f[][] vecsIM;
private static Slow2f[][] vecsS;
private static Switch2f[][] vecsSw;
static long lastTime;
static long slowTime;
static long mixedTime;
static long slowITime;
static long switchTime;
public static void main(String[] args) {
for (int i=0;i<NUM_ITERATIONS;i++){
initVars();
loopInterface();
loopSlow();
loopMixed();
loopSwitch();
System.out.println();
}
System.out.println();
System.out.println("Total all Slow2f.getHash() : " + slowTime);
System.out.println("Total all Slow2f from IVector2f.getHash() : " + slowITime);
System.out.println("Total mixed slow/fast getHash() inside Interface : " + mixedTime);
System.out.println("Total switch getHash() : " + switchTime);
}
private static void loopSlow() {
System.out.println(“getHash of all Slow2f()”);
setTime();
// processArray(vecsS);
processArraySlow(vecsS);
slowTime+=fromTime();
System.out.println(“The time taken:” + fromTime());
}
private static void loopSwitch() {
System.out.println(“getHash of all Switch2f()”);
setTime();
// processArray(vecsS);
processArraySwitch(vecsSw);
switchTime+=fromTime();
System.out.println(“The time taken:” + fromTime());
}
private static void loopMixed() {
System.out.println(“getHash of mixed slow and fast inside Interface”);
setTime();
processArray(vecsIM);
mixedTime+=fromTime();
System.out.println(“The time taken:” + fromTime());
}
private static void loopInterface() {
System.out.println(“getHash of all Slow2f() inside interface”);
setTime();
processArray(vecsIS);
slowITime+=fromTime();
System.out.println(“The time taken:” + fromTime());
}
private static void initVars() {
vecsIS=new IVector2f[N][];
vecsS=new Slow2f[N][];
vecsIM=new IVector2f[N][];
vecsSw=new Switch2f[N][];
Random r=new Random();
for (int i=0;i<N;i++){
vecsIS[i]=new IVector2f[N];
vecsS[i]=new Slow2f[N];
vecsIM[i]=new IVector2f[N];
vecsSw[i]=new Switch2f[N];
for (int j=0;j<N;j++){
float x=r.nextFloat();
float y=r.nextFloat();
vecsIS[i][j]=new Slow2f(x,y);
vecsS[i][j]=new Slow2f(x,y);
vecsSw[i][j]=new Switch2f(x,y);
if (i+j%2==0)
vecsIM[i][j]=new Slow2f(x,y);
else
vecsIM[i][j]=new Fast2f(x,y);
}
}
}
private static void processArray(IVector2f[][] a){
float sum=0;
for (int i=0;i<a.length;i++)
for (int j=0;j<a[i].length;j++)
sum+=a[j][i].getHash();
System.out.println("The sum is " + sum);
}
private static void processArraySwitch(Switch2f[][] a) {
float sum=0;
for (int i=0;i<a.length;i++)
for (int j=0;j<a[i].length;j++){
Switch2f.fastSwitch=(j%2==0);
sum+=a[j][i].getHash();
}
System.out.println("The sum is " + sum);
}
private static void processArraySlow(Slow2f[][] a){
float sum=0;
for (int i=0;i<a.length;i++)
for (int j=0;j<a[i].length;j++)
sum+=a[j][i].getHash();
System.out.println("The sum is " + sum);
}
private static void setTime(){
lastTime=System.currentTimeMillis();
}
private static long fromTime(){
return System.currentTimeMillis()-lastTime;
}
private static interface IVector2f{
void set(float x,float y);
float getHash();
}
private static class Slow2f implements IVector2f{
float x,y;
public Slow2f(float x,float y) {
set(x,y);
}
public void set(float x, float y) {
this.x=x;
this.y=y;
}
public float getHash() {
return x+y;
}
}
private static class Switch2f implements IVector2f{
float x,y;
public static boolean fastSwitch;
public Switch2f(float x,float y) {
set(x,y);
}
public void set(float x, float y) {
this.x=x;
this.y=y;
}
public float getHash() {
if (fastSwitch)
return 1.0f;
else
return x+y;
}
}
private static class Fast2f implements IVector2f{
float x,y;
public Fast2f(float x, float y) {
set(x,y);
}
public void set(float x, float y) {
this.x=x;
this.y=y;
}
public float getHash() {
return 1.0f;
}
}
}