<p>I constantly hear how bad reflection is to use. While I generally avoid reflection and rarely find situations where it is impossible to solve my problem without it, I was wondering...   </p>

<p>For those who have used reflection in applications, have you measured performance hits and, is it really so bad?</p>

[{'answer_id': 25465, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 8, 'selected': True, 'text': '<p>It is. But that depends on what you\'re trying to do. </p>\n\n<p>I use reflection to dynamically load assemblies (plugins) and its performance "penalty" is not a problem, since the operation is something I do during startup of the application.</p>\n\n<p>However, if you\'re reflecting inside a series of nested loops with reflection calls on each, I\'d say you should revisit your code :)</p>\n\n<p>For "a couple of time" operations, reflection is perfectly acceptable and you won\'t notice any delay or problem with it. It\'s a very powerful mechanism and it is even used by .NET, so I don\'t see why you shouldn\'t give it a try. </p>\n'}, {'answer_id': 25472, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 2, 'selected': False, 'text': '<p>As with everything, it\'s all about assessing the situation. In <a href="http://en.wikipedia.org/wiki/DotNetNuke" rel="nofollow noreferrer">DotNetNuke</a> there\'s a fairly core component called <code>FillObject</code> that uses reflection to populate objects from datarows.</p>\n\n<p>This is a fairly common scenario and there\'s an article on MSDN, <em><a href="http://msdn.microsoft.com/en-us/library/aa478957.aspx" rel="nofollow noreferrer">Using Reflection to Bind Business Objects to ASP.NET Form Controls</a></em> that covers the performance issues.</p>\n\n<p>Performance aside, one thing I don\'t like about using reflection in that particular scenario is that it tends to reduce the ability to understand the code at a quick glance which for me doesn\'t seem worth the effort when you consider you also lose compile time safety as opposed to strongly typed datasets or something like <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query#LINQ_to_SQL" rel="nofollow noreferrer">LINQ&nbsp;to&nbsp;SQL</a>.</p>\n'}, {'answer_id': 25474, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 2, 'selected': False, 'text': '<p>Reflection can have noticeable impact on performance if you use it for frequent object creation. I\'ve developed application based on <a href="http://www.codeplex.com/smartclient/Wiki/View.aspx?title=Composite%20UI%20Application%20Block" rel="nofollow noreferrer">Composite UI Application Block</a> which is relying on reflection heavily. There was a noticeable performance degradation related with objects creation via reflection.</p>\n\n<p>However in most cases there are no problems with reflection usage. If your only need is to inspect some assembly I would recommend <a href="http://www.mono-project.com/Cecil" rel="nofollow noreferrer">Mono.Cecil</a> which is very <a href="http://codebetter.com/blogs/patricksmacchia/archive/2008/03/18/mono-cecil-vs-system-reflection.aspx" rel="nofollow noreferrer">lightweight and fast</a></p>\n'}, {'answer_id': 25477, 'author': 'Mike Thompson', 'author_id': 2754, 'author_profile': 'https://Stackoverflow.com/users/2754', 'pm_score': 3, 'selected': False, 'text': "<p>As with all things in programming you have to balance performance cost with with any benefit gained. Reflection is an invaluable tool when used with care. I created a O/R mapping library in C# which used reflection to do the bindings. This worked fantastically well. Most of the reflection code was only executed once, so any performance hit was quite small, but the benefits were great. If I were writing a new fandangled sorting algorithm, I would probably not use reflection, since it would probably scale poorly.  </p>\n\n<p>I appreciate that I haven't exactly answered your question here. My point is that it doesn't really matter. Use reflection where appropriate. It's just another language feature that you need to learn how and when to use.</p>\n"}, {'answer_id': 29599, 'author': 'ESRogs', 'author_id': 88, 'author_profile': 'https://Stackoverflow.com/users/88', 'pm_score': 7, 'selected': False, 'text': '<p>In his talk <a href="http://weblogs.asp.net/craigshoemaker/archive/2008/06/17/the-performance-of-everyday-things.aspx" rel="noreferrer">The Performance of Everyday Things</a>, Jeff Richter shows that calling a method by reflection is about <strong>1000 times slower</strong> than calling it normally.  </p>\n\n<p>Jeff\'s tip: if you need to call the method multiple times, use reflection once to find it, then assign it to a <strong>delegate</strong>, and then call the delegate.</p>\n'}, {'answer_id': 36581, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s bad enough that you have to be worried even about reflection done internally by the .NET libraries for performance-critical code.</p>\n\n<p><strong>The following example is obsolete - true at the time (2008), but long ago fixed in more recent CLR versions. Reflection in general is still a somewhat costly thing, though!</strong></p>\n\n<p>Case in point: You should never use a member declared as "Object" in a lock (C#) / SyncLock (VB.NET) statement in high-performance code. Why? Because the CLR can\'t lock on a value type, which means that it has to do a run-time reflection type check to see whether or not your Object is actually a value type instead of a reference type.</p>\n'}, {'answer_id': 36816, 'author': 'Gaz', 'author_id': 3856, 'author_profile': 'https://Stackoverflow.com/users/3856', 'pm_score': 4, 'selected': False, 'text': '<p>My most pertinent experience was writing code to compare any two data entities of the same type in a large object model property-wise.  Got it working, tried it, ran like a dog, obviously.  </p>\n\n<p>I was despondent, then overnight realised that wihout changing the logic, I could use the same algorithm to auto-generate methods for doing the comparison but statically accessing the properties.  It took no time at all to adapt the code for this purpose and I had the ability to do deep property-wise comparison of entities with static code that could be updated at the click of a button whenever the object model changed.</p>\n\n<p>My point being:  In conversations with colleagues since I have several times pointed out that their use of reflection could be to autogenerate code to compile rather than perform runtime operations and this is often worth considering.</p>\n'}, {'answer_id': 37690, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 4, 'selected': False, 'text': '<p>Not massively. I\'ve never had an issue with it in desktop development unless, as Martin states, you\'re using it in a silly location. I\'ve heard a lot of people have utterly irrational fears about its performance in desktop development.</p>\n\n<p>In the <a href="https://en.wikipedia.org/wiki/.NET_Compact_Framework" rel="nofollow noreferrer">Compact Framework</a> (which I\'m usually in) though, it\'s pretty much <a href="https://en.wiktionary.org/wiki/anathema" rel="nofollow noreferrer">anathema</a> and should be avoided like the plague in most cases. I can still get away with using it infrequently, but I have to be really careful with its application which is way less fun. :(</p>\n'}, {'answer_id': 206697, 'author': 'Chris Pietschmann', 'author_id': 7831, 'author_profile': 'https://Stackoverflow.com/users/7831', 'pm_score': 2, 'selected': False, 'text': '<p>Reflection does not drastically slow the performance of your app. You may be able to do certain things quicker by not using reflection, but if Reflection is the easiest way to achieve some functionality, then use it. You can always refactor you code away from Reflection if it becomes a perf problem.</p>\n'}, {'answer_id': 813853, 'author': 'tsimon', 'author_id': 1685, 'author_profile': 'https://Stackoverflow.com/users/1685', 'pm_score': 1, 'selected': False, 'text': "<p>I think you will find that the answer is, it depends. It's not a big deal if you want to put it in your task-list application. It is a big deal if you want to put it in Facebook's persistence library.</p>\n"}, {'answer_id': 813885, 'author': 'David Plumpton', 'author_id': 16709, 'author_profile': 'https://Stackoverflow.com/users/16709', 'pm_score': 4, 'selected': False, 'text': "<p>If you're not in a loop, don't worry about it.</p>\n"}, {'answer_id': 905464, 'author': 'mP.', 'author_id': 56524, 'author_profile': 'https://Stackoverflow.com/users/56524', 'pm_score': 2, 'selected': False, 'text': '<p>Reflection is costly because of the many checks the runtime must make whenever you make a request for a method that matches a list of parameters. Somewhere deep inside, code exists that loops over all methods for a type, verifies its visibility, checks the return type and also checks the type of each and every parameter. All of this stuff costs time.</p>\n\n<p>When you execute that method internally theres some code that does stuff like checking you passed a compatible list of parameters before executing the actual target method.</p>\n\n<p>If possible it is always recommended that one caches the method handle if one is going to continually reuse it in the future. Like all good programming tips, it often makes sense to avoid repeating oneself. In this case it would be wasteful to continually lookup the method with certain parameters and then execute it each and everytime.</p>\n\n<p>Poke around the source and take a look at whats being done.</p>\n'}, {'answer_id': 4440657, 'author': 'grenade', 'author_id': 68115, 'author_profile': 'https://Stackoverflow.com/users/68115', 'pm_score': 6, 'selected': False, 'text': '<p>Reflection performance will depend on the implementation (repetitive calls should be cached eg: <code>entity.GetType().GetProperty("PropName")</code>). Since most of the reflection I see on a day to day basis is used to populate entities from data readers or other repository type structures I decided to benchmark performance specifically on reflection when it is used to get or set an objects properties.</p>\n\n<p>I devised a test which I think is fair since it caches all the repeating calls and only times the actual SetValue or GetValue call. All the source code for the performance test is in bitbucket at: <a href="https://bitbucket.org/grenade/accessortest" rel="noreferrer">https://bitbucket.org/grenade/accessortest</a>. Scrutiny is welcome and encouraged.</p>\n\n<p>The conclusion I have come to is that it isn\'t practical and doesn\'t provide noticeable performance improvements to remove reflection in a data access layer that is returning less than 100,000 rows at a time when the reflection implementation is done well.</p>\n\n<p><img src="https://chart.apis.google.com/chart?chs=600x300&amp;cht=lc&amp;chd=t:0,0,0,0,0,0,0,0,0,0,1,2,5,10,20,40|0,0,0,0,0,0,1,3,7,14,29,57,115,228,457,930|0,0,0,0,0,0,0,1,1,3,7,15,30,60,120,247&amp;chdl=Accessor|Reflector|Hyper&amp;chds=0,930,0,930,0,930&amp;chco=006400,BDB76B,8B0000&amp;chxt=x,x,y,y&amp;chxl=0:|1024|6711705|13422386|20133067|26843748|33554429|1:|cycles|2:|0|186|372|558|744|930|3:|sec&amp;chxp=1,50|3,50" alt="Graph of time (y) against number of entities populated(x)"></p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/25458', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/392/']