4 class SubClass1 extends Object {
7 class SubClass2 extends Object {
10 SubClass2 (int aa, int ab, int ac) {
16 public void methodWithLocalAccess () {
18 for (int i=0; i<JavaPerformance.MAXCOUNT; i++) {
25 public void methodWithInstanceAccess () {
26 for (int i=0; i<JavaPerformance.MAXCOUNT; i++) {
34 class List extends Object {
36 static Random r = new Random();
42 memory = new char[512];
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);
52 public class JavaPerformance {
54 static final int MAXCOUNT = 500000;
55 static Throwable aThrowable = new Throwable ();
57 public JavaPerformance () {
63 static public int staticCall () {
67 static public final int staticFinalCall () {
71 static public synchronized int synchronizedStaticCall () {
75 static public void throwingStaticCall () throws Throwable {
76 throw new Throwable ();
81 public int methodCall () {
85 public final int finalMethodCall () {
89 static public synchronized int synchronizedMethodCall () {
93 static public void throwingMethodCall () throws Throwable {
94 throw new Throwable ();
99 public static int ggT (int x, int y) {
101 if (x<y) y = y-x; else x = x-y;
106 public static long fak (long x) {
113 public static void arithmeticTest () {
114 System.out.println ("Arithmetic time");
115 System.out.println ();
117 System.out.print ("ggT (24,42)...");
118 long l = System.currentTimeMillis();
119 for (int i = 0; i<MAXCOUNT; i++)
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 (")");
130 System.out.print ("13!...");
131 l = System.currentTimeMillis();
132 for (int i = 0; i<MAXCOUNT; i++)
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 (")");
144 public static void memoryTest () {
145 System.out.println ("Memory time");
146 System.out.println ();
148 System.out.print ("Object creation...");
149 long l = System.currentTimeMillis();
150 for (int i = 0; i<MAXCOUNT; i++)
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 (")");
161 System.out.print ("Subclass creation...");
162 l = System.currentTimeMillis();
163 for (int i = 0; i<MAXCOUNT; i++)
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 (")");
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 (")");
187 System.out.print ("List generation (GC test)...");
188 l = System.currentTimeMillis();
189 for (int i = 0; i<250; i++)
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 (")");
202 public static void methodTest () {
203 SubClass2 object = new SubClass2 (1,2,3);
205 System.out.println ("Method execution time");
206 System.out.println ();
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 (")");
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 (")");
233 public static void callTest () {
234 JavaPerformance object = new JavaPerformance ();
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++)
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 (")");
251 System.out.print ("Static final function call...");
252 l = System.currentTimeMillis();
253 for (int i = 0; i<MAXCOUNT; i++)
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 (")");
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 (")");
277 System.out.print ("Static function call in try catch block...");
278 l = System.currentTimeMillis();
279 for (int i = 0; i<MAXCOUNT; i++)
282 } catch (Throwable a) {
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 (")");
293 System.out.print ("Static exception throwing function call...");
294 l = System.currentTimeMillis();
295 for (int i = 0; i<5000; i++)
297 throwingStaticCall ();
298 } catch (Throwable exp) {
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 (")");
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 (")");
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 (")");
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 (")");
349 System.out.print ("Method call in try catch block...");
350 l = System.currentTimeMillis();
351 for (int i = 0; i<MAXCOUNT; i++)
353 object.methodCall ();
354 } catch (Throwable a) {
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 (")");
365 System.out.print ("Exception throwing method call...");
366 l = System.currentTimeMillis();
367 for (int i = 0; i<50000; i++)
369 object.throwingMethodCall ();
370 } catch (Throwable exp) {
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 (")");
382 public static final void main (String[] args) {
383 long l = System.currentTimeMillis();
385 System.out.println ();
387 System.out.println ();
389 System.out.println ();
391 System.out.println ();
392 System.out.print ("Overall time ");
393 System.out.print(System.currentTimeMillis()-l);
394 System.out.println ("ms");