1 package org.objectweb.jac.samples.bench; 2 3 import java.lang.reflect.Method ; 4 5 public class Bench { 6 14 public static void main(String args[]) throws Exception { 15 if (args.length<1 || args.length>2) { 16 System.err.println("Wrong number of arguments"); 17 printUsage(); 18 System.exit(1); 19 } 20 Bench b = new Bench( 21 Integer.parseInt(args[0]), 22 args.length > 1 ? args[1] : "d"); 23 24 try { 25 m1=Bench.class.getMethod("m1",new Class []{}); 26 m2=Bench.class.getMethod("m2",new Class []{int.class}); 27 m3=Bench.class.getMethod("m3",new Class []{int.class,int.class}); 28 m4=Bench.class.getMethod("m4",new Class []{}); 29 m5=Bench.class.getMethod("m5",new Class []{int.class}); 30 m6=Bench.class.getMethod("m6",new Class []{int.class,int.class}); 31 m7=Bench.class.getMethod("m7",new Class []{Object .class}); 32 m8=Bench.class.getMethod("m8",new Class []{Object .class,Object .class}); 33 } catch(Exception e) { 34 e.printStackTrace(); 35 } 36 System.out.print("Warming up "); 38 b.doBench(); System.out.print("."); 39 b.doBench(); System.out.print("."); 40 b.doBench(); System.out.print("."); 41 b.doBench(); System.out.println("."); 42 44 System.out.println("Starting bench"); 45 long total = 0; 46 int num_runs = 5; 47 for (int i=0; i<num_runs; i++) { 48 long start = System.currentTimeMillis(); 49 b.doBench(); 50 long end = System.currentTimeMillis(); 51 System.out.print(i+": "+(end-start)+"ms; "); 52 total += end-start; 53 } 54 System.out.println(" average="+(total/num_runs)); 55 } 56 57 static void printUsage() { 58 System.out.println("Usage: java org.objectweb.jac.samples.bench.Bench <nb iterations> [s|r]"); 59 System.out.println(" <nb iterations>: number of iterations"); 60 System.out.println(" [r|s]: r(reflective calls), d(direct calls)"); 61 } 62 63 boolean rCalls; 64 65 void doBench() { 66 if(rCalls) { 67 bench1(); 69 bench2(); 70 bench3(); 71 bench4(); 72 bench5(); 73 bench6(); 74 bench7(); 75 bench8(); 76 } else { 77 sbench1(); 79 sbench2(); 80 sbench3(); 81 sbench4(); 82 sbench5(); 83 sbench6(); 84 sbench7(); 85 sbench8(); 86 } 87 } 88 89 static Method m1; 90 static Method m2; 91 static Method m3; 92 static Method m4; 93 static Method m5; 94 static Method m6; 95 static Method m7; 96 static Method m8; 97 98 public int n; 99 String s; 100 101 106 public Bench(int n,String reflectiveCalls) { 107 this.n = n; 108 if(reflectiveCalls.equals("r")) { 109 this.rCalls=true; 110 } else { 111 this.rCalls=false; 112 } 113 } 114 115 118 void bench1() { 119 for(int i=0; i<n; i++) { 120 try { 121 m1.invoke(this,new Object []{}); 122 } catch(Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 } 127 128 void sbench1() { 129 for(int i=0; i<n; i++) { 130 m1(); 131 } 132 } 133 134 public void m1() { 135 int i=0; 136 } 137 138 139 142 void bench2() { 143 for(int i=0; i<n; i++) { 144 try { 145 m2.invoke(this,new Object []{new Integer (0)}); 146 } catch(Exception e) { 147 e.printStackTrace(); 148 } 149 } 150 } 151 152 void sbench2() { 153 for(int i=0; i<n; i++) { 155 m2(0); 156 } 157 } 158 159 public void m2(int a) { 160 int i=0; 161 } 162 163 166 void bench3() { 167 for(int i=0; i<n; i++) { 168 try { 169 m3.invoke(this,new Object []{new Integer (0),new Integer (0)}); 170 } catch(Exception e) { 171 e.printStackTrace(); 172 } 173 } 174 } 175 176 void sbench3() { 177 for(int i=0; i<n; i++) { 178 m3(0,0); 179 } 180 } 181 182 public void m3(int a, int b) { 183 int i=0; 184 } 185 186 189 void bench4() { 190 for(int i=0; i<n; i++) { 191 try { 192 m4.invoke(this,new Object []{}); 193 } catch(Exception e) { 194 e.printStackTrace(); 195 } 196 } 197 } 198 199 void sbench4() { 200 for(int i=0; i<n; i++) { 201 m4(); 202 } 203 } 204 205 public int m4() { 206 return 0; 207 } 208 209 212 void bench5() { 213 for(int i=0; i<n; i++) { 214 try { 215 m5.invoke(this,new Object []{new Integer (0)}); 216 } catch(Exception e) { 217 e.printStackTrace(); 218 } 219 } 220 } 221 222 void sbench5() { 223 for(int i=0; i<n; i++) { 224 m5(0); 225 } 226 } 227 228 public int m5(int a) { 229 return 0; 230 } 231 232 235 void bench6() { 236 for(int i=0; i<n; i++) { 237 try { 238 m6.invoke(this,new Object []{new Integer (0),new Integer (0)}); 239 } catch(Exception e) { 240 e.printStackTrace(); 241 } 242 } 243 } 244 245 void sbench6() { 246 for(int i=0; i<n; i++) { 247 m6(0,0); 248 } 249 } 250 251 public int m6(int a, int b) { 252 return 0; 253 } 254 255 258 void bench7() { 259 for(int i=0; i<n; i++) { 260 try { 261 m7.invoke(this,new Object []{null}); 262 } catch(Exception e) { 263 e.printStackTrace(); 264 } 265 } 266 } 267 268 void sbench7() { 269 for(int i=0; i<n; i++) { 270 m7(null); 271 } 272 } 273 274 public void m7(Object a) { 275 return; 276 } 277 278 279 282 void bench8() { 283 for(int i=0; i<n; i++) { 284 try { 285 m8.invoke(this,new Object []{null,null}); 286 } catch(Exception e) { 287 e.printStackTrace(); 288 } 289 } 290 } 291 292 void sbench8() { 293 for(int i=0; i<n; i++) { 294 m8(null,null); 295 } 296 } 297 298 public void m8(Object a, Object b) { 299 return; 300 } 301 302 } 303 | Popular Tags |