all groups > vj# > december 2004 >
You're in the

vj#

group:

J# reflection performance problem


J# reflection performance problem DMS
12/6/2004 1:55:10 AM
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
Re: J# reflection performance problem Lars-Inge Tønnessen [VJ# MVP]
12/6/2004 10:46:40 PM
Here are my test results. Please notice the J#.net 2.0 beta results. =:o)



Java_J# test>java -version

java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)


Java_J# test>java ReflectionTest

Without reflection time:16
With reflection time:360



J# .net 1.1

Debug>J_reflection_test.exe

Without reflection time:0
With reflection time:141563



J# .net 2.0 beta
Debug>ConsoleApplication2.exe

Without reflection time:10
With reflection time:671



//////////////////_______________\\\\\\\\\\\\\\\\\\\\
Second run:

Java 1.4.2
Java_J# test>java ReflectionTest

Without reflection time:16
With reflection time:516


..net 1.1
Debug>J_reflection_test.exe

Without reflection time:0
With reflection time:144968



..net 2.0 beta
Without reflection time:10
With reflection time:671



Regards,
Lars-Inge Tønnessen

AddThis Social Bookmark Button