<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Alex Sarafian as Developer &#187; Performance</title>
	<atom:link href="http://sarafianalex.wordpress.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://sarafianalex.wordpress.com</link>
	<description>Adventures in C# Land and some SQL</description>
	<lastBuildDate>Fri, 04 Sep 2009 11:27:50 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='sarafianalex.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ea38bb69738d229a89ab00b82aad64b7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Alex Sarafian as Developer &#187; Performance</title>
		<link>http://sarafianalex.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sarafianalex.wordpress.com/osd.xml" title="Alex Sarafian as Developer" />
		<item>
		<title>Reflection BenchMark</title>
		<link>http://sarafianalex.wordpress.com/2008/04/19/reflection-benchmark/</link>
		<comments>http://sarafianalex.wordpress.com/2008/04/19/reflection-benchmark/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 22:50:24 +0000</pubDate>
		<dc:creator>Sarafian Alex</dc:creator>
				<category><![CDATA[.NET C#]]></category>
		<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://sarafianalex.wordpress.com/2008/04/19/reflection-benchmark/</guid>
		<description><![CDATA[For a project I have been developing, over the last week, I have come to utilize a lot of reflection, generics and their combination using MakeGenericType.
So today last day, I began to optimize its performance based on my experience of reflection and the .net world. I have used a lot of reflection so I believe [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=14&subd=sarafianalex&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p align="justify">For a project I have been developing, over the last week, I have come to utilize a lot of reflection, generics and their combination using MakeGenericType.</p>
<p align="justify">So today last day, I began to optimize its performance based on my experience of reflection and the .net world. I have used a lot of reflection so I believe my performance hunches were correct.</p>
<p align="justify">I assumed that</p>
<ol>
<li>
<div>It is a lot quicker to have a Dictionary of Types instead of calling MakeGenericType</div>
</li>
<li>
<div>It is a lot quicker to have a dictionary of  PropertyInfo instead of using repeatetly GetPropetyInfo</div>
</li>
<li>
<div>It is a lot quicker to have a dictionary of  MethodInfo instead of using repeatetly GetMethodInfo</div>
</li>
<li>
<div>It is better to create static functions that will do your work as a single function, instead of calling through reflection the constructor and then the method.</div>
</li>
</ol>
<p align="justify">So tonight I created a Benchmarking program that proved I was right on all 4. What I didn&#8217;t expect was the great time MakeGenericType needed to execute.</p>
<p align="justify">In the <a href="http://www.codeproject.com/KB/dotnet/ReflectionOptimize/ReflectionBenchMark.zip" target="_blank">benchmark project</a> I have a TestType.cs without generics and a GenericType.cs with two generic parameters.Each class has a public constructor, an Execute method and a static ExecuteStatic which calls the constructor and the the Execute method.</p>
<p align="justify">Because the operations are pretty simple, there will be no milliseconds but average ticks.</p>
<p align="justify">The names consist of the following</p>
<ol>
<li>
<div>Test or Generic class</div>
</li>
<li>
<div>With reflection or not</div>
</li>
<li>
<div>Instantiating and calling Execute or just call static ExecuteStatic</div>
</li>
<li>
<div>Optimized or not</div>
</li>
</ol>
<p align="justify">So here are the results</p>
<blockquote>
<p align="justify">TestStatic-&gt;7<br />
TestInstance-&gt;7<br />
TestReflectionStatic-&gt;21<br />
TestReflectionInstance-&gt;33<br />
TestReflectionStaticOptimized-&gt;16<br />
TestReflectionInstanceOptimized-&gt;24<br />
GenericStatic-&gt;8<br />
GenericInstance-&gt;7<br />
GenericReflectionStatic-&gt;41<br />
GenericReflectionInstance-&gt;75<br />
GenericReflectionStaticOptimized-&gt;37<br />
GenericReflectionInstanceOptimized-&gt;67<br />
GenericReflectionMake-&gt;32<br />
GenericReflectionMakeOptimized-&gt;12</p>
</blockquote>
<p align="justify">The above example is with just one combination of generic parameters used. When I increase the number of generic combinations the results become</p>
<blockquote>
<p align="justify">TestStatic-&gt;9<br />
TestInstance-&gt;9<br />
TestReflectionStatic-&gt;22<br />
TestReflectionInstance-&gt;31<br />
TestReflectionStaticOptimized-&gt;16<br />
TestReflectionInstanceOptimized-&gt;23<br />
GenericStatic-&gt;8<br />
GenericInstance-&gt;9<br />
GenericReflectionStatic-&gt;40<br />
GenericReflectionInstance-&gt;71<br />
GenericReflectionStaticOptimized-&gt;37<br />
GenericReflectionInstanceOptimized-&gt;66<br />
GenericReflectionMake-&gt;31<br />
GenericReflectionMakeOptimized-&gt;19</p>
</blockquote>
<p align="justify">As you can see the performance weakens, probably because of the dictionary and its long keys. String manipulation in .NET is not one of its strongest features.</p>
<p align="justify">Another thing is that in my classes, there is only one method, so minor as the difference in optimization as it may seem, keep in mind that the more methods you have in a type the slower GetMethodInfo executes.</p>
<p align="justify">In my benchmark I didn&#8217;t implement test about properties but I firmly believe that they will not have any difference from methods.</p>
<p align="justify">There is no much code in the post, because the optimization is pretty forward. Just reuse Type and MethodInfo as much as possible.</p>
<p align="justify">For example the not optimized way is</p>
<blockquote>
<div><span style="color:#0000ff;">public static void </span>TestReflectionInstance() { <span style="color:#2b91af;">Type </span>type = <span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">TestType</span>); <span style="color:#0000ff;">object </span>tt = type.GetConstructor(<span style="color:#0000ff;">new </span><span style="color:#2b91af;">Type</span>[] { }).Invoke(<span style="color:#0000ff;">null</span>); type.GetMethod(<span style="color:#a31515;">&#8220;Execute&#8221;</span>).Invoke(tt, <span style="color:#0000ff;">null</span>); <span style="color:#2b91af;">Stopwatch </span>sw = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">Stopwatch</span>(); sw.Start(); <span style="color:#0000ff;">for </span>(<span style="color:#0000ff;">int </span>i = 0; i &lt; iterations; i++) { type = <span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">TestType</span>); tt = type.GetConstructor(<span style="color:#0000ff;">new </span><span style="color:#2b91af;">Type</span>[] { }).Invoke(<span style="color:#0000ff;">null</span>); type.GetMethod(<span style="color:#a31515;">&#8220;Execute&#8221;</span>).Invoke(tt, <span style="color:#0000ff;">null</span>); } sw.Stop(); <span style="color:#0000ff;">if </span>(Write) <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;TestReflectionInstance &#8221; </span>+ sw.ElapsedTicks.ToString()); times[<span style="color:#a31515;">"TestReflectionInstance"</span>] += sw.ElapsedTicks; }</div>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p align="justify">and the optimized way is</p>
<p align="justify"><a href="http://11011.net/software/vspaste"></a></p>
<blockquote>
<div><span style="color:#0000ff;">public static void </span>TestReflectionInstanceOptimized() { <span style="color:#2b91af;">Type </span>type = <span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">TestType</span>); <span style="color:#2b91af;">ConstructorInfo </span>ci = type.GetConstructor(<span style="color:#0000ff;">new </span><span style="color:#2b91af;">Type</span>[] { }); <span style="color:#0000ff;">object </span>tt = ci.Invoke(<span style="color:#0000ff;">null</span>); <span style="color:#2b91af;">MethodInfo </span>mi = type.GetMethod(<span style="color:#a31515;">&#8220;Execute&#8221;</span>); mi.Invoke(tt, <span style="color:#0000ff;">null</span>); <span style="color:#2b91af;">Stopwatch </span>sw = <span style="color:#0000ff;">new </span><span style="color:#2b91af;">Stopwatch</span>(); sw.Start(); <span style="color:#0000ff;">for </span>(<span style="color:#0000ff;">int </span>i = 0; i &lt; iterations; i++) { tt = ci.Invoke(<span style="color:#0000ff;">null</span>); mi.Invoke(tt, <span style="color:#0000ff;">null</span>); <span style="color:#008000;">//type.GetMethod(&#8220;Execute&#8221;).Invoke(tt, null); </span>} sw.Stop(); <span style="color:#0000ff;">if </span>(Write) <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;TestReflectionInstanceOptimized &#8221; </span>+ sw.ElapsedTicks.ToString()); times[<span style="color:#a31515;">"TestReflectionInstanceOptimized"</span>] += sw.ElapsedTicks; }</div>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p align="justify">My belief is that if you optimize well you code when using reflection, then the performance penalty is not so great but the advantages are many times more significant. I believe this must have been taken into account, when Microsoft developed WPF, WWF and LINQ.</p>
<p align="justify"><a href="http://www.codeproject.com/KB/dotnet/ReflectionOptimize.aspx">CodeProject Article</a></p>
<p align="justify"><strong>The next day while I was out in the sun some thoughts came to me that made me modify the makegeneric comparicon</strong></p>
<p>The Generic Benchmarking is not fair. The practical dilemma is that at any single point of execution is it better to call MakeGenericType for a generic type or to find the type form a variety of pre made types from a dictionary.</p>
<p>So I ran the test using the below codes</p>
<blockquote><p><span style="color:#2b91af;">Type </span>ii = type.MakeGenericType(<span style="color:#0000ff;">new </span><span style="color:#2b91af;">Type</span>[] { <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">int</span>), <span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">int</span>) });</p></blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>and the optimized version</p>
<blockquote><p><span style="color:#2b91af;">Type </span>ii = makedTypes[type.FullName + <span style="color:#a31515;">"|int|int"</span>];</p></blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>and the results where which</p>
<blockquote><p>GenericReflectionMake-<span class="code-keyword">&gt;</span><span class="code-digit">26</span> GenericReflectionMakeOptimized-<span class="code-keyword">&gt;</span><span class="code-digit">9</span></p></blockquote>
<p align="justify">This shows that having pre made generic types in a sort of list is far much quicker than calling MakeGenericType</p>
<p align="justify">
<p align="justify"> </p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sarafianalex.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sarafianalex.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sarafianalex.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sarafianalex.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sarafianalex.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sarafianalex.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sarafianalex.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sarafianalex.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sarafianalex.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sarafianalex.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sarafianalex.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sarafianalex.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sarafianalex.wordpress.com&blog=2367780&post=14&subd=sarafianalex&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sarafianalex.wordpress.com/2008/04/19/reflection-benchmark/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64f00567ef7dda3ec36dcf0e01043d11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sarafian</media:title>
		</media:content>
	</item>
	</channel>
</rss>