Why is reflection slow?

Is it because we should load class (by string for example), create instance, then search for appropriate method, pack parameters, and then just invoke method? So most time is spent on these operations instead of explicit method invocation on an object, right?


Every step you take needs to be validated every time you take it when you use reflection. For example, when you invoke a method, it needs to check whether the target is actually an instance of the declarer of the method, whether you've got the right number of arguments, whether each argument is of the right type, etc.

There's absolutely no possibility of inlining or other performance tricks.

If you're finding types or methods by name, that's at best going to involve a simple map lookup - which will be performed every time you execute it, rather than once at JIT time.

Basically there's a lot more to do. However, reflection has become a lot faster than it used to be... if you're finding it much too slow, you may well be overusing it.


As an addendum to Jon Skeet's answer above (I need more reputation in order to be able to comment.):

Reflection is dependent on CPU resources being available; if you have problem with your application being slow, reflection won't solve anything, just make it slower.

Like Java itself, reflection isn't slow any more - it is more of an old rumor ;)