Coder Perfect

Is.NET reflection expensive?


I’m constantly hearing how ineffective reflection is. While I usually avoid introspection and only come across situations where I can’t solve my problem without it, I was curious…

Have you tested performance hits from using reflection in applications, and is it really that bad?

Asked by Dan Herbert

Solution #1

Jeff Richter demonstrates in his talk The Performance of Everyday Things that invoking a method through reflection is 1000 times slower than doing it ordinarily.

If you need to call the method numerous times, use reflection to locate it once, then assign it to a delegate, and finally call the delegate.

Answered by ESRogs

Solution #2

Yes, it is. But it depends on what you’re attempting to do.

I utilize reflection to dynamically load assemblies (plugins), and the performance “penalty” isn’t an issue because the action occurs at the application’s startup.

I’d think you should review your code if you’re reflecting inside a series of nested loops with reflection calls on each one:)

Reflection is completely appropriate for “a couple of time” processes, and you won’t notice any delay or problems. It’s a pretty powerful mechanism that even.NET uses, so I don’t see why you shouldn’t try it.

Answered by Martin Marconcini

Solution #3

The performance of reflection will be determined by the implementation (repetitive calls, such as entity, should be cached). GetType(). GetProperty(“PropName”)). Because the majority of the reflection I encounter on a daily basis is used to populate entities with data from data readers or other repository-type structures, I chose to test performance when reflection is used to get or set an object’s properties.

I came up with a test that I believe is fair because it caches all recurring calls and only times the actual SetValue or GetValue call. The performance test source code can be found at Scrutiny is encouraged and welcomed.

When the reflection implementation is done effectively, I’ve come to the opinion that removing reflection in a data access layer that returns less than 100,000 rows at a time isn’t realistic and doesn’t deliver noticeable speed advantages.

The graph above demonstrates the output of my little benchmark and shows that mechanisms that outperform reflection, only do so noticeably after the 100,000 cycles mark. Most DALs only return several hundred or perhaps thousands of rows at a time and at these levels reflection performs just fine.

Answered by grenade

Solution #4

Don’t be concerned if you aren’t in a loop.

Answered by David Plumpton

Solution #5

Not by a long shot. In desktop development, I’ve never had an issue with it unless, as Martin points out, you’re utilizing it in an inappropriate position. Many people have expressed completely unjustified concerns about its performance in desktop development.

It’s almost anathema in the Compact Framework (which I’m generally in), and should be avoided like the plague in most circumstances. I can still get away with using it seldom, but I have to be very careful with how I use it, which is a lot less enjoyable.

Answered by Quibblesome

Post is based on