File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reflection is a performance issue Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Reflection is a performance issue " Watch "Reflection is a performance issue " New topic
Author

Reflection is a performance issue

sonalika kulkarni
Greenhorn

Joined: Dec 10, 2010
Posts: 16
When the reflection is used the performance on code involving reflection can decrease even to 10% of the perfomance of a non reflection code.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12266
    
    1
What is the purpose of your post?

Are you reporting your own experimental results? Repeating a rumor you heard somewhere? Looking for an argument? Just being annoying?


Bill

Java Resources at www.wbrogden.com
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3359

Where is the question?
sonalika kulkarni
Greenhorn

Joined: Dec 10, 2010
Posts: 16
Sorry I missed that.
My question is why and how reflection causes performance issue making it down by 90%
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Says who? Do you have a quote for that?
Anything besides writing optimized machine level code will be slower and therefore everything is a tradeoff.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9940
    
    6

sonalika kulkarni wrote:When the reflection is used the performance on code involving reflection can decrease even to 10% of the perfomance of a non reflection code.

Poorly written code is a bigger factor than any specific technology. It's very hard to compare things like this, since someone may be a brilliant coder when not using reflection, but horrible at using it (and may not even know it). So, they write terrific code one way, and VASTLY inefficient code the other way, then make blanket statements like this.

They are completely meaningless without something to back them up, and even with it you have to carefully analyze what that is.


Never ascribe to malice that which can be adequately explained by stupidity.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12266
    
    1
Consider the various ways reflection might be used:

If you use reflection to find a method that simply returns a value, sure reflection is a big part of the execution time. But if the method goes off and does something complicated, reflection is insignificant.

Bill
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

I don't know if it is 10 times as slow, but it is still true that calling a method by reflection is slower than calling a method directly.

Note that to call the method via reflection, you have to make several method calls. First, you have to lookup the method (line 8 - although you need to do this only once when you call the method multiple times). Then you have to call it (line 9), for which you have to call Method.invoke() which will do a number of checks (for example security and access checks), and then it calls your method. When you call the method directly, it's just that one method call, without checks etc. around it - so it is less work, and is therefore faster.

Also, normal method calls can be optimized by the JVM. The JVM will for example inline frequently called methods, so that invoking the method essentially costs nothing at all. Calls via reflection cannot be inlined or optimized away like this and will always have some overhead.

Whether this is an issue for your application depends on the usage, as William already suggested.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

I was reading about this last day. It is really a little bit slower if you invoke the method "scream" instead of doing "children.scream();".

But 90% slower? I believe that there is something wrong in the code.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reflection is a performance issue
 
Similar Threads
question about using reflection
Reflection: Performance vs Convenience
Do IDEs use Reflection API
Reflection: Performance vs Convenience
how to get static members of a class