import java.applet.*; import java.util.Date; import java.awt.*; public class Profile extends Applet { TextArea output; private void println(String s) { output.append(s); } public void init() { int i,j; int x,y; Date t0,t1; // set up output window setLayout(null); output=new TextArea(); output.setEditable(false); // set output to applet size output.setSize(getSize()); add(output); System.gc(); // do garbage collection t0=new Date(); // do enough to get a good time for (j=0;j<1000;j++) for (i=0;i<100000;i++) { y=500-i; x=Math.abs(y); // use math library } t1=new Date(); elapsed("Math library",t1,t0); System.gc(); // do garbage collection t0=new Date(); for (j=0;j<1000;j++) for (i=0;i<100000;i++) { y=500-i; x=y<0?-y:y; // compute yourself } t1=new Date(); elapsed("Manual ABS",t1,t0); // set up for array test: int[] ary0; int[] ary1; ary0 = new int[100]; ary1 =new int[50]; System.gc(); // do garbage collection t0=new Date(); for (i=0;i<500000;i++) { System.arraycopy(ary0,10,ary1,0,50); } t1=new Date(); elapsed("arraycopy",t1,t0); System.gc(); // do garbage collection t0=new Date(); // array takes more time // so less loops req'd for (i=0;i<500000;i++) { int q; for (q=10;q<60;q++) ary1[q-10]=ary0[q]; } t1=new Date(); elapsed("Manual array",t1,t0); System.gc(); // do garbage collection t0=new Date(); for (j=0;j<100;j++) for (i=0;i<5000000;i++) { int x1,q=10,z=4; x1=q*z; } t1=new Date(); elapsed("Multiply by 4",t1,t0); System.gc(); // do garbage collection t0=new Date(); for (j=0;j<100;j++) for (i=0;i<5000000;i++) { int x1,q=10,z=2; x1=q<<z; } t1=new Date(); elapsed("Shift to multiply",t1,t0); } private void elapsed(String lbl,Date t1,Date t0) { long diff=t1.getTime()-t0.getTime(); println(lbl +" : "+diff + "ms\n"); } }