* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / tests / JavaPerformance.java
1 import java.util.*;
2 import java.io.*;
3
4 class SubClass1 extends Object {
5 }
6
7 class SubClass2 extends Object {
8   int a,b,c;
9
10   SubClass2 (int aa, int ab, int ac) {
11     a = aa;
12     b = ab;
13     c = ac;
14   }
15
16   public void methodWithLocalAccess () {
17     int la,lb,lc;
18     for (int i=0; i<JavaPerformance.MAXCOUNT; i++) {
19       la = 5;
20       lb = 6;
21       lc = 7;
22     }
23   }
24
25   public void methodWithInstanceAccess () {
26     for (int i=0; i<JavaPerformance.MAXCOUNT; i++) {    
27       a = 5;
28       b = 6;
29       c = 7;
30     }
31   }  
32 }
33
34 class List extends Object {
35
36   static Random r = new Random();
37   
38   List[] data;
39   char[] memory;
40   
41   List (int depth) {
42     memory = new char[512];
43     if (depth>0) {
44       int length = 1+Math.abs(r.nextInt())%5;
45       data = new List[length];
46       for (int i=0; i<length; i++)
47         data[i] = new List (depth-1);
48     }
49   }
50 }
51
52 public class JavaPerformance {
53
54   static final int MAXCOUNT = 500000;  
55   static Throwable aThrowable = new Throwable ();
56   
57   public JavaPerformance () {
58     super();
59   }
60
61   // static
62   
63   static public int staticCall () {
64     return 0;
65   }
66
67   static public final int staticFinalCall () {
68     return 0;
69   }
70   
71   static public synchronized int synchronizedStaticCall () {
72     return 0;
73   }
74   
75   static public void throwingStaticCall () throws Throwable {
76     throw new Throwable ();
77   }
78
79   // methods
80   
81   public int methodCall () {
82     return 0;
83   }
84
85   public final int finalMethodCall () {
86     return 0;
87   }
88
89   static public synchronized int synchronizedMethodCall () {
90     return 0;
91   }
92   
93   static public void throwingMethodCall () throws Throwable {
94     throw new Throwable ();
95   }
96
97   // ggT
98
99   public static int ggT (int x, int y) {
100     while (x!=y) {
101       if (x<y) y = y-x; else x = x-y;
102     }
103     return x;
104   }
105
106   public static long fak (long x) {
107     if (x>1)
108       return x*fak(x-1);
109     else
110       return 1;
111   }
112   
113   public static void arithmeticTest () {
114     System.out.println ("Arithmetic time");
115     System.out.println ();
116
117     System.out.print ("ggT (24,42)...");
118     long l = System.currentTimeMillis();
119     for (int i = 0; i<MAXCOUNT; i++)
120       ggT (24,42);
121     long e = System.currentTimeMillis();
122     System.out.print ("(");
123     System.out.print (((double) e-l)/MAXCOUNT);
124     System.out.print ("ms ");    
125     System.out.print(e-l);
126     System.out.print ("ms/");
127     System.out.print (MAXCOUNT);
128     System.out.println (")");
129
130     System.out.print ("13!...");
131     l = System.currentTimeMillis();
132     for (int i = 0; i<MAXCOUNT; i++)
133       fak (13);
134     e = System.currentTimeMillis();    
135     System.out.print ("(");
136     System.out.print (((double) e-l)/MAXCOUNT);
137     System.out.print ("ms ");    
138     System.out.print(e-l);
139     System.out.print ("ms/");
140     System.out.print (MAXCOUNT);
141     System.out.println (")");    
142   }
143
144   public static void memoryTest () {
145     System.out.println ("Memory time");
146     System.out.println ();
147
148     System.out.print ("Object creation...");
149     long l = System.currentTimeMillis();
150     for (int i = 0; i<MAXCOUNT; i++)
151       new Object ();
152     long e = System.currentTimeMillis();        
153     System.out.print ("(");
154     System.out.print (((double) e-l)/MAXCOUNT);
155     System.out.print ("ms ");    
156     System.out.print(e-l);
157     System.out.print ("ms/");
158     System.out.print (MAXCOUNT);
159     System.out.println (")");
160
161     System.out.print ("Subclass creation...");
162     l = System.currentTimeMillis();
163     for (int i = 0; i<MAXCOUNT; i++)
164       new SubClass1 ();
165     e = System.currentTimeMillis();        
166     System.out.print ("(");
167     System.out.print (((double) e-l)/MAXCOUNT);
168     System.out.print ("ms ");    
169     System.out.print(e-l);
170     System.out.print ("ms/");
171     System.out.print (MAXCOUNT);
172     System.out.println (")");    
173
174     System.out.print ("Subclass creation with constructor call...");
175     l = System.currentTimeMillis();
176     for (int i = 0; i<MAXCOUNT; i++)
177       new SubClass2 (1,2,3);
178     e = System.currentTimeMillis();        
179     System.out.print ("(");
180     System.out.print (((double) e-l)/MAXCOUNT);
181     System.out.print ("ms ");    
182     System.out.print(e-l);    
183     System.out.print ("ms/");
184     System.out.print (MAXCOUNT);    
185     System.out.println (")");    
186
187     System.out.print ("List generation (GC test)...");
188     l = System.currentTimeMillis();
189     for (int i = 0; i<250; i++)
190       new List (5);
191     e = System.currentTimeMillis();        
192     System.out.print ("(");
193     System.out.print (((double) e-l)/250);
194     System.out.print ("ms ");    
195     System.out.print(e-l);
196     System.out.print ("ms/");
197     System.out.print (250);    
198     System.out.println (")");    
199     
200   }
201
202   public static void methodTest () {
203     SubClass2 object = new SubClass2 (1,2,3);
204     
205     System.out.println ("Method execution time");
206     System.out.println ();
207     
208     System.out.print ("Method with access only to local variables...");
209     long l = System.currentTimeMillis();
210     object.methodWithLocalAccess ();
211     long e = System.currentTimeMillis();        
212      System.out.print ("(");
213     System.out.print (((double) e-l)/MAXCOUNT);
214     System.out.print ("ms ");     
215      System.out.print(e-l);
216     System.out.print ("ms/");
217     System.out.print (MAXCOUNT);     
218      System.out.println (")");
219
220      System.out.print ("Method with access only to instance variables...");
221      l = System.currentTimeMillis();
222      object.methodWithInstanceAccess ();
223      e = System.currentTimeMillis();         
224      System.out.print ("(");
225     System.out.print (((double) e-l)/MAXCOUNT);
226     System.out.print ("ms ");     
227      System.out.print(e-l);
228     System.out.print ("ms/");
229     System.out.print (MAXCOUNT);     
230      System.out.println (")");
231   }
232   
233   public static void callTest () {
234     JavaPerformance object = new JavaPerformance ();
235
236     System.out.println ("Calling time");
237     System.out.println ();
238     System.out.print ("Static function call...");
239     long l = System.currentTimeMillis();
240      for (int i = 0; i<MAXCOUNT; i++)
241        staticCall ();
242      long e = System.currentTimeMillis();         
243      System.out.print ("(");
244     System.out.print (((double) e-l)/MAXCOUNT);
245     System.out.print ("ms ");     
246      System.out.print(e-l);
247     System.out.print ("ms/");
248     System.out.print (MAXCOUNT);     
249      System.out.println (")");
250
251     System.out.print ("Static final function call...");
252      l = System.currentTimeMillis();
253      for (int i = 0; i<MAXCOUNT; i++)
254        staticFinalCall ();
255      e = System.currentTimeMillis();         
256      System.out.print ("(");
257     System.out.print (((double) e-l)/MAXCOUNT);
258     System.out.print ("ms ");     
259      System.out.print(e-l);
260     System.out.print ("ms/");
261     System.out.print (MAXCOUNT);     
262      System.out.println (")");
263
264      System.out.print ("Static synchronized function call...");
265      l = System.currentTimeMillis();
266      for (int i = 0; i<MAXCOUNT; i++)
267        synchronizedStaticCall();
268      e = System.currentTimeMillis();         
269      System.out.print ("(");
270     System.out.print (((double) e-l)/MAXCOUNT);
271     System.out.print ("ms ");     
272      System.out.print(e-l);
273     System.out.print ("ms/");
274     System.out.print (MAXCOUNT);     
275      System.out.println (")");
276      
277      System.out.print ("Static function call in try catch block...");
278      l = System.currentTimeMillis();
279      for (int i = 0; i<MAXCOUNT; i++)
280        try {
281          staticCall ();
282        } catch (Throwable a) {
283        }
284      e = System.currentTimeMillis();         
285      System.out.print ("(");
286     System.out.print (((double) e-l)/MAXCOUNT);
287     System.out.print ("ms ");     
288      System.out.print(e-l);
289     System.out.print ("ms/");
290     System.out.print (MAXCOUNT);     
291      System.out.println (")");
292     
293     System.out.print ("Static exception throwing function call...");
294     l = System.currentTimeMillis();
295     for (int i = 0; i<5000; i++)
296       try {
297         throwingStaticCall ();
298       } catch (Throwable exp) {
299       }
300     e = System.currentTimeMillis();        
301     System.out.print ("(");
302     System.out.print (((double) e-l)/5000);
303     System.out.print ("ms ");    
304     System.out.print(e-l);
305     System.out.print ("ms/");
306     System.out.print (5000);
307     System.out.println (")");
308
309
310     System.out.print ("Method call...");
311     l = System.currentTimeMillis();
312      for (int i = 0; i<MAXCOUNT; i++)
313        object.methodCall ();
314      e = System.currentTimeMillis();         
315      System.out.print ("(");
316     System.out.print (((double) e-l)/MAXCOUNT);
317     System.out.print ("ms ");     
318      System.out.print(e-l);
319     System.out.print ("ms/");
320     System.out.print (MAXCOUNT);     
321      System.out.println (")");
322
323     System.out.print ("Final method call...");
324     l = System.currentTimeMillis();
325      for (int i = 0; i<MAXCOUNT; i++)
326        object.finalMethodCall ();
327      e = System.currentTimeMillis();         
328      System.out.print ("(");
329     System.out.print (((double) e-l)/MAXCOUNT);
330     System.out.print ("ms ");     
331      System.out.print(e-l);
332     System.out.print ("ms/");
333     System.out.print (MAXCOUNT);     
334      System.out.println (")");
335
336      System.out.print ("Synchronized method call...");
337      l = System.currentTimeMillis();
338      for (int i = 0; i<MAXCOUNT; i++)
339        object.synchronizedMethodCall();
340      e = System.currentTimeMillis();         
341      System.out.print ("(");
342     System.out.print (((double) e-l)/MAXCOUNT);
343     System.out.print ("ms ");     
344      System.out.print(e-l);
345     System.out.print ("ms/");
346     System.out.print (MAXCOUNT);     
347      System.out.println (")");
348      
349      System.out.print ("Method call in try catch block...");
350      l = System.currentTimeMillis();
351      for (int i = 0; i<MAXCOUNT; i++)
352        try {
353          object.methodCall ();
354        } catch (Throwable a) {
355        }
356      e = System.currentTimeMillis();         
357      System.out.print ("(");
358     System.out.print (((double) e-l)/MAXCOUNT);
359     System.out.print ("ms ");     
360      System.out.print(e-l);
361     System.out.print ("ms/");
362     System.out.print (MAXCOUNT);     
363      System.out.println (")");
364     
365      System.out.print ("Exception throwing method call...");
366      l = System.currentTimeMillis();
367      for (int i = 0; i<50000; i++)
368        try {
369          object.throwingMethodCall ();
370        } catch (Throwable exp) {
371        }
372      e = System.currentTimeMillis();         
373      System.out.print ("(");
374     System.out.print (((double) e-l)/5000);
375     System.out.print ("ms ");     
376      System.out.print(e-l);
377     System.out.print ("ms/");
378     System.out.print (5000);
379      System.out.println (")");
380   }
381
382   public static final void main (String[] args) {
383     long l = System.currentTimeMillis();    
384     arithmeticTest ();
385     System.out.println ();
386     callTest ();
387     System.out.println ();
388     methodTest ();
389     System.out.println ();    
390     memoryTest ();
391     System.out.println ();
392     System.out.print ("Overall time ");
393     System.out.print(System.currentTimeMillis()-l);
394     System.out.println ("ms");
395   }
396 }