1 /***************************************************************************************
2 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package test;
9
10 /***
11 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
12 */
13 public class CastBench implements Cast {
14 private static final int NR_INVOCATIONS = 1000000000;
15
16 public void invoke() {
17 }
18
19 public void invokeInterface() {
20 }
21
22 public static void main(String[] args) {
23 CastBench bench = new CastBench();
24 for (int i = 0; i < NR_INVOCATIONS; i++) {
25 bench.invoke();
26 }
27
28 benchRegularInvoke(bench);
29 benchInterfaceInvoke(bench);
30 benchCastInvoke(bench);
31 }
32
33 private static void benchCastInvoke(CastBench bench) {
34 long start = System.currentTimeMillis();
35 for (int i = 0; i < NR_INVOCATIONS; i++) {
36 ((Cast) bench).invokeInterface();
37 }
38 long end = System.currentTimeMillis() - start;
39 double time = end / (double) NR_INVOCATIONS;
40 System.out.println("cast invoke = " + time);
41 }
42
43 private static void benchInterfaceInvoke(Cast bench) {
44 long start = System.currentTimeMillis();
45 for (int i = 0; i < NR_INVOCATIONS; i++) {
46 bench.invokeInterface();
47 }
48 long end = System.currentTimeMillis() - start;
49 double time = end / (double) NR_INVOCATIONS;
50 System.out.println("interface invoke = " + time);
51 }
52
53 private static void benchRegularInvoke(CastBench bench) {
54 long start = System.currentTimeMillis();
55 for (int i = 0; i < NR_INVOCATIONS; i++) {
56 bench.invoke();
57 }
58 long end = System.currentTimeMillis() - start;
59 double time = end / (double) NR_INVOCATIONS;
60 System.out.println("regular invoke = " + time);
61 }
62 }