Parameterized Lisp Calls vs Array Calls In the code I added a Interface "barrier" just in-case there was some inlining
/***********************START JAVA*************************************************/ package benchtest;
interface ISomeFunParams { int SomeFun(int i, int j); }
interface ISomeFunArray { int SomeFun(int[] ij); }
interface ISomeFunVarArgs { int SomeFun(int... ij); }
class BenchArrays1 {
public static void main(String[] args) {
ISomeFunParams sumFun = new SomeFunImpl1();
int res = 0; for (int i = 0; i < 100000; i++) for (int j = 0; j < 100000; j++) { res += sumFun.SomeFun(i, j); }
// make sure we care about 'res' int foo = res; if (foo > 0) { res--; } } }
class BenchArrays2 {
public static void main(String[] args) {
ISomeFunVarArgs sumFun = new SomeFunImpl2();
int res = 0; for (int i = 0; i < 100000; i++) for (int j = 0; j < 100000; j++) { res += sumFun.SomeFun(i, j); }
// make sure we care about 'res' int foo = res; if (foo > 0) { res--; } } }
class BenchArrays3 {
public static void main(String[] args) {
ISomeFunArray sumFun = new SomeFunImpl3();
int res = 0; for (int i = 0; i < 100000; i++) for (int j = 0; j < 100000; j++) { res += sumFun.SomeFun(new int[]{i, j}); }
// make sure we care about 'res' int foo = res; if (foo > 0) { res--; } } }
class SomeFunImpl1 implements ISomeFunParams { public int SomeFun(int i, int j) { return i + j; } }
class SomeFunImpl2 implements ISomeFunVarArgs { public int SomeFun(int... ij) { return ij[0] + ij[1]; } }
class SomeFunImpl3 implements ISomeFunArray { public int SomeFun(int[] ij) { return ij[0] + ij[1]; } } /***********************END JAVA*************************************************/
[root@titan ArrayBoundsCheckBenchmarks]# time java -cp bin/ benchtest.BenchArrays1
real 0m0.113s user 0m0.040s sys 0m0.013s [root@titan ArrayBoundsCheckBenchmarks]# time java -cp bin/ benchtest.BenchArrays2
real 1m42.898s user 1m21.279s sys 0m0.742s [root@titan ArrayBoundsCheckBenchmarks]# time java -cp bin/ benchtest.BenchArrays3
real 1m40.823s user 1m21.411s sys 0m0.651s