we know that with the help of reflection we can create instance of a class dynamically at run time and can call the method of the class very easily. so this point reflection is late binding because action is taken at run time. so i just want know reflection is faster or not. what is the performance of reflection. is it good or bad...is it resource hungry. please discuss. thnks.
Asked
Active
Viewed 321 times
2 Answers
4
Technically speaking reflection is a performance hit. But if you're doing something that needs it then you have to use it. If you can go without it, avoid it.
EDIT
To further emphasize, reflection is neither good or bad. Its in the Framework because there's very legitimate reasons to use it. That said, 90% of the time that I see someone using reflection they're trying to do something the hard way, not knowing the easy route. Often its because they don't know about generics.

Chris Haas
- 53,986
- 12
- 141
- 274
-
thanks for your answer but would please discuss why reflection is performance issue in detail or should avoid it in detail. – Thomas Apr 21 '11 at 18:39
-
@Thomas: It's the difference between the runtime having to figure out what methods a given unknown class even has vs "knowing" it because it was compiled in. – NotMe Apr 21 '11 at 18:41
-
@Thomas, check out the post below. Its about java but it holds true for .Net. http://stackoverflow.com/questions/1392351/java-reflection-why-is-it-so-bad/1392362#1392362 – Chris Haas Apr 21 '11 at 18:43
-
Compare with driving a car. Once you _compiled in_ which pedal is for acceleration and which for brakes, you can do it intuitively and performant. While still reflecting about it - thinking which pedal did which thing - you are slower and kind of security risk ;) – ZoolWay Apr 21 '11 at 18:48
2
Generally, the performance of reflection is worse than when you do the same thing without reflection. But whether it is too slow for you depends on what your performance requirements are (do you need it to be fast) and what exactly are you doing.

svick
- 236,525
- 50
- 385
- 514
-
depending on situation sometime we may have to use reflection. i just want to know is it fast or slow in whatever situation we use. it is late binding and i think it is slow. am i right svick? – Thomas Apr 21 '11 at 19:24
-
It could be slow. Why do you care? If your application is slow, profile it find out what really slows it down. You really can't say that X is slow or fast, it always depends on circumstances. – svick Apr 21 '11 at 19:50
-
What @svick is saying is very important. If you have to use it, then do so. If there's no alternative then performance doesn't come into it really. – Chris Haas Apr 21 '11 at 19:53