开发者

What Performs Worse: Reflection or Boxing?

开发者 https://www.devze.com 2022-12-20 05:06 出处:网络
I\'m working on creating my own DI framework that creates delegate factories as a learning exercise.My way of building typed delegates is to use expressions to create a function that calls a static me

I'm working on creating my own DI framework that creates delegate factories as a learning exercise. My way of building typed delegates is to use expressions to create a function that calls a static method with reference to my container and any constructor params.

This thrown up an interesting question with regards to value types. Which is the most per开发者_Go百科formant:

a) Using reflection to select a static generic method with the correct number of parameters then use MakeGenericMethod to remove the generics

b) Go for the old fashion params Object[] and take the hit on boxing?


IME, boxing time is nothing compared to reflection.


I'd guess that reflection would be alot slower, probably orders of magintude so.

It's pretty easy to bench though, give it a go and post your results :)


In this case, boxing will be orders of magnitude faster than reflection.

Of course, you could always cache the reflection results.


In general, I would say even if boxing had been slower (to an extent not noticeable), its the right way to go. Reflection is a tool to facilitate some sorta meta-programming - when you have to do some work over the code itself, and not to facilitate your applications business logic, and hence you shouldn't use it without a good reason. A programmer should think from the physical domain first. That said, in your case it probably doesn't matter since you're already going the meta way I think. Using object still gives you compile time safety to certain extent and better maintenance.

As others have said, reflection is the slower one here (unless you dont cache). Another thing that comes to boxing's favour is that you are most probably anyway boxing when dealing with reflection. The reflection API always deals with object, so if you are getting back some instance value you have to unbox. Similarly, calling GetType on a value type instance first boxes it to object which you may have to if you don't have the type argument, but only the instance.

But a better alternative is to rely on generics. Some nice patterns are detailed here.


If one has to process a million items, boxing each item will be less efficient than processing them without boxing, but will be much faster than using Reflection to process the type of each item.

On the other hand, in many scenarios it will be possible to process a million items of some generic type T by using Reflection once, on type T, to construct an object which can process something of type T without boxing, and then cache the result of that for the life of the program. This is how things things like EqualityComparer<T>.Default work. Such an approach can easily be more than an order of magnitude faster than boxing each item.

0

精彩评论

暂无评论...
验证码 换一张
取 消