vj#:
Hi All.
I have the following java code:
ReflectionTest.java
package com.test;
import java.lang.reflect.*;
public class ReflectionTest {
public static void main(String[] args){
try{
ReflectionTestClass c = new ReflectionTestClass();
int count = 1000000;
Integer a = new Integer(5);
Integer b = new Integer(10);
long before = System.currentTimeMillis();
for(int i = 0; i < count; i++){
// c.sum(a, b);
a = c.a;
}
long after = System.currentTimeMillis();
System.out.println("Without reflection time:" + (after - before));
Method m = ReflectionTestClass.class.getMethod("sum", new
Class[]{Integer.class, Integer.class});
Field f = ReflectionTestClass.class.getField("a");
Object[] obj_arr = new Object[]{a, b};
before = System.currentTimeMillis();
for(int i = 0; i < count; i++){
// m.invoke(c, obj_arr);
a = (Integer)f.get(c);
}
after = System.currentTimeMillis();
System.out.println("With reflection time:" + (after - before));
} catch(Exception e){
e.printStackTrace();
}
}
}
ReflectionTestClass.java
package com.test;
public class ReflectionTestClass {
public Integer a = new Integer(55);
public int sum(Integer a, Integer b){
return a.intValue() + b.intValue();
}
}
For getting a public field value the results are:
JDK1.3 - w/o reflection - 15ms, with reflection - 250ms
J# - w/o reflection - 15ms, with reflection - 59000ms!!!!!
For invoking a simple method:
JDK1.3 - w/o reflection - 15ms, with reflection - 1300ms
J# - w/o reflection - 15ms, with reflection - 25000ms!!!!!
Can somebody explain the results? (Microsoft maybe?)
It seems J# reflection is not useful at all.
10x