Tuesday, August 10, 2010

Raw reflection vs java.beans.Expression

I have been just curious if java.beans.Expression and java.beans.Statement
are just convenience APIs over raw reflection APIs
or if those classes really bring up some "performance" or other boost.

First testcase:


java beans API:

String s1=(String)(new Expression(b1,"method1",args)).getValue()


raw reflection:

String s2=(String)b1.getClass().getMethod("method1",new Class[]{Date.class}).invoke(b1,args);


"Cached reflected method":

Method m=b1.getClass().getMethod("method1",new Class[]{Date.class});
for(..)
String s3=(String)m.invoke(b1,args);


Results:

360 [100000,loops,ms]
238 [100000,loops,ms]
105 [100000,loops,ms]

java.beans api seems to be the slowest.
Even if Expression is designed with statefull API,
which provokes "the intend to reuse same Expression object"
and change only arguments for example and keep target and method the same,
the Statement.invoke() code
seems to do all reflection work again and again
without any optimization and many extra ifs.

Just Another useless testcase for
standardized and useless convenience API ?

No comments:

Post a Comment

Post a Comment