KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > reflect > TestReflectPerf


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.reflect;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.*;
21 import junit.framework.*;
22
23 public class TestReflectPerf extends net.sf.cglib.CodeGenTestCase {
24     public interface IndexOf {
25         int indexOf(String JavaDoc s, int start);
26     }
27
28     public void testReflectPerf() throws Throwable JavaDoc {
29         int iterations = 1000000;
30         System.out.println();
31         System.out.println("iteration count: " + iterations);
32
33         String JavaDoc test = "abcabcabc";
34
35         Class JavaDoc[] types = new Class JavaDoc[]{ String JavaDoc.class, Integer.TYPE };
36         Method JavaDoc indexOf = String JavaDoc.class.getDeclaredMethod("indexOf", types);
37         FastClass fc = FastClass.create(String JavaDoc.class);
38         FastMethod fm = fc.getMethod("indexOf", types);
39         int fidx = fm.getIndex();
40         Object JavaDoc[] args = new Object JavaDoc[]{ "ab", new Integer JavaDoc(1) };
41
42         IndexOf fast = (IndexOf)MethodDelegate.create(test, "indexOf", IndexOf.class);
43         
44         int result;
45         long t1 = System.currentTimeMillis();
46         for (int i = 0; i < iterations; i++) {
47             result = ((Integer JavaDoc)fc.invoke("indexOf", types, test, args)).intValue();
48         }
49         long t2 = System.currentTimeMillis();
50         for (int i = 0; i < iterations; i++) {
51             args = new Object JavaDoc[]{ "ab", new Integer JavaDoc(1) };
52             result = ((Integer JavaDoc)indexOf.invoke(test, args)).intValue();
53         }
54         long t3 = System.currentTimeMillis();
55         for (int i = 0; i < iterations; i++) {
56             result = ((Integer JavaDoc)indexOf.invoke(test, args)).intValue();
57         }
58         long t4 = System.currentTimeMillis();
59         for (int i = 0; i < iterations; i++) {
60             args = new Object JavaDoc[]{ "ab", new Integer JavaDoc(1) };
61             result = ((Integer JavaDoc)fm.invoke(test, args)).intValue();
62         }
63         long t5 = System.currentTimeMillis();
64         for (int i = 0; i < iterations; i++) {
65             result = ((Integer JavaDoc)fm.invoke(test, args)).intValue();
66         }
67         long t6 = System.currentTimeMillis();
68         for (int i = 0; i < iterations; i++) {
69             result = ((Integer JavaDoc)fc.invoke(fidx, test, args)).intValue();
70         }
71         long t7 = System.currentTimeMillis();
72         for (int i = 0; i < iterations; i++) {
73             result = fast.indexOf("ab", 1);
74         }
75         long t8 = System.currentTimeMillis();
76         for (int i = 0; i < iterations; i++) {
77             result = test.indexOf("ab", 1);
78         }
79         long t9 = System.currentTimeMillis();
80
81         System.out.println("fc = " + (t2 - t1)
82                            + "\n" + "reflect+args = " + (t3 - t2)
83                            + "\n" + "reflect = " + (t4 - t3)
84                            + "\n" + "fm+args = " + (t5 - t4)
85                            + "\n" + "fm = " + (t6 - t5)
86                            + "\n" + "fc w/idx = " + (t7 - t6)
87                            + "\n" + "delegate = " + (t8 - t7)
88                            + "\n" + "raw = " + (t9 - t8));
89     }
90     
91
92
93     public TestReflectPerf(String JavaDoc testName) {
94         super(testName);
95     }
96     
97     public static void main(String JavaDoc[] args) {
98         junit.textui.TestRunner.run(suite());
99     }
100     
101     public static Test suite() {
102         return new TestSuite(TestReflectPerf.class);
103     }
104     
105     public void perform(ClassLoader JavaDoc loader) throws Throwable JavaDoc {
106     }
107     
108     public void testFailOnMemoryLeak() throws Throwable JavaDoc {
109     }
110     
111 }
112
Popular Tags