// /** * Class RandNum * * Provides random numbers based on the second-order linear * congruential generator that we've always used. Initialization * constants are suggested by J. G. Skellam. */ final class RandNum { // The current value of the random number generator. int [] currval; /** * constructor ** */ // The constructor merely initializes the current value. public RandNum() { currval = new int[2]; // Allocate array. currval[0] = 13; currval[1] = 117; } /** * next * * Returns the next random number in the sequence. */ public int next() { int temp; // Holds intermediate calculations. temp = (currval[0] * 254754 + currval[1] * 529562) % 999563; currval[1] = currval[0]; currval[0] = temp; return (temp); } /** * nextwc * * Returns next random number, but with a ceiling. */ public int nextwc(int ceiling) { return(next() % ceiling); } /** * abs_nextwc * * Returns next random number with ceiling; applies absolute value * function on the way out. */ public int abs_nextwc(int ceiling) { int temp; temp = nextwc(ceiling); return (Math.abs(temp)); } } // final class testclass { byte[] bytes; public testclass(int n) { bytes = new byte[n]; bench.allocated++; } protected void finalize() throws Throwable { //System.out.println("finalized"); bytes = null; bench.allocated--; super.finalize(); } } // final class testclass1 { public final int func(int t) { t=t+1; return(t); } } // class testclassr { public int func(int t) { t=t+2; return(t); } } class testclass2 extends testclassr //;-) { public int func(int t) { t=t+1; return(t); } } final class bench { static int allocated; public static void main (String args[]) { int N=2500; int N2=10000000; int i,k; int r; long time,total_time; int which; RandNum rndnum = new RandNum(); testclass[] test = new testclass[N]; Runtime RT = Runtime.getRuntime(); testclassr tt0; testclass1 tt1; testclass2 tt2; time = System.currentTimeMillis(); // allocated=0; for(k=0;k<1;k++) { //System.out.println("free/total: "+RT.freeMemory()+"/"+RT.totalMemory()); for(i=0;i<N;i++) test[i] = new testclass((int)rndnum.abs_nextwc(5000)); System.out.println("created "+i+" objects in "+(System.currentTimeMillis()-time)+" msecs"+" allocated: "+allocated); time = System.currentTimeMillis(); for(i=0;i<N;i++) test[i] = null; System.out.println("deleted "+i+" objects in "+(System.currentTimeMillis()-time)+"msecs "+" allocated: "+allocated); //System.out.println("free/total: "+RT.freeMemory()+"/"+RT.totalMemory()); } time = System.currentTimeMillis(); System.gc(); System.runFinalization(); System.out.println("last allocated: "+allocated+" in "+(System.currentTimeMillis()-time)+"msecs"); allocated=0; which=0; System.out.println("--------------------------------------"); // total_time=0; for(k=0;k<10;k++) { time = System.currentTimeMillis(); for(i=0;i<N;i++) { which = (int)rndnum.abs_nextwc(N); test[which] = new testclass((int)rndnum.abs_nextwc(5000)); } //System.out.print(which); System.out.println("created "+i+" objects in "+(System.currentTimeMillis()-time)+"msecs "+" allocated: "+allocated); total_time+=(System.currentTimeMillis()-time); //System.gc(); //System.runFinalization(); //System.out.println("/"+allocated); } //k=i; warum geht das nicht ? time = System.currentTimeMillis(); System.gc(); System.runFinalization(); System.out.println("last allocated: "+allocated+" in "+(System.currentTimeMillis()-time)+"msecs"); //System.out.println(" created "+i*k+"objects in "+total_time+"msecs"); System.out.println(" created "+2500*k+" objects in "+total_time+"msecs"); // r=0; tt0 = new testclassr(); time = System.currentTimeMillis(); for(i=0;i<N2;i++) r+=tt0.func(0); System.out.println(N2+" virtual functions in "+(System.currentTimeMillis()-time)+"msecs, result: "+r); r=0; tt2 = new testclass2(); time = System.currentTimeMillis(); for(i=0;i<N2;i++) r+=tt2.func(0); System.out.println(N2+" virtual functions in "+(System.currentTimeMillis()-time)+"msecs, result: "+r); r=0; tt1 = new testclass1(); time = System.currentTimeMillis(); for(i=0;i<N2;i++) r+=tt1.func(0); System.out.println(N2+" functions in "+(System.currentTimeMillis()-time)+"msecs, result: "+r); } } //