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
posted
0
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.
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.