‘Stupid’ Question 9: How does reflection affect performance, and should I avoid using it?
New clients that I train always want to focus on performance from the start, I tell them it is more important to build a solid base using good technique so you can grow into the movement – physically and psychological. Think about performance when it is needed and only if you know what you are doing. Good technique in itself should result in performance improvement and is therefore more rewarding than just pushing to push.
Okey, so I’ve been asked to please keep posting workout pictures so I’ll throw in a couple a week and I’ll see how the response is (I really appreciate the emails, I haven’t had time to answer - but I will, to every single one :) - I just don’t want to do it when I am in a hurry as I want to give proper replies). The pictures are from a run earlier today, in the pouring rain. Did a 50 min run, and then we did some outdoor strength training.
Now! A very important question, How does reflection affect performance, and should I avoid using it? I got so many great comments on my what is reflection post that I’ve decided to make this one a separate question and post the comments here.
The answer is , as I’ve understood it, that reflection does slightly affect performance - but VERY little. If performance isn’t a big issue and using reflection will allow you to write prettier code that isesier to read and maintain - then go for it.
While doing one-arm pushups is fun and is a great party trick, nobody will be impressed when you dislocate your shoulder and neither will this necessarily make you stronger. But if you know what you are doing, and you can make it work- well, then it works- doesn’t it?
Esko Luontola says: July 22, 2012 at 5:08 pm
Though reflection is slower than non-reflection, we are only talking about times in the nanosecond range. For example it calling a method normally takes 1 ns, with reflection it might take 3-5 ns. As always with performance: profile and find your bottleneck before optimizing. Keep the code clean and it will be easy to optimize later where it matters.
Reflection is good for handling situations which would otherwise be impossible to implement, or would require lots of boilerplate code. There are ways to do things faster than reflection (for example code generation at runtime or compile time), but those are much harder to use than reflection. (Based on my recent experiences, using bytecode manipulation or source code generation requires in the order of 10 times more work than using reflection.)
Some years ago I was practicing reflection (and TDD) by writing an application which can take as input a string “foo abc 123″ and then using reflection does the method call
foo("abc", 123)
tsells says:
July 23, 2012 at 5:26 pm (Edit)
Here is a good use of reflection along with some performance numbers.
James Curran says:
July 24, 2012 at 7:43 am
I’ve long felt the Reflection get a bad name due to it’s frequent misuse. There’s is nothing inherently slow about reflection; the problem is that it is often used for things that could be done much faster using inheritance/polymorphism or method overloading.
Daniel COHEN-ZARDI says: July 24, 2012 at 2:51 pm
Though powerful when used properly, reflection is often over-used.
The main issue is not performance, it is the fact that using a “dynamic” approach might make your software more robust and harder to maintain.
Once you get an error in production, it is much more complex to support and debug than a static approach.
In my view, to some extent, it is a little bit like using languages that have no types (or interpreted languages versus compiled languages). It is very powerful and flexible but has drawbacks when overly used.
SondreB says: July 27, 2012 at 12:57 am
As others have mentioned, reflection does not come with a very hard performance penalty, at least not these days. It depends on what you are doing.
The question is the trade off you need to do: Should you use reflection to save time, write less code and have less bugs – or write more code, possibly more bugs?
Comments
The general statement "Reflection is slow" is rather silly. Slow compared to what? Getting information via reflection is orders of magnitude faster that get info from a database or webservice, but people rarely say "Don't use a database, it's slow". The main problem with reflection is that developers often use it as a "quick way out" when they are confront with a problem that they don't full understand or didn't properly plan for, where it's generally quite a bit slower than a properly designed solution. As such, use of reflection became a "code smell" : If a programmer used it, it usually meant that he'd done something wrong.
@GYelland: as you formulated some may think that dynamic type has to do with reflection and as far as i know it has nothign to do with it. @iris: great workout pix! what were we talking about... uh right! reflection is something quite cool that allows you to do a number of hard/impossible things. for example it's used extensively in asp.net in databinding controls to do the binding between a data row and the control row. i once, when i was younger (2 years ago :)) was working on a nhibernate/asp.net/sql project. nhibernate did not allow me to get the actual query that was built, as i wanted to wrap that in a CTE expression. there was a way however... to call an internal method, so using reflection i was able to do it. most of the guys in the team thought i was mad but that code works even now in production. :) what i did is in the "mad computer scientist" realm and i do not recomend it, but it's an example on what i could do that otherwise was impossible (besides getting the nhibernate sources and doing my own build but that would be even crazier). now for the actual question... be really careful when using reflection because it's a lot slower than doing it the normal way. sure for a few cycles it does not matter if it's 0.2ms vs 50ms but if you use it extensively then it's bad. it's a also a sign of code smell as James pointed out. so be careful when using it and use it as a last resort.
@Cosmin.Net I didn't say dynamics was to do with reflection just tried to state an example of an area of c# lanaguage that people could argue against. As dynamics is slower than using a normal property setter. People could also argue that if dynamics is slower than using a normal propert setter why use it! Then there is MEF in .net which I pressume is using reflection under the bonnet - maybe wrong there - but people could also argue why would I ever need to use this framework as it would be too slow.
Last modified on 2012-07-27