<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Samurai Programmer.com - Performance</title>
    <link>http://www.samuraiprogrammer.com/blog/</link>
    <description>I know kung fu</description>
    <language>en-us</language>
    <copyright>Greg Varveris</copyright>
    <lastBuildDate>Sat, 19 Nov 2011 13:45:14 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>greg@samuraiprogrammer.com</managingEditor>
    <webMaster>greg@samuraiprogrammer.com</webMaster>
    <item>
      <trackback:ping>http://www.samuraiprogrammer.com/blog/Trackback.aspx?guid=640974ce-fd96-4cad-bcd0-6c45ed248018</trackback:ping>
      <pingback:server>http://www.samuraiprogrammer.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.samuraiprogrammer.com/blog/PermaLink,guid,640974ce-fd96-4cad-bcd0-6c45ed248018.aspx</pingback:target>
      <dc:creator>Greg Varveris</dc:creator>
      <wfw:comment>http://www.samuraiprogrammer.com/blog/CommentView,guid,640974ce-fd96-4cad-bcd0-6c45ed248018.aspx</wfw:comment>
      <wfw:commentRss>http://www.samuraiprogrammer.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=640974ce-fd96-4cad-bcd0-6c45ed248018</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So in a code-base I was working in yesterday, we use PInvoke to call out to the Performance
Data Helper (PDH) API’s to collect performance information for machines without using
Perfmon.  One of those PInvoke calls looked like this:
</p>
        <pre class="c#" name="code">/*<br />
PDH_STATUS PdhExpandCounterPath(<br />
LPCTSTR szWildCardPath,<br />
LPTSTR mszExpandedPathList,<br />
LPDWORD pcchPathListLength<br />
);<br />
*/<br />
[DllImport("pdh.dll", CharSet = CharSet.Unicode)]<br />
private static extern PdhStatus PdhExpandCounterPath(<br />
string szWildCardPath,<br />
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] char[] mszExpandedPathList,<br />
ref uint pcchPathListLength<br />
);</pre>
        <p>
In .NET 3.5 and below, this PInvoke call works perfectly fine.  In .NET 4.0,
though, I saw this exception:
</p>
        <pre>System.Runtime.InteropServices.MarshalDirectiveException: 
<br />
Cannot marshal 'parameter #2': Array size control parameter index is out of range. 
<br />
at System.Runtime.InteropServices.Marshal.InternalPrelink(IRuntimeMethodInfo m) 
<br />
at System.Runtime.InteropServices.Marshal.Prelink(MethodInfo m)</pre>
        <p>
So, can you identify what’s wrong in the code above?
</p>
        <p>
Well, the Array size control parameter index indicates the zero-based parameter that
contains the count of the array elements, similar to size_is in COM.  Because
the marshaler cannot determine the size of an unmanaged array, you have to pass it
in as a separate parameter.  So in the call above, parameter #2, we specify “SizeParamIndex
= 3” to reference the pcchPathListLength parameter to set the length of the array. 
So what’s the catch?
</p>
        <p>
Well, since the SizeParamIndex is a zero-based index, the 3rd parameter doesn’t really
exist.  So, to fix this, we just change the “SizeParamIndex=3” to “SizeParamIndex=2”
to reference the pcchPathListLength:
</p>
        <pre class="c#" name="code">/*<br />
PDH_STATUS PdhExpandCounterPath(<br />
LPCTSTR szWildCardPath,<br />
LPTSTR mszExpandedPathList,<br />
LPDWORD pcchPathListLength<br />
);<br />
*/<br />
[DllImport("pdh.dll", CharSet = CharSet.Unicode)]<br />
private static extern PdhStatus PdhExpandCounterPath(<br />
string szWildCardPath,<br />
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] char[] mszExpandedPathList,<br />
ref uint pcchPathListLength<br />
);</pre>
        <p>
It looks like in .NET 3.5 and below, though, we allowed you to reference either 1-based
index or a zero-based index but in .NET 4.0, we buttoned that up a bit and force you
to use the zero-based index.    Big thanks to my co-worker and frequent
collaborator, <a href="http://blogs.msdn.com/b/pfedev/" target="_blank">Zach Kramer</a> for
his assistance in looking at this issue.
</p>
        <p>
Until Next Time!
</p>
      </body>
      <title>PInvoke Error in .NET 4: Array size control parameter index is out of range</title>
      <guid isPermaLink="false">http://www.samuraiprogrammer.com/blog/PermaLink,guid,640974ce-fd96-4cad-bcd0-6c45ed248018.aspx</guid>
      <link>http://www.samuraiprogrammer.com/blog/2011/11/19/PInvokeErrorInNET4ArraySizeControlParameterIndexIsOutOfRange.aspx</link>
      <pubDate>Sat, 19 Nov 2011 13:45:14 GMT</pubDate>
      <description>&lt;p&gt;
So in a code-base I was working in yesterday, we use PInvoke to call out to the Performance
Data Helper (PDH) API’s to collect performance information for machines without using
Perfmon.&amp;#160; One of those PInvoke calls looked like this:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;/*&lt;br /&gt;
PDH_STATUS PdhExpandCounterPath(&lt;br /&gt;
LPCTSTR szWildCardPath,&lt;br /&gt;
LPTSTR mszExpandedPathList,&lt;br /&gt;
LPDWORD pcchPathListLength&lt;br /&gt;
);&lt;br /&gt;
*/&lt;br /&gt;
[DllImport(&amp;quot;pdh.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
private static extern PdhStatus PdhExpandCounterPath(&lt;br /&gt;
string szWildCardPath,&lt;br /&gt;
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] char[] mszExpandedPathList,&lt;br /&gt;
ref uint pcchPathListLength&lt;br /&gt;
);&lt;/pre&gt;
&lt;p&gt;
In .NET 3.5 and below, this PInvoke call works perfectly fine.&amp;#160; In .NET 4.0,
though, I saw this exception:
&lt;/p&gt;
&lt;pre&gt;System.Runtime.InteropServices.MarshalDirectiveException: 
&lt;br /&gt;
Cannot marshal 'parameter #2': Array size control parameter index is out of range. 
&lt;br /&gt;
at System.Runtime.InteropServices.Marshal.InternalPrelink(IRuntimeMethodInfo m) 
&lt;br /&gt;
at System.Runtime.InteropServices.Marshal.Prelink(MethodInfo m)&lt;/pre&gt;
&lt;p&gt;
So, can you identify what’s wrong in the code above?
&lt;/p&gt;
&lt;p&gt;
Well, the Array size control parameter index indicates the zero-based parameter that
contains the count of the array elements, similar to size_is in COM.&amp;#160; Because
the marshaler cannot determine the size of an unmanaged array, you have to pass it
in as a separate parameter.&amp;#160; So in the call above, parameter #2, we specify “SizeParamIndex
= 3” to reference the pcchPathListLength parameter to set the length of the array.&amp;#160;
So what’s the catch?
&lt;/p&gt;
&lt;p&gt;
Well, since the SizeParamIndex is a zero-based index, the 3rd parameter doesn’t really
exist.&amp;#160; So, to fix this, we just change the “SizeParamIndex=3” to “SizeParamIndex=2”
to reference the pcchPathListLength:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;/*&lt;br /&gt;
PDH_STATUS PdhExpandCounterPath(&lt;br /&gt;
LPCTSTR szWildCardPath,&lt;br /&gt;
LPTSTR mszExpandedPathList,&lt;br /&gt;
LPDWORD pcchPathListLength&lt;br /&gt;
);&lt;br /&gt;
*/&lt;br /&gt;
[DllImport(&amp;quot;pdh.dll&amp;quot;, CharSet = CharSet.Unicode)]&lt;br /&gt;
private static extern PdhStatus PdhExpandCounterPath(&lt;br /&gt;
string szWildCardPath,&lt;br /&gt;
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] char[] mszExpandedPathList,&lt;br /&gt;
ref uint pcchPathListLength&lt;br /&gt;
);&lt;/pre&gt;
&lt;p&gt;
It looks like in .NET 3.5 and below, though, we allowed you to reference either 1-based
index or a zero-based index but in .NET 4.0, we buttoned that up a bit and force you
to use the zero-based index.&amp;#160;&amp;#160;&amp;#160; Big thanks to my co-worker and frequent
collaborator, &lt;a href="http://blogs.msdn.com/b/pfedev/" target="_blank"&gt;Zach Kramer&lt;/a&gt; for
his assistance in looking at this issue.
&lt;/p&gt;
&lt;p&gt;
Until Next Time!
&lt;/p&gt;</description>
      <comments>http://www.samuraiprogrammer.com/blog/CommentView,guid,640974ce-fd96-4cad-bcd0-6c45ed248018.aspx</comments>
      <category>.NET 4</category>
      <category>Perfmon</category>
      <category>Performance</category>
    </item>
    <item>
      <trackback:ping>http://www.samuraiprogrammer.com/blog/Trackback.aspx?guid=90aee052-b425-4e42-82ee-2b736c506e39</trackback:ping>
      <pingback:server>http://www.samuraiprogrammer.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.samuraiprogrammer.com/blog/PermaLink,guid,90aee052-b425-4e42-82ee-2b736c506e39.aspx</pingback:target>
      <dc:creator>Greg Varveris</dc:creator>
      <wfw:comment>http://www.samuraiprogrammer.com/blog/CommentView,guid,90aee052-b425-4e42-82ee-2b736c506e39.aspx</wfw:comment>
      <wfw:commentRss>http://www.samuraiprogrammer.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=90aee052-b425-4e42-82ee-2b736c506e39</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.hanselman.com/blog/BackToParallelBasicsDontBlockYourThreadsMakeAsyncIOWorkForYou.aspx">Read
a great blog entry by Scott Hanselman</a> recently talking about the parallel dilemma
that I’m sure we’ll see folks face in the future with the (old/new) Parallel classes. 
I wanted to add a few things to this discussion as he focused on the mechanics of
the parallel requests but maybe not the potential effects it could have on the macro
view of your application.  This was originally written as an e-mail I sent to
my team but thought others might find it interesting.
</p>
        <p>
There will be an inclination by people to use the new Parallel functionality in .NET
4.0 to easily spawn operations onto numerous background threads.  That will generally
be okay for console/winform/wpf apps – but could also be potentially bad for ASP.NET
apps as the spawned threads could take away from the processing power and threads
available to process new webpage requests.  I’ll explain more on that later.  
</p>
        <p>
For example, by default, when you do something like Parallel.ForEach(…) or some such,
the parallel library starts firing Tasks to the thread pool so that it can best utilize
the processing power available on your machine (oversimplification but you get the
idea).  The downside is that the thread pool contains a finite number of worker
threads threads available to a process.  Granted, you have about 100 threads
per logical processor in .NET 4 – but it’s worth noting.
</p>
        <p>
While Scott’s entry talks about the new way to implement the Async pattern, I’ve already
seen a bunch of folks use the “Parallel” class because it abstracts away some of the
plumbing of the Async operations and that ease of use could become problematic.  
</p>
        <p>
For example, consider this code:
</p>
        <pre class="c#" name="code">string[] myStrings = { "hello", "world", "you", "crazy", "pfes", "out", "there" };<br /><br />
Parallel.ForEach(myStrings, myString =&gt;<br />
{<br /><br />
System.Console.WriteLine(DateTime.Now + ":" + myString + 
<br />
" - From Thread #" + 
<br />
Thread.CurrentThread.ManagedThreadId);<br />
Thread.Sleep(new Random().Next(1000, 5000));<br /><br />
});<br /></pre>
        <p>
This is a very simple implementation of Parallel’izing a foreach that just writes
some string output with an artificial delay.  Output would be something like:
</p>
        <blockquote>
          <p>
11/16/2010 2:40:05 AM:hello - From Thread #10 
<br />
11/16/2010 2:40:05 AM:crazy - From Thread #11 
<br />
11/16/2010 2:40:05 AM:there - From Thread #12 
<br />
11/16/2010 2:40:06 AM:world - From Thread #13 
<br />
11/16/2010 2:40:06 AM:pfes - From Thread #14 
<br />
11/16/2010 2:40:06 AM:you - From Thread #12 
<br />
11/16/2010 2:40:07 AM:out - From Thread #11  
</p>
        </blockquote>
        <p>
Note the multiple thread ids and extrapolate that out to a server that has more than
just my paltry 2 CPUs.  This can be potentially problematic for ASP.NET applications
as you have a finite number of worker threads available in your worker process and
they must be shared across not just one user but hundreds (or even thousands). 
So, we might see that spawning an operation across tons of threads can potentially
reduce the scalability of your site.
</p>
        <p>
Fortunately, there is a ParallelOptions class where you can set the degree of parallel’ism. 
Updated code as follows:
</p>
        <pre class="c#" name="code">string[] myStrings = { "hello", "world", "you", "crazy", "pfes", "out", "there" };<br /><br />
ParallelOptions options = new ParallelOptions();<br />
options.MaxDegreeOfParallelism = 1;<br /><br />
Parallel.ForEach(myStrings,options, myString =&gt;<br />
{<br /><br />
// Nothing changes here<br />
...<br /><br />
});<br /></pre>
        <p>
This would then output something like:
</p>
        <blockquote>
          <p>
11/16/2010 2:40:11 AM:hello - From Thread #10 
<br />
11/16/2010 2:40:12 AM:world - From Thread #10 
<br />
11/16/2010 2:40:16 AM:you - From Thread #10 
<br />
11/16/2010 2:40:20 AM:crazy - From Thread #10 
<br />
11/16/2010 2:40:23 AM:pfes - From Thread #10 
<br />
11/16/2010 2:40:26 AM:out - From Thread #10 
<br />
11/16/2010 2:40:29 AM:there - From Thread #10
</p>
        </blockquote>
        <p>
Since I set the MaxDegreeOfParallelism to “1”, we see that it just uses the same thread
over and over.  Within reason, that setting *should* correspond to the number
of threads it will use to handle the request.  
</p>
        <h2>Applying to a website
</h2>
        <p>
So, let’s apply the code from the above to a simple website and compare the difference
between the full parallel implementation and the non-parallel implementation. 
The test I used ran for 10 minutes with a consistent load of 20 users on a dual-core
machine running IIS 7. <strong><em></em></strong></p>
        <p>
          <strong>
            <em>In all of the images below, the blue line (or baseline) represents the
single-threaded implementation and the purple line (or compared) represents the parallel
implementation</em>
          </strong>.  
</p>
        <p>
We’ll start with the <strong>request execution time</strong>.  As we’d expect,
the time to complete the request decreases significantly with the parallel implementation.
</p>
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime_2.gif">
            <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime" border="0" alt="__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime_thumb.gif" width="484" height="364" />
          </a>
        </p>
        <p>
But what is the cost from a thread perspective?  For that, we’ll look at the <strong>number
of physical threads</strong>: 
</p>
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads_2.gif">
            <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads" border="0" alt="__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads_thumb.gif" width="484" height="364" />
          </a>
        </p>
        <p>
As we’d also expect, there is a significant increase in the number of threads used
in the process.  We go from ~20 threads in the process to a peak of almost 200
threads throughout the test.  Seeing as this was run on a dual-core machine,
we’ll have a maximum of 200 worker threads available in the thread pool.  After
those threads become depleted, you often see requests start getting queued, waiting
for a thread to become available.  So, what happened in our simple test? 
We’re look at the <strong>requests queued</strong> value for that:
</p>
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued_2.gif">
            <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued" border="0" alt="__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued_thumb.gif" width="484" height="364" />
          </a>  
</p>
        <p>
We did, in-fact, start to see a small number of requests become queued throughout
our test.  This indicates that some requests started to pile up waiting for an
available thread to become available.  
</p>
        <p>
Please note that I’m <strong>NOT</strong> saying that you should not use Parallel
operations in your website.  <strong>You saw in the first image that the actual
request execution time decreased significantly from the non-parallel implementation
to the parallel implementation.</strong> But it’s important to note that nothing is
free and while parallelizing your work can and will improve the performance of a single
request, it should also be weighed against the potential performance of your site
overall.
</p>
        <p>
Until next time.
</p>
      </body>
      <title>The (potentially) dark side of parallelism…</title>
      <guid isPermaLink="false">http://www.samuraiprogrammer.com/blog/PermaLink,guid,90aee052-b425-4e42-82ee-2b736c506e39.aspx</guid>
      <link>http://www.samuraiprogrammer.com/blog/2010/12/05/ThePotentiallyDarkSideOfParallelism.aspx</link>
      <pubDate>Sun, 05 Dec 2010 18:52:17 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.hanselman.com/blog/BackToParallelBasicsDontBlockYourThreadsMakeAsyncIOWorkForYou.aspx"&gt;Read
a great blog entry by Scott Hanselman&lt;/a&gt; recently talking about the parallel dilemma
that I’m sure we’ll see folks face in the future with the (old/new) Parallel classes.&amp;#160;
I wanted to add a few things to this discussion as he focused on the mechanics of
the parallel requests but maybe not the potential effects it could have on the macro
view of your application.&amp;#160; This was originally written as an e-mail I sent to
my team but thought others might find it interesting.
&lt;/p&gt;
&lt;p&gt;
There will be an inclination by people to use the new Parallel functionality in .NET
4.0 to easily spawn operations onto numerous background threads.&amp;#160; That will generally
be okay for console/winform/wpf apps – but could also be potentially bad for ASP.NET
apps as the spawned threads could take away from the processing power and threads
available to process new webpage requests.&amp;#160; I’ll explain more on that later.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
For example, by default, when you do something like Parallel.ForEach(…) or some such,
the parallel library starts firing Tasks to the thread pool so that it can best utilize
the processing power available on your machine (oversimplification but you get the
idea).&amp;#160; The downside is that the thread pool contains a finite number of worker
threads threads available to a process.&amp;#160; Granted, you have about 100 threads
per logical processor in .NET 4 – but it’s worth noting.
&lt;/p&gt;
&lt;p&gt;
While Scott’s entry talks about the new way to implement the Async pattern, I’ve already
seen a bunch of folks use the “Parallel” class because it abstracts away some of the
plumbing of the Async operations and that ease of use could become problematic.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
For example, consider this code:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;string[] myStrings = { &amp;quot;hello&amp;quot;, &amp;quot;world&amp;quot;, &amp;quot;you&amp;quot;, &amp;quot;crazy&amp;quot;, &amp;quot;pfes&amp;quot;, &amp;quot;out&amp;quot;, &amp;quot;there&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
Parallel.ForEach(myStrings, myString =&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
System.Console.WriteLine(DateTime.Now + &amp;quot;:&amp;quot; + myString + 
&lt;br /&gt;
&amp;quot; - From Thread #&amp;quot; + 
&lt;br /&gt;
Thread.CurrentThread.ManagedThreadId);&lt;br /&gt;
Thread.Sleep(new Random().Next(1000, 5000));&lt;br /&gt;
&lt;br /&gt;
});&lt;br /&gt;
&lt;/pre&gt;
&lt;p&gt;
This is a very simple implementation of Parallel’izing a foreach that just writes
some string output with an artificial delay.&amp;#160; Output would be something like:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
11/16/2010 2:40:05 AM:hello - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:05 AM:crazy - From Thread #11 
&lt;br /&gt;
11/16/2010 2:40:05 AM:there - From Thread #12 
&lt;br /&gt;
11/16/2010 2:40:06 AM:world - From Thread #13 
&lt;br /&gt;
11/16/2010 2:40:06 AM:pfes - From Thread #14 
&lt;br /&gt;
11/16/2010 2:40:06 AM:you - From Thread #12 
&lt;br /&gt;
11/16/2010 2:40:07 AM:out - From Thread #11&amp;#160; 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Note the multiple thread ids and extrapolate that out to a server that has more than
just my paltry 2 CPUs.&amp;#160; This can be potentially problematic for ASP.NET applications
as you have a finite number of worker threads available in your worker process and
they must be shared across not just one user but hundreds (or even thousands).&amp;#160;
So, we might see that spawning an operation across tons of threads can potentially
reduce the scalability of your site.
&lt;/p&gt;
&lt;p&gt;
Fortunately, there is a ParallelOptions class where you can set the degree of parallel’ism.&amp;#160;
Updated code as follows:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;string[] myStrings = { &amp;quot;hello&amp;quot;, &amp;quot;world&amp;quot;, &amp;quot;you&amp;quot;, &amp;quot;crazy&amp;quot;, &amp;quot;pfes&amp;quot;, &amp;quot;out&amp;quot;, &amp;quot;there&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
ParallelOptions options = new ParallelOptions();&lt;br /&gt;
options.MaxDegreeOfParallelism = 1;&lt;br /&gt;
&lt;br /&gt;
Parallel.ForEach(myStrings,options, myString =&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
// Nothing changes here&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
});&lt;br /&gt;
&lt;/pre&gt;
&lt;p&gt;
This would then output something like:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
11/16/2010 2:40:11 AM:hello - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:12 AM:world - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:16 AM:you - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:20 AM:crazy - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:23 AM:pfes - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:26 AM:out - From Thread #10 
&lt;br /&gt;
11/16/2010 2:40:29 AM:there - From Thread #10
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Since I set the MaxDegreeOfParallelism to “1”, we see that it just uses the same thread
over and over.&amp;#160; Within reason, that setting *should* correspond to the number
of threads it will use to handle the request.&amp;#160; 
&lt;/p&gt;
&lt;h2&gt;Applying to a website
&lt;/h2&gt;
&lt;p&gt;
So, let’s apply the code from the above to a simple website and compare the difference
between the full parallel implementation and the non-parallel implementation.&amp;#160;
The test I used ran for 10 minutes with a consistent load of 20 users on a dual-core
machine running IIS 7. &lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;em&gt;In all of the images below, the blue line (or baseline) represents the
single-threaded implementation and the purple line (or compared) represents the parallel
implementation&lt;/em&gt;&lt;/strong&gt;.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
We’ll start with the &lt;strong&gt;request execution time&lt;/strong&gt;.&amp;#160; As we’d expect,
the time to complete the request decreases significantly with the parallel implementation.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime_2.gif"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime" border="0" alt="__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestExecutionTime_thumb.gif" width="484" height="364" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
But what is the cost from a thread perspective?&amp;#160; For that, we’ll look at the &lt;strong&gt;number
of physical threads&lt;/strong&gt;: 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads_2.gif"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads" border="0" alt="__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC__NETCLRLocksAndThreads_w3wp__NumofcurrentphysicalThreads_thumb.gif" width="484" height="364" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
As we’d also expect, there is a significant increase in the number of threads used
in the process.&amp;#160; We go from ~20 threads in the process to a peak of almost 200
threads throughout the test.&amp;#160; Seeing as this was run on a dual-core machine,
we’ll have a maximum of 200 worker threads available in the thread pool.&amp;#160; After
those threads become depleted, you often see requests start getting queued, waiting
for a thread to become available.&amp;#160; So, what happened in our simple test?&amp;#160;
We’re look at the &lt;strong&gt;requests queued&lt;/strong&gt; value for that:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued_2.gif"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued" border="0" alt="__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/df4a939434b1_CB17/__GREGVAR-PC_ASP_NETv4_0_30319_RequestsQueued_thumb.gif" width="484" height="364" /&gt;&lt;/a&gt;&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
We did, in-fact, start to see a small number of requests become queued throughout
our test.&amp;#160; This indicates that some requests started to pile up waiting for an
available thread to become available.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
Please note that I’m &lt;strong&gt;NOT&lt;/strong&gt; saying that you should not use Parallel
operations in your website.&amp;#160; &lt;strong&gt;You saw in the first image that the actual
request execution time decreased significantly from the non-parallel implementation
to the parallel implementation.&lt;/strong&gt; But it’s important to note that nothing is
free and while parallelizing your work can and will improve the performance of a single
request, it should also be weighed against the potential performance of your site
overall.
&lt;/p&gt;
&lt;p&gt;
Until next time.
&lt;/p&gt;</description>
      <comments>http://www.samuraiprogrammer.com/blog/CommentView,guid,90aee052-b425-4e42-82ee-2b736c506e39.aspx</comments>
      <category>.NET 4</category>
      <category>ASP.NET</category>
      <category>Parallel</category>
      <category>Performance</category>
    </item>
    <item>
      <trackback:ping>http://www.samuraiprogrammer.com/blog/Trackback.aspx?guid=79911823-e816-4166-8552-884b2ea29759</trackback:ping>
      <pingback:server>http://www.samuraiprogrammer.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.samuraiprogrammer.com/blog/PermaLink,guid,79911823-e816-4166-8552-884b2ea29759.aspx</pingback:target>
      <dc:creator>Greg Varveris</dc:creator>
      <wfw:comment>http://www.samuraiprogrammer.com/blog/CommentView,guid,79911823-e816-4166-8552-884b2ea29759.aspx</wfw:comment>
      <wfw:commentRss>http://www.samuraiprogrammer.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=79911823-e816-4166-8552-884b2ea29759</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
            <strong>
              <em>“We shall neither fail nor falter; we shall not weaken or tire…give us
the tools and we will finish the job.” – Winston Churchill</em>
            </strong>
          </p>
        </blockquote>
        <p>
I don’t often blog about specific language features but over the past few weeks I’ve
spoken to a few folks that did not know of the <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx">“yield”
keyword and the “yield return” and “yield break” statements</a>, so I thought it might
be a good opportunity to shed some light on this little known but extremely useful
C# feature.  Chances are, you’ve probably indirectly used this feature before
and just never known it.
</p>
        <p>
We’ll start with the problem I’ve seen that plagues many applications.  Often
times, you’ll call a method that returns a List&lt;T&gt; or some other concrete collection. 
The method probably looks something like this:
</p>
        <pre class="c#" name="code">public static List&lt;string&gt; GenerateMyList()<br />
{<br />
List&lt;string&gt; myList = new List&lt;string&gt;();<br /><br />
for (int i = 0; i &lt; 100; i++)<br />
{<br /><br />
myList.Add(i.ToString()); 
<br /><br />
}<br /><br />
return myList;<br /><br />
}</pre>
        <p>
I’m sure your logic is going to be significantly more complex than what I have above
but you get the idea.  There are a few problems and inefficiencies with this
method.  Can you spot them?
</p>
        <ul>
          <li>
The entire List&lt;T&gt; must be stored in memory. 
</li>
          <li>
Its caller must wait for the List&lt;T&gt; to be returned before it can process anything. 
</li>
          <li>
The method itself returns a List&lt;T&gt; back to its caller. 
</li>
        </ul>
        <p>
          <em>As an aside – with public methods, you should strive to not return a List&lt;T&gt;
in your methods.  </em>
          <a href="http://msdn.microsoft.com/en-us/library/ms182142.aspx">
            <em>Full
details can be found here</em>
          </a>
          <em>.  The main idea here is that if you choose
to change the method signature and return a different collection type in the future,
this would be considered a breaking change to your callers.</em>
        </p>
        <p>
In any case, I’ll focus on the first two items in the list above.  If the List&lt;T&gt;
that is returned from the GenerateMyList() method is large then that will be a lot
of data that must be kept around in memory.  In addition, if it takes a long
time to generate the list, your caller is stuck until you’ve completely finished your
processing.
</p>
        <p>
Instead, you can use that nifty “<strong>yield</strong>” keyword.  This allows
the GenerateMyList() method to return items to its caller as they are being processed. 
This means that you no longer need to keep the entire list in memory and can just
return one item at a time until you get to the end of your returned items.  To
illustrate my point, I’ll refactor the above method into the following:
</p>
        <pre class="c#" name="code">private static IEnumerable&lt;string&gt; GenerateMyList()<br />
{<br />
for (int i = 0; i &lt; 100; i++)<br />
{<br />
string value = i.ToString();<br />
Console.WriteLine("Returning {0} to caller.", value);<br />
yield return value;<br />
}<br /><br />
Console.WriteLine("Method done!");<br /><br />
yield break;<br />
}</pre>
        <p>
A few things to note in this method.  The return type has been changed to an
IEnumerable&lt;string&gt;.  This is one of those nifty interfaces that exposes
an enumerator.  This allows its caller to cycle through the results in a foreach
or while loop.  In addition, the “yield return i.ToString()” will return that
item to its caller at that point and not when the entire method has completed its
processing.  This allows for a very exciting caller-callee type relationship. 
For example, if I call this method like so:
</p>
        <pre class="c#" name="code">IEnumerable&lt;string&gt; myList = GenerateMyList();<br /><br />
foreach (string listItem in myList)<br />
{<br />
Console.WriteLine("Item: " + listItem);<br />
}</pre>
        <p>
The output would be:
</p>
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/abde526a5631_13833/image_4.png">
            <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/abde526a5631_13833/image_thumb_1.png" width="257" height="339" />
          </a>
        </p>
        <p>
Thus showing that each item gets returned at the time we processed it.  So, how
does this work?  Well, at compile time, we will generate a class to implement
the behavior in the iterator.  Essentially, this means that the GenerateMyList()
method body gets placed into the MoveNext() method.  In-fact, if you open up
the compiled assembly in <a href="http://www.red-gate.com/products/reflector/">Reflector</a>,
you see that plumbing in place (comments are mine and some code was omitted for clarity’s
sake):
</p>
        <pre class="c#" name="code">private bool MoveNext()<br />
{<br /><br />
this.&lt;&gt;1__state = -1;<br />
this.&lt;i&gt;5__1 = 0;<br />
// My for loop has changed to a while loop.<br />
while (this.&lt;i&gt;5__1 &lt; 100)<br />
{<br />
// Sets a local value<br />
this.&lt;value&gt;5__2 = this.&lt;i&gt;5__1.ToString();<br />
// Here is my Console.WriteLine(...)<br />
Console.WriteLine("Returning {0} to caller.", this.&lt;value&gt;5__2);<br />
// Here is where the current member variable<br />
// gets stored.<br />
this.&lt;&gt;2__current = this.&lt;value&gt;5__2;<br />
this.&lt;&gt;1__state = 1;<br />
// We return "true" to the caller so it knows<br />
// there is another record to be processed.<br />
return true;<br />
...<br />
}<br />
// Here is my Console.WriteLine() at the bottom<br />
// when we've finished processing the loop.<br />
Console.WriteLine("Method done!");<br />
break;<br /><br /><br />
}</pre>
        <p>
Pretty straightforward.  Of course, the real power is that it the compiler converts
the “yield return &lt;blah&gt;” into a nice clean enumerator with a MoveNext(). 
In-fact, if you’ve used LINQ, you’ve probably used this feature without even knowing
it.  Consider the following code:
</p>
        <pre class="c#" name="code">private static void OutputLinqToXmlQuery()<br />
{<br />
XDocument doc = XDocument.Parse<br />
(@"&lt;root&gt;&lt;data&gt;hello world&lt;/data&gt;&lt;data&gt;goodbye world&lt;/data&gt;&lt;/root&gt;");<br /><br />
var results = from data in doc.Descendants("data")<br />
select data;<br /><br />
foreach (var result in results)<br />
{<br />
Console.WriteLine(result); 
<br />
}<br />
}</pre>
        <p>
The “results” object, by default will be of type “WhereSelectEnumerableIterator” which
exposes a MoveNext() method.  In-fact, that is also why the results object doesn’t
allow you to do something like this:
</p>
        <pre class="c#" name="code">var results = from data in doc.Descendants("data")<br />
select data;<br /><br />
var bad = results[1];</pre>
        <p>
The IEnumerator does not expose an indexer allowing you to go straight to a particular
element in the collection because the full collection hasn’t been generated yet. 
Instead, you would do something like this:
</p>
        <pre class="c#" name="code">var results = from data in doc.Descendants("data")<br />
select data; 
<br /><br />
var good = results.ElementAt(1);</pre>
        <p>
And then under the covers, the ElementAt(int) method will just keep calling MoveNext()
until it reaches the index you specified.  Something like this: 
</p>
        <blockquote>
          <p>
            <strong>
              <em>Note:  this is my own code and is NOT from the .NET Framework – it
is merely meant to illustrate a point.</em>
            </strong>
          </p>
        </blockquote>
        <pre class="c#" name="code">public static XElement MyElementAt(this IEnumerable&lt;XElement&gt; elements, 
<br />
int index)<br />
{<br />
int counter = 0;<br />
using (IEnumerator&lt;XElement&gt; enumerator = 
<br />
elements.GetEnumerator())<br />
{ 
<br />
while(enumerator.MoveNext()){<br />
if (counter == index)<br />
return enumerator.Current;<br />
counter++;<br />
}<br />
}<br /><br />
return null;<br />
}</pre>
        <p>
Hope this helps to demystify some things and put another tool in your toolbox.
</p>
        <p>
Until next time.
</p>
      </body>
      <title>Yield Return…a little known but incredible language feature</title>
      <guid isPermaLink="false">http://www.samuraiprogrammer.com/blog/PermaLink,guid,79911823-e816-4166-8552-884b2ea29759.aspx</guid>
      <link>http://www.samuraiprogrammer.com/blog/2010/10/17/YieldReturnaLittleKnownButIncredibleLanguageFeature.aspx</link>
      <pubDate>Sun, 17 Oct 2010 22:54:49 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;&lt;em&gt;“We shall neither fail nor falter; we shall not weaken or tire…give us
the tools and we will finish the job.” – Winston Churchill&lt;/em&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I don’t often blog about specific language features but over the past few weeks I’ve
spoken to a few folks that did not know of the &lt;a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx"&gt;“yield”
keyword and the “yield return” and “yield break” statements&lt;/a&gt;, so I thought it might
be a good opportunity to shed some light on this little known but extremely useful
C# feature.&amp;#160; Chances are, you’ve probably indirectly used this feature before
and just never known it.
&lt;/p&gt;
&lt;p&gt;
We’ll start with the problem I’ve seen that plagues many applications.&amp;#160; Often
times, you’ll call a method that returns a List&amp;lt;T&amp;gt; or some other concrete collection.&amp;#160;
The method probably looks something like this:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;public static List&amp;lt;string&amp;gt; GenerateMyList()&lt;br /&gt;
{&lt;br /&gt;
List&amp;lt;string&amp;gt; myList = new List&amp;lt;string&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
for (int i = 0; i &amp;lt; 100; i++)&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
myList.Add(i.ToString()); 
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return myList;&lt;br /&gt;
&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
I’m sure your logic is going to be significantly more complex than what I have above
but you get the idea.&amp;#160; There are a few problems and inefficiencies with this
method.&amp;#160; Can you spot them?
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The entire List&amp;lt;T&amp;gt; must be stored in memory. 
&lt;/li&gt;
&lt;li&gt;
Its caller must wait for the List&amp;lt;T&amp;gt; to be returned before it can process anything. 
&lt;/li&gt;
&lt;li&gt;
The method itself returns a List&amp;lt;T&amp;gt; back to its caller. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;em&gt;As an aside – with public methods, you should strive to not return a List&amp;lt;T&amp;gt;
in your methods.&amp;#160; &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms182142.aspx"&gt;&lt;em&gt;Full
details can be found here&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&amp;#160; The main idea here is that if you choose
to change the method signature and return a different collection type in the future,
this would be considered a breaking change to your callers.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
In any case, I’ll focus on the first two items in the list above.&amp;#160; If the List&amp;lt;T&amp;gt;
that is returned from the GenerateMyList() method is large then that will be a lot
of data that must be kept around in memory.&amp;#160; In addition, if it takes a long
time to generate the list, your caller is stuck until you’ve completely finished your
processing.
&lt;/p&gt;
&lt;p&gt;
Instead, you can use that nifty “&lt;strong&gt;yield&lt;/strong&gt;” keyword.&amp;#160; This allows
the GenerateMyList() method to return items to its caller as they are being processed.&amp;#160;
This means that you no longer need to keep the entire list in memory and can just
return one item at a time until you get to the end of your returned items.&amp;#160; To
illustrate my point, I’ll refactor the above method into the following:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;private static IEnumerable&amp;lt;string&amp;gt; GenerateMyList()&lt;br /&gt;
{&lt;br /&gt;
for (int i = 0; i &amp;lt; 100; i++)&lt;br /&gt;
{&lt;br /&gt;
string value = i.ToString();&lt;br /&gt;
Console.WriteLine(&amp;quot;Returning {0} to caller.&amp;quot;, value);&lt;br /&gt;
yield return value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Console.WriteLine(&amp;quot;Method done!&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
yield break;&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
A few things to note in this method.&amp;#160; The return type has been changed to an
IEnumerable&amp;lt;string&amp;gt;.&amp;#160; This is one of those nifty interfaces that exposes
an enumerator.&amp;#160; This allows its caller to cycle through the results in a foreach
or while loop.&amp;#160; In addition, the “yield return i.ToString()” will return that
item to its caller at that point and not when the entire method has completed its
processing.&amp;#160; This allows for a very exciting caller-callee type relationship.&amp;#160;
For example, if I call this method like so:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;IEnumerable&amp;lt;string&amp;gt; myList = GenerateMyList();&lt;br /&gt;
&lt;br /&gt;
foreach (string listItem in myList)&lt;br /&gt;
{&lt;br /&gt;
Console.WriteLine(&amp;quot;Item: &amp;quot; + listItem);&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
The output would be:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/abde526a5631_13833/image_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/abde526a5631_13833/image_thumb_1.png" width="257" height="339" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Thus showing that each item gets returned at the time we processed it.&amp;#160; So, how
does this work?&amp;#160; Well, at compile time, we will generate a class to implement
the behavior in the iterator.&amp;#160; Essentially, this means that the GenerateMyList()
method body gets placed into the MoveNext() method.&amp;#160; In-fact, if you open up
the compiled assembly in &lt;a href="http://www.red-gate.com/products/reflector/"&gt;Reflector&lt;/a&gt;,
you see that plumbing in place (comments are mine and some code was omitted for clarity’s
sake):
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;private bool MoveNext()&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
this.&amp;lt;&amp;gt;1__state = -1;&lt;br /&gt;
this.&amp;lt;i&amp;gt;5__1 = 0;&lt;br /&gt;
// My for loop has changed to a while loop.&lt;br /&gt;
while (this.&amp;lt;i&amp;gt;5__1 &amp;lt; 100)&lt;br /&gt;
{&lt;br /&gt;
// Sets a local value&lt;br /&gt;
this.&amp;lt;value&amp;gt;5__2 = this.&amp;lt;i&amp;gt;5__1.ToString();&lt;br /&gt;
// Here is my Console.WriteLine(...)&lt;br /&gt;
Console.WriteLine(&amp;quot;Returning {0} to caller.&amp;quot;, this.&amp;lt;value&amp;gt;5__2);&lt;br /&gt;
// Here is where the current member variable&lt;br /&gt;
// gets stored.&lt;br /&gt;
this.&amp;lt;&amp;gt;2__current = this.&amp;lt;value&amp;gt;5__2;&lt;br /&gt;
this.&amp;lt;&amp;gt;1__state = 1;&lt;br /&gt;
// We return &amp;quot;true&amp;quot; to the caller so it knows&lt;br /&gt;
// there is another record to be processed.&lt;br /&gt;
return true;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
// Here is my Console.WriteLine() at the bottom&lt;br /&gt;
// when we've finished processing the loop.&lt;br /&gt;
Console.WriteLine(&amp;quot;Method done!&amp;quot;);&lt;br /&gt;
break;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
Pretty straightforward.&amp;#160; Of course, the real power is that it the compiler converts
the “yield return &amp;lt;blah&amp;gt;” into a nice clean enumerator with a MoveNext().&amp;#160;
In-fact, if you’ve used LINQ, you’ve probably used this feature without even knowing
it.&amp;#160; Consider the following code:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;private static void OutputLinqToXmlQuery()&lt;br /&gt;
{&lt;br /&gt;
XDocument doc = XDocument.Parse&lt;br /&gt;
(@&amp;quot;&amp;lt;root&amp;gt;&amp;lt;data&amp;gt;hello world&amp;lt;/data&amp;gt;&amp;lt;data&amp;gt;goodbye world&amp;lt;/data&amp;gt;&amp;lt;/root&amp;gt;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
var results = from data in doc.Descendants(&amp;quot;data&amp;quot;)&lt;br /&gt;
select data;&lt;br /&gt;
&lt;br /&gt;
foreach (var result in results)&lt;br /&gt;
{&lt;br /&gt;
Console.WriteLine(result); 
&lt;br /&gt;
}&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
The “results” object, by default will be of type “WhereSelectEnumerableIterator” which
exposes a MoveNext() method.&amp;#160; In-fact, that is also why the results object doesn’t
allow you to do something like this:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;var results = from data in doc.Descendants(&amp;quot;data&amp;quot;)&lt;br /&gt;
select data;&lt;br /&gt;
&lt;br /&gt;
var bad = results[1];&lt;/pre&gt;
&lt;p&gt;
The IEnumerator does not expose an indexer allowing you to go straight to a particular
element in the collection because the full collection hasn’t been generated yet.&amp;#160;
Instead, you would do something like this:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;var results = from data in doc.Descendants(&amp;quot;data&amp;quot;)&lt;br /&gt;
select data; 
&lt;br /&gt;
&lt;br /&gt;
var good = results.ElementAt(1);&lt;/pre&gt;
&lt;p&gt;
And then under the covers, the ElementAt(int) method will just keep calling MoveNext()
until it reaches the index you specified.&amp;#160; Something like this: 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;&lt;em&gt;Note:&amp;#160; this is my own code and is NOT from the .NET Framework – it
is merely meant to illustrate a point.&lt;/em&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;/blockquote&gt; &lt;pre class="c#" name="code"&gt;public static XElement MyElementAt(this IEnumerable&amp;lt;XElement&amp;gt; elements, 
&lt;br /&gt;
int index)&lt;br /&gt;
{&lt;br /&gt;
int counter = 0;&lt;br /&gt;
using (IEnumerator&amp;lt;XElement&amp;gt; enumerator = 
&lt;br /&gt;
elements.GetEnumerator())&lt;br /&gt;
{ 
&lt;br /&gt;
while(enumerator.MoveNext()){&lt;br /&gt;
if (counter == index)&lt;br /&gt;
return enumerator.Current;&lt;br /&gt;
counter++;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
return null;&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
Hope this helps to demystify some things and put another tool in your toolbox.
&lt;/p&gt;
&lt;p&gt;
Until next time.
&lt;/p&gt;</description>
      <comments>http://www.samuraiprogrammer.com/blog/CommentView,guid,79911823-e816-4166-8552-884b2ea29759.aspx</comments>
      <category>.NET</category>
      <category>Performance</category>
    </item>
    <item>
      <trackback:ping>http://www.samuraiprogrammer.com/blog/Trackback.aspx?guid=38980ec9-3b6a-4667-b745-9456639486f5</trackback:ping>
      <pingback:server>http://www.samuraiprogrammer.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.samuraiprogrammer.com/blog/PermaLink,guid,38980ec9-3b6a-4667-b745-9456639486f5.aspx</pingback:target>
      <dc:creator>Greg Varveris</dc:creator>
      <wfw:comment>http://www.samuraiprogrammer.com/blog/CommentView,guid,38980ec9-3b6a-4667-b745-9456639486f5.aspx</wfw:comment>
      <wfw:commentRss>http://www.samuraiprogrammer.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=38980ec9-3b6a-4667-b745-9456639486f5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/Ricky_Bobby_I_wanna_go_fast_2.jpg">
            <img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="Ricky_Bobby_I_wanna_go_fast" border="0" alt="Ricky_Bobby_I_wanna_go_fast" align="left" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/Ricky_Bobby_I_wanna_go_fast_thumb.jpg" width="227" height="244" />
          </a>Many
times, when I work with a customer, it’s because they’ve tried to accomplish something
and need a little extra help.  Often, this falls into the application optimization
area.  For example, a few years ago, I had a customer that was developing a rather
sophisticated SharePoint workflow that had some custom code that would process and
merge two Excel spreadsheets together.  They were using Excel 2007 so their merging
was being done using the excellent <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&amp;DisplayLang=en">Open
XML SDK</a>.  To their credit, the application did what it needed to do – but
it took about an hour to process these spreadsheets.  The developers on the project
knew about the performance problems but as so often happens, they thought they knew
where the bottlenecks were and how they should approach optimizing it.  So, they
started injecting some tracing into their code and worked hard to optimize this lengthy
process.  After a while, though, they had only shaved a few seconds off of that
60 minute time and while they did show some improvement – they knew they needed to
get the processing done even faster for this to become a viable solution for their
organization.  So, they sent me a simple repro of the code and together in just
a few short days, we were able to get the processing from 60 minutes to under a minute. 
That’s a BIG win.  Big like the Titanic big.  
</p>
        <p>
I love these types of engagements because, well, I like to make things faster. 
The biggest problem that I see, though, is that some people shy away from using the
tools in optimization scenarios because they’ve been so invested in their code that
they think they know why it’s not performing.  This is the “psychic” effect and
the mindset is usually something like this:  
</p>
        <blockquote>
          <p>
“I wrote this darn code and while I was writing it – I knew this method could be improved
so now I’m going to finally optimize the darn thing.”  
</p>
        </blockquote>
        <p>
It sounds good in-theory, right?  You wrote the code, so you should know how
to improve it and really, what’s a tool going to tell you that you don’t already know? 
In truth, it can tell you quite a bit.  In other situations, the developers will
add some instrumentation (via tracing/debugging statements) to what they perceive
as the critical code paths and the resultant timing points to one area when the problem
really resides in a completely different section of code.  That’s right, folks,
your tracing statements may be lying to you.  So what’s a developer to do? 
Well, use the tools, of course – the right tool for the right job, as they say. 
Let me expand upon that in the context of some code that yours truly wrote a little
while ago.
</p>
        <h3>The Problem
</h3>
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/2010/08/29/ParsingASPNETEventLogErrorMessagesForFunAndProfit.aspx">A
few short weeks ago, I posted a entry about parsing your ASP.NET event log error messages</a>. 
For those that didn’t read it, we just convert the EventLog messages to XML, parse
the messages using RegEx.Match and then generate some statistics on them.  The
code I provided on that blog entry appeared to be very fast when it was processing
a few records – but as the XML files grew, I started noticing that it seemed to take
longer and longer to process the results.  So, I did what any developer does
initially.  We’ll add some instrumentation into our code to see if we can figure
out the problem.  This usually takes the form of something like the following:
</p>
        <pre class="c#" name="code">Console.WriteLine("Start:  Load XDocument." + DateTime.Now.ToString());<br />
XDocument document = XDocument.Load(@"C:\Work\TestData\AllEVTX\MyData.xml");<br />
Console.WriteLine("End: Load XDocument." + DateTime.Now.ToString());<br /><br />
Console.WriteLine("Start: Load Messages into object." + DateTime.Now.ToString());<br />
var messages = from message in document.Descendants("Message")<br />
select EventLogMessage.Load(message.Value);<br />
Console.WriteLine("End: Load Messages into object." + DateTime.Now.ToString());<br /><br />
Console.WriteLine("Start: Query Objects." + DateTime.Now.ToString());<br />
var results = from log in messages<br />
group log by log.Exceptiontype into l<br />
orderby l.Count() descending, l.Key<br />
select new<br />
{<br />
ExceptionType = l.Key,<br />
ExceptionCount = l.Count()<br />
};<br />
Console.WriteLine("End: Query Objects." + DateTime.Now.ToString());<br /><br />
Console.WriteLine("Start: Output Exception Type Details." + DateTime.Now.ToString());<br />
foreach (var result in results)<br />
{<br />
Console.WriteLine("{0} : {1} time(s)",<br />
result.ExceptionType,<br />
result.ExceptionCount);<br />
}<br />
Console.WriteLine("End: Output Exception Type Details." + DateTime.Now.ToString());</pre>
        <p>
As you can see, all I’ve done is taken the code and slapped some Console.WriteLine
statements with a DateTime.Now call to find out when the operation starts and when
it completes.  If I run this code I get the following timings:
</p>
        <blockquote>
          <p>
Start:  Load XDocument.9/19/2010 11:28:08 AM 
<br />
End:  Load XDocument.9/19/2010 11:28:09 AM 
<br />
Start:  Load Messages into object.9/19/2010 11:28:09 AM 
<br />
End: Load Messages into object.9/19/2010 11:28:09 AM 
<br />
Start:  Query Objects.9/19/2010 11:28:09 AM 
<br />
End:  Query Objects.9/19/2010 11:28:09 AM 
<br /><font color="#ff0000"><strong>Start:  Output Exception Type Details.9/19/2010
11:28:09 AM 
<br />
End:  Output Exception Type Details.9/19/2010 11:28:49 AM</strong></font></p>
        </blockquote>
        <p>
This is clearly a problem  Now, the question is why?
</p>
        <h3>The Research
</h3>
        <p>
This might lead you to believe that the simple loop I have to get the ExceptionType
and the ExceptionCount was the root of all evil, so to speak.  The problem, though,
is that there’s not really a lot you can do to improve this:
</p>
        <pre class="c#" name="code">foreach (var result in results)<br />
{<br />
Console.WriteLine("{0} : {1} time(s)", 
<br />
result.ExceptionType, 
<br />
result.ExceptionCount); 
<br />
}</pre>
        <p>
Oh sure, you could use the excellent Parallel functionality in .NET 4 to send off
the work to alternate threads.  So, you might do something like this:
</p>
        <pre class="c#" name="code">Parallel.ForEach(results, result=&gt; {<br /><br />
Console.WriteLine("{0} : {1} times(s)", 
<br />
result.ExceptionType, 
<br />
result.ExceptionCount);<br /><br />
});</pre>
        <p>
But if you re-run the test, you get the following results:
</p>
        <blockquote>
          <p>
Start:  Load XDocument.9/19/2010 11:46:20 AM 
<br />
End:  Load XDocument.9/19/2010 11:46:20 AM 
<br />
Start:  Load Messages into object.9/19/2010 11:46:20 AM 
<br />
End: Load Messages into object.9/19/2010 11:46:20 AM 
<br />
Start:  Query Objects.9/19/2010 11:46:20 AM 
<br />
End:  Query Objects.9/19/2010 11:46:20 AM 
<br /><font color="#ff0000"><strong>Start:  Output Exception Type Details.9/19/2010
11:46:20 AM 
<br />
End:  Output Exception Type Details.9/19/2010 11:46:54 AM</strong></font></p>
        </blockquote>
        <p>
Wait a second…is it taking longer now???  That’s just not right.  At this
point, you should take a step back and tell yourself <strong>“STOP GUESSING AND JUST
USE THE PROFILER.”  </strong>So, always being one to listen to myself, I kick
off the Visual Studio profiler and immediately it shows us the “hot path”.  This
was actually a nice improvement in the VS2010 profiler.  Right on the summary
page, it will show us our most expensive call paths:
</p>
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/image_2.png">
            <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/image_thumb.png" width="809" height="194" />
          </a>
        </p>
        <p>
But wait, it’s pointing to the RegEx.Match(…).  Why is it pointing to that? 
From my own metrics above, I see that the loading of the Message strings into an object
takes less than a second to execute.  Well, the real reason is that LINQ uses
kind of a lazy loading algorithm.  Basically, it won’t necessarily process your
queries until you try to do something with the data.  Yes, that’s an over-simplification
but in our case, it means that my EventLogMessage.Load(…) method won’t be called until
I actually try to do something with the data.  So, now, armed with this information,
I can take a look at my Load() method and see what it’s actually doing and how it’s
using the RegEx.Match(…) functionality:
</p>
        <pre class="c#" name="code">Match myMatch = s_regex.Match(rawMessageText);<br /><br />
EventLogMessage message = new EventLogMessage();<br /><br />
message.Eventcode = myMatch.Groups["Eventcode"].Value;<br />
message.Eventmessage = myMatch.Groups["Eventmessage"].Value;<br />
message.Eventtime = myMatch.Groups["Eventtime"].Value;<br />
message.EventtimeUTC = myMatch.Groups["EventtimeUTC"].Value;<br />
...<br /><br />
return message;</pre>
        <p>
So, basically we’re using the Match(…) to take that Message property and parse it
out into the properties of the EventLogMessage object.  The fact that this is
the slowest part of the code shouldn’t necessarily shock you.  <a href="http://www.codinghorror.com/blog/2006/01/regex-performance.html">Jeff
Atwood wrote up a good blog entry a few years ago on something like this</a>. 
If we look just at the RegEx.Match(…) in the profiler, we see that the problem isn’t
necessarily with each Match call but the overall cost with 10,000+ calls:
</p>
        <div class="content">
          <table cellspacing="0" cellpadding="2">
            <thead>
              <tr>
                <th>
Function Name</th>
                <th>
Number of Calls</th>
                <th>
Min Elapsed Inclusive Time</th>
                <th>
Avg Elapsed Inclusive Time</th>
                <th>
Max Elapsed Inclusive Time</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <table style="margin-left: 0px" class="expansion" cellspacing="0" cellpadding="0">
                    <tbody>
                      <tr>
                        <td>
System.Text.RegularExpressions.Regex.Match(string)</td>
                      </tr>
                    </tbody>
                  </table>
                </td>
                <td>
                  <font style="background-color: #ffff00">10,881</font>
                </td>
                <td>
                  <font style="background-color: #ffff00">1.01</font>
                </td>
                <td>
                  <font style="background-color: #ffff00">3.27</font>
                </td>
                <td>
                  <font style="background-color: #ffff00">684.29</font>
                </td>
              </tr>
            </tbody>
          </table>
          <br />
        </div>
        <h3>The Fix
</h3>
        <p>
So, now that we know the problem and the reason for it – what’s a Dev to do? 
Well, at this point, we should be thinking of an alternate way of performing this
parsing without using RegEx.  The simplest method is just to use some general
string parsing, so we’ll start off with replacing our large RegEx pattern with a simple
string array:
</p>
        <pre class="c#" name="code">public static readonly string[] ParserString = new string[] {<br />
@"Event code:" ,<br />
@"Event message:" ,<br />
@"Event time:" ,<br />
@"Event time (UTC):" ,<br />
@"Event ID:" ,<br />
...<br />
@"Stack trace:" ,<br />
@"Custom event details:"};</pre>
        <p>
This array will be used in our fancy new GetValue method:
</p>
        <pre class="c#" name="code">private static string GetValue(string rawMessageText, int Key)<br />
{<br />
int startLoc = rawMessageText.IndexOf(ParserString[Key]);<br />
int endLoc;<br />
if (Key + 1 == ParserString.Length)<br />
endLoc = rawMessageText.Length;<br />
else<br />
endLoc = rawMessageText.IndexOf(ParserString[Key + 1], startLoc);<br /><br />
return rawMessageText.Substring(startLoc + ParserString[Key].Length, endLoc - startLoc
- ParserString[Key].Length);<br />
}</pre>
        <p>
This method accepts our raw message string and a key index.  This method just
finds the existence of a string like “Event message:” in our raw message and then
finds the index of the next string, like “Event time:” and subtracts the two to get
at the value of the field.  For example, given the following string:
</p>
        <blockquote>
          <p>
            <font color="#ff0000">
              <strong>“… Event message:</strong>
            </font>
            <font style="background-color: #ffff00" color="#0000ff">
              <strong>
                <em>An
unhandled exception has occurred.</em>
              </strong>
            </font>
            <font color="#ff0000">
              <strong>Event
time: …”</strong>
            </font>
          </p>
        </blockquote>
        <p>
The red text are the keys and the highlighted blue text is the string between them. 
The idea for the above GetValue(…) method was actually provided by a fellow <a href="http://blogs.msdn.com/b/pfedev/">PFE
engineer</a>, Richard Lang during a late night chat session.
</p>
        <p>
The last step to this process is just to call the GetValue(…) method from our new
Load method:
</p>
        <pre class="c#" name="code">public static EventLogMessage Load(string rawMessageText)<br />
{<br />
EventLogMessage message = new EventLogMessage();<br /><br />
int Key = 0;<br />
message.Eventcode = GetValue(rawMessageText, Key++);<br />
message.Eventmessage = GetValue(rawMessageText, Key++); ;<br />
message.Eventtime = GetValue(rawMessageText, Key++); ;<br />
...<br />
return message;<br /><br />
}</pre>
        <p>
So, now we’ve essentially removed the need for RegEx by implementing our own string
parsing algorithm.  Once we compile the code and run our application again, we
see some major improvements:
</p>
        <blockquote>
          <p>
Start:  Load XDocument.9/19/2010 12:55:31 PM 
<br />
End:  Load XDocument.9/19/2010 12:55:31 PM 
<br />
Start:  Load Messages into object.9/19/2010 12:55:31 PM 
<br />
End: Load Messages into object.9/19/2010 12:55:31 PM 
<br />
Start:  Query Objects.9/19/2010 12:55:31 PM 
<br />
End:  Query Objects.9/19/2010 12:55:31 PM 
<br /><font color="#ff0000"><strong>Start:  Output Exception Type Details.9/19/2010
12:55:31 PM 
<br />
End:  Output Exception Type Details.9/19/2010 12:55:33 PM</strong></font></p>
        </blockquote>
        <p>
We essentially improved the processing of our records from 40 seconds to under 2 seconds. 
I’d say that’s a pretty big improvement.  Even better, we can use our Parallel.ForEach(…)
code from above to make this even faster since we’re no longer bound by the RegEx
parser:
</p>
        <blockquote>
          <p>
Start:  Load XDocument.9/19/2010 1:00:13 PM 
<br />
End:  Load XDocument.9/19/2010 1:00:13 PM 
<br />
Start:  Load Messages into object.9/19/2010 1:00:13 PM 
<br />
End: Load Messages into object.9/19/2010 1:00:13 PM 
<br />
Start:  Query Objects.9/19/2010 1:00:13 PM 
<br />
End:  Query Objects.9/19/2010 1:00:13 PM 
<br /><font color="#ff0000"><strong>Start:  Output Exception Type Details.9/19/2010
1:00:13 PM 
<br />
End:  Output Exception Type Details.9/19/2010 1:00:14 PM</strong></font></p>
        </blockquote>
        <p>
So now it takes just about a second to process these records.  Considering it’s
processing over 10,000 event log messages, I’d say this is acceptable performance,
for now.
</p>
        <h3>Closing Comments
</h3>
        <p>
I just want to say a few things real quick.  RegEx is not inherently evil. 
It is still one of the fastest and easiest methods to consume and parse data. 
You should not feel the need to go back to your own applications that are working
just fine and refactor all of your code to strip out the RegEx expressions. 
It just so happens that sometimes too much of a good thing can be bad for your health. 
In our case, with over 10,000 RegEx.Match(…) calls in rapid succession, the RegEx
appeared to be our bottleneck.  This may or may not be the root cause of your
own performance problems.  The key takeaway from this blog entry should be that
you should <strong>NOT</strong> guess when it comes to optimizing code paths. 
Instead, you should use the tools available at your disposal to find the bottleneck.  
</p>
        <p>
Until next time.
</p>
      </body>
      <title>Don’t guess when it comes to performance…a RegEx story.</title>
      <guid isPermaLink="false">http://www.samuraiprogrammer.com/blog/PermaLink,guid,38980ec9-3b6a-4667-b745-9456639486f5.aspx</guid>
      <link>http://www.samuraiprogrammer.com/blog/2010/09/19/DontGuessWhenItComesToPerformanceaRegExStory.aspx</link>
      <pubDate>Sun, 19 Sep 2010 18:30:02 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/Ricky_Bobby_I_wanna_go_fast_2.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" title="Ricky_Bobby_I_wanna_go_fast" border="0" alt="Ricky_Bobby_I_wanna_go_fast" align="left" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/Ricky_Bobby_I_wanna_go_fast_thumb.jpg" width="227" height="244" /&gt;&lt;/a&gt;Many
times, when I work with a customer, it’s because they’ve tried to accomplish something
and need a little extra help.&amp;#160; Often, this falls into the application optimization
area.&amp;#160; For example, a few years ago, I had a customer that was developing a rather
sophisticated SharePoint workflow that had some custom code that would process and
merge two Excel spreadsheets together.&amp;#160; They were using Excel 2007 so their merging
was being done using the excellent &lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&amp;amp;DisplayLang=en"&gt;Open
XML SDK&lt;/a&gt;.&amp;#160; To their credit, the application did what it needed to do – but
it took about an hour to process these spreadsheets.&amp;#160; The developers on the project
knew about the performance problems but as so often happens, they thought they knew
where the bottlenecks were and how they should approach optimizing it.&amp;#160; So, they
started injecting some tracing into their code and worked hard to optimize this lengthy
process.&amp;#160; After a while, though, they had only shaved a few seconds off of that
60 minute time and while they did show some improvement – they knew they needed to
get the processing done even faster for this to become a viable solution for their
organization.&amp;#160; So, they sent me a simple repro of the code and together in just
a few short days, we were able to get the processing from 60 minutes to under a minute.&amp;#160;
That’s a BIG win.&amp;#160; Big like the Titanic big.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
I love these types of engagements because, well, I like to make things faster.&amp;#160;
The biggest problem that I see, though, is that some people shy away from using the
tools in optimization scenarios because they’ve been so invested in their code that
they think they know why it’s not performing.&amp;#160; This is the “psychic” effect and
the mindset is usually something like this:&amp;#160; 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
“I wrote this darn code and while I was writing it – I knew this method could be improved
so now I’m going to finally optimize the darn thing.”&amp;#160; 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
It sounds good in-theory, right?&amp;#160; You wrote the code, so you should know how
to improve it and really, what’s a tool going to tell you that you don’t already know?&amp;#160;
In truth, it can tell you quite a bit.&amp;#160; In other situations, the developers will
add some instrumentation (via tracing/debugging statements) to what they perceive
as the critical code paths and the resultant timing points to one area when the problem
really resides in a completely different section of code.&amp;#160; That’s right, folks,
your tracing statements may be lying to you.&amp;#160; So what’s a developer to do?&amp;#160;
Well, use the tools, of course – the right tool for the right job, as they say.&amp;#160;
Let me expand upon that in the context of some code that yours truly wrote a little
while ago.
&lt;/p&gt;
&lt;h3&gt;The Problem
&lt;/h3&gt;
&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/2010/08/29/ParsingASPNETEventLogErrorMessagesForFunAndProfit.aspx"&gt;A
few short weeks ago, I posted a entry about parsing your ASP.NET event log error messages&lt;/a&gt;.&amp;#160;
For those that didn’t read it, we just convert the EventLog messages to XML, parse
the messages using RegEx.Match and then generate some statistics on them.&amp;#160; The
code I provided on that blog entry appeared to be very fast when it was processing
a few records – but as the XML files grew, I started noticing that it seemed to take
longer and longer to process the results.&amp;#160; So, I did what any developer does
initially.&amp;#160; We’ll add some instrumentation into our code to see if we can figure
out the problem.&amp;#160; This usually takes the form of something like the following:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;Console.WriteLine(&amp;quot;Start:  Load XDocument.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
XDocument document = XDocument.Load(@&amp;quot;C:\Work\TestData\AllEVTX\MyData.xml&amp;quot;);&lt;br /&gt;
Console.WriteLine(&amp;quot;End: Load XDocument.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
&lt;br /&gt;
Console.WriteLine(&amp;quot;Start: Load Messages into object.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
var messages = from message in document.Descendants(&amp;quot;Message&amp;quot;)&lt;br /&gt;
select EventLogMessage.Load(message.Value);&lt;br /&gt;
Console.WriteLine(&amp;quot;End: Load Messages into object.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
&lt;br /&gt;
Console.WriteLine(&amp;quot;Start: Query Objects.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
var results = from log in messages&lt;br /&gt;
group log by log.Exceptiontype into l&lt;br /&gt;
orderby l.Count() descending, l.Key&lt;br /&gt;
select new&lt;br /&gt;
{&lt;br /&gt;
ExceptionType = l.Key,&lt;br /&gt;
ExceptionCount = l.Count()&lt;br /&gt;
};&lt;br /&gt;
Console.WriteLine(&amp;quot;End: Query Objects.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
&lt;br /&gt;
Console.WriteLine(&amp;quot;Start: Output Exception Type Details.&amp;quot; + DateTime.Now.ToString());&lt;br /&gt;
foreach (var result in results)&lt;br /&gt;
{&lt;br /&gt;
Console.WriteLine(&amp;quot;{0} : {1} time(s)&amp;quot;,&lt;br /&gt;
result.ExceptionType,&lt;br /&gt;
result.ExceptionCount);&lt;br /&gt;
}&lt;br /&gt;
Console.WriteLine(&amp;quot;End: Output Exception Type Details.&amp;quot; + DateTime.Now.ToString());&lt;/pre&gt;
&lt;p&gt;
As you can see, all I’ve done is taken the code and slapped some Console.WriteLine
statements with a DateTime.Now call to find out when the operation starts and when
it completes.&amp;#160; If I run this code I get the following timings:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Start:&amp;#160; Load XDocument.9/19/2010 11:28:08 AM 
&lt;br /&gt;
End:&amp;#160; Load XDocument.9/19/2010 11:28:09 AM 
&lt;br /&gt;
Start:&amp;#160; Load Messages into object.9/19/2010 11:28:09 AM 
&lt;br /&gt;
End: Load Messages into object.9/19/2010 11:28:09 AM 
&lt;br /&gt;
Start:&amp;#160; Query Objects.9/19/2010 11:28:09 AM 
&lt;br /&gt;
End:&amp;#160; Query Objects.9/19/2010 11:28:09 AM 
&lt;br /&gt;
&lt;font color="#ff0000"&gt;&lt;strong&gt;Start:&amp;#160; Output Exception Type Details.9/19/2010
11:28:09 AM 
&lt;br /&gt;
End:&amp;#160; Output Exception Type Details.9/19/2010 11:28:49 AM&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This is clearly a problem&amp;#160; Now, the question is why?
&lt;/p&gt;
&lt;h3&gt;The Research
&lt;/h3&gt;
&lt;p&gt;
This might lead you to believe that the simple loop I have to get the ExceptionType
and the ExceptionCount was the root of all evil, so to speak.&amp;#160; The problem, though,
is that there’s not really a lot you can do to improve this:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;foreach (var result in results)&lt;br /&gt;
{&lt;br /&gt;
Console.WriteLine(&amp;quot;{0} : {1} time(s)&amp;quot;, 
&lt;br /&gt;
result.ExceptionType, 
&lt;br /&gt;
result.ExceptionCount); 
&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
Oh sure, you could use the excellent Parallel functionality in .NET 4 to send off
the work to alternate threads.&amp;#160; So, you might do something like this:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;Parallel.ForEach(results, result=&amp;gt; {&lt;br /&gt;
&lt;br /&gt;
Console.WriteLine(&amp;quot;{0} : {1} times(s)&amp;quot;, 
&lt;br /&gt;
result.ExceptionType, 
&lt;br /&gt;
result.ExceptionCount);&lt;br /&gt;
&lt;br /&gt;
});&lt;/pre&gt;
&lt;p&gt;
But if you re-run the test, you get the following results:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Start:&amp;#160; Load XDocument.9/19/2010 11:46:20 AM 
&lt;br /&gt;
End:&amp;#160; Load XDocument.9/19/2010 11:46:20 AM 
&lt;br /&gt;
Start:&amp;#160; Load Messages into object.9/19/2010 11:46:20 AM 
&lt;br /&gt;
End: Load Messages into object.9/19/2010 11:46:20 AM 
&lt;br /&gt;
Start:&amp;#160; Query Objects.9/19/2010 11:46:20 AM 
&lt;br /&gt;
End:&amp;#160; Query Objects.9/19/2010 11:46:20 AM 
&lt;br /&gt;
&lt;font color="#ff0000"&gt;&lt;strong&gt;Start:&amp;#160; Output Exception Type Details.9/19/2010
11:46:20 AM 
&lt;br /&gt;
End:&amp;#160; Output Exception Type Details.9/19/2010 11:46:54 AM&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Wait a second…is it taking longer now???&amp;#160; That’s just not right.&amp;#160; At this
point, you should take a step back and tell yourself &lt;strong&gt;“STOP GUESSING AND JUST
USE THE PROFILER.”&amp;#160; &lt;/strong&gt;So, always being one to listen to myself, I kick
off the Visual Studio profiler and immediately it shows us the “hot path”.&amp;#160; This
was actually a nice improvement in the VS2010 profiler.&amp;#160; Right on the summary
page, it will show us our most expensive call paths:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/image_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.samuraiprogrammer.com/blog/content/binary/Windows-Live-Writer/b173a31ed118_99C1/image_thumb.png" width="809" height="194" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
But wait, it’s pointing to the RegEx.Match(…).&amp;#160; Why is it pointing to that?&amp;#160;
From my own metrics above, I see that the loading of the Message strings into an object
takes less than a second to execute.&amp;#160; Well, the real reason is that LINQ uses
kind of a lazy loading algorithm.&amp;#160; Basically, it won’t necessarily process your
queries until you try to do something with the data.&amp;#160; Yes, that’s an over-simplification
but in our case, it means that my EventLogMessage.Load(…) method won’t be called until
I actually try to do something with the data.&amp;#160; So, now, armed with this information,
I can take a look at my Load() method and see what it’s actually doing and how it’s
using the RegEx.Match(…) functionality:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;Match myMatch = s_regex.Match(rawMessageText);&lt;br /&gt;
&lt;br /&gt;
EventLogMessage message = new EventLogMessage();&lt;br /&gt;
&lt;br /&gt;
message.Eventcode = myMatch.Groups[&amp;quot;Eventcode&amp;quot;].Value;&lt;br /&gt;
message.Eventmessage = myMatch.Groups[&amp;quot;Eventmessage&amp;quot;].Value;&lt;br /&gt;
message.Eventtime = myMatch.Groups[&amp;quot;Eventtime&amp;quot;].Value;&lt;br /&gt;
message.EventtimeUTC = myMatch.Groups[&amp;quot;EventtimeUTC&amp;quot;].Value;&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
return message;&lt;/pre&gt;
&lt;p&gt;
So, basically we’re using the Match(…) to take that Message property and parse it
out into the properties of the EventLogMessage object.&amp;#160; The fact that this is
the slowest part of the code shouldn’t necessarily shock you.&amp;#160; &lt;a href="http://www.codinghorror.com/blog/2006/01/regex-performance.html"&gt;Jeff
Atwood wrote up a good blog entry a few years ago on something like this&lt;/a&gt;.&amp;#160;
If we look just at the RegEx.Match(…) in the profiler, we see that the problem isn’t
necessarily with each Match call but the overall cost with 10,000+ calls:
&lt;/p&gt;
&lt;div class="content"&gt;
&lt;table cellspacing="0" cellpadding="2"&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
Function Name&lt;/th&gt;
&lt;th&gt;
Number of Calls&lt;/th&gt;
&lt;th&gt;
Min Elapsed Inclusive Time&lt;/th&gt;
&lt;th&gt;
Avg Elapsed Inclusive Time&lt;/th&gt;
&lt;th&gt;
Max Elapsed Inclusive Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;table style="margin-left: 0px" class="expansion" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
System.Text.RegularExpressions.Regex.Match(string)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;font style="background-color: #ffff00"&gt;10,881&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font style="background-color: #ffff00"&gt;1.01&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font style="background-color: #ffff00"&gt;3.27&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;
&lt;font style="background-color: #ffff00"&gt;684.29&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;h3&gt;The Fix
&lt;/h3&gt;
&lt;p&gt;
So, now that we know the problem and the reason for it – what’s a Dev to do?&amp;#160;
Well, at this point, we should be thinking of an alternate way of performing this
parsing without using RegEx.&amp;#160; The simplest method is just to use some general
string parsing, so we’ll start off with replacing our large RegEx pattern with a simple
string array:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;public static readonly string[] ParserString = new string[] {&lt;br /&gt;
@&amp;quot;Event code:&amp;quot; ,&lt;br /&gt;
@&amp;quot;Event message:&amp;quot; ,&lt;br /&gt;
@&amp;quot;Event time:&amp;quot; ,&lt;br /&gt;
@&amp;quot;Event time (UTC):&amp;quot; ,&lt;br /&gt;
@&amp;quot;Event ID:&amp;quot; ,&lt;br /&gt;
...&lt;br /&gt;
@&amp;quot;Stack trace:&amp;quot; ,&lt;br /&gt;
@&amp;quot;Custom event details:&amp;quot;};&lt;/pre&gt;
&lt;p&gt;
This array will be used in our fancy new GetValue method:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;private static string GetValue(string rawMessageText, int Key)&lt;br /&gt;
{&lt;br /&gt;
int startLoc = rawMessageText.IndexOf(ParserString[Key]);&lt;br /&gt;
int endLoc;&lt;br /&gt;
if (Key + 1 == ParserString.Length)&lt;br /&gt;
endLoc = rawMessageText.Length;&lt;br /&gt;
else&lt;br /&gt;
endLoc = rawMessageText.IndexOf(ParserString[Key + 1], startLoc);&lt;br /&gt;
&lt;br /&gt;
return rawMessageText.Substring(startLoc + ParserString[Key].Length, endLoc - startLoc
- ParserString[Key].Length);&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
This method accepts our raw message string and a key index.&amp;#160; This method just
finds the existence of a string like “Event message:” in our raw message and then
finds the index of the next string, like “Event time:” and subtracts the two to get
at the value of the field.&amp;#160; For example, given the following string:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font color="#ff0000"&gt;&lt;strong&gt;“… Event message:&lt;/strong&gt;&lt;/font&gt; &lt;font style="background-color: #ffff00" color="#0000ff"&gt;&lt;strong&gt;&lt;em&gt;An
unhandled exception has occurred.&lt;/em&gt;&lt;/strong&gt;&lt;/font&gt; &lt;font color="#ff0000"&gt;&lt;strong&gt;Event
time: …”&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The red text are the keys and the highlighted blue text is the string between them.&amp;#160;
The idea for the above GetValue(…) method was actually provided by a fellow &lt;a href="http://blogs.msdn.com/b/pfedev/"&gt;PFE
engineer&lt;/a&gt;, Richard Lang during a late night chat session.
&lt;/p&gt;
&lt;p&gt;
The last step to this process is just to call the GetValue(…) method from our new
Load method:
&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;public static EventLogMessage Load(string rawMessageText)&lt;br /&gt;
{&lt;br /&gt;
EventLogMessage message = new EventLogMessage();&lt;br /&gt;
&lt;br /&gt;
int Key = 0;&lt;br /&gt;
message.Eventcode = GetValue(rawMessageText, Key++);&lt;br /&gt;
message.Eventmessage = GetValue(rawMessageText, Key++); ;&lt;br /&gt;
message.Eventtime = GetValue(rawMessageText, Key++); ;&lt;br /&gt;
...&lt;br /&gt;
return message;&lt;br /&gt;
&lt;br /&gt;
}&lt;/pre&gt;
&lt;p&gt;
So, now we’ve essentially removed the need for RegEx by implementing our own string
parsing algorithm.&amp;#160; Once we compile the code and run our application again, we
see some major improvements:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Start:&amp;#160; Load XDocument.9/19/2010 12:55:31 PM 
&lt;br /&gt;
End:&amp;#160; Load XDocument.9/19/2010 12:55:31 PM 
&lt;br /&gt;
Start:&amp;#160; Load Messages into object.9/19/2010 12:55:31 PM 
&lt;br /&gt;
End: Load Messages into object.9/19/2010 12:55:31 PM 
&lt;br /&gt;
Start:&amp;#160; Query Objects.9/19/2010 12:55:31 PM 
&lt;br /&gt;
End:&amp;#160; Query Objects.9/19/2010 12:55:31 PM 
&lt;br /&gt;
&lt;font color="#ff0000"&gt;&lt;strong&gt;Start:&amp;#160; Output Exception Type Details.9/19/2010
12:55:31 PM 
&lt;br /&gt;
End:&amp;#160; Output Exception Type Details.9/19/2010 12:55:33 PM&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
We essentially improved the processing of our records from 40 seconds to under 2 seconds.&amp;#160;
I’d say that’s a pretty big improvement.&amp;#160; Even better, we can use our Parallel.ForEach(…)
code from above to make this even faster since we’re no longer bound by the RegEx
parser:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Start:&amp;#160; Load XDocument.9/19/2010 1:00:13 PM 
&lt;br /&gt;
End:&amp;#160; Load XDocument.9/19/2010 1:00:13 PM 
&lt;br /&gt;
Start:&amp;#160; Load Messages into object.9/19/2010 1:00:13 PM 
&lt;br /&gt;
End: Load Messages into object.9/19/2010 1:00:13 PM 
&lt;br /&gt;
Start:&amp;#160; Query Objects.9/19/2010 1:00:13 PM 
&lt;br /&gt;
End:&amp;#160; Query Objects.9/19/2010 1:00:13 PM 
&lt;br /&gt;
&lt;font color="#ff0000"&gt;&lt;strong&gt;Start:&amp;#160; Output Exception Type Details.9/19/2010
1:00:13 PM 
&lt;br /&gt;
End:&amp;#160; Output Exception Type Details.9/19/2010 1:00:14 PM&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
So now it takes just about a second to process these records.&amp;#160; Considering it’s
processing over 10,000 event log messages, I’d say this is acceptable performance,
for now.
&lt;/p&gt;
&lt;h3&gt;Closing Comments
&lt;/h3&gt;
&lt;p&gt;
I just want to say a few things real quick.&amp;#160; RegEx is not inherently evil.&amp;#160;
It is still one of the fastest and easiest methods to consume and parse data.&amp;#160;
You should not feel the need to go back to your own applications that are working
just fine and refactor all of your code to strip out the RegEx expressions.&amp;#160;
It just so happens that sometimes too much of a good thing can be bad for your health.&amp;#160;
In our case, with over 10,000 RegEx.Match(…) calls in rapid succession, the RegEx
appeared to be our bottleneck.&amp;#160; This may or may not be the root cause of your
own performance problems.&amp;#160; The key takeaway from this blog entry should be that
you should &lt;strong&gt;NOT&lt;/strong&gt; guess when it comes to optimizing code paths.&amp;#160;
Instead, you should use the tools available at your disposal to find the bottleneck.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
Until next time.
&lt;/p&gt;</description>
      <comments>http://www.samuraiprogrammer.com/blog/CommentView,guid,38980ec9-3b6a-4667-b745-9456639486f5.aspx</comments>
      <category>Performance</category>
      <category>RegEx</category>
    </item>
    <item>
      <trackback:ping>http://www.samuraiprogrammer.com/blog/Trackback.aspx?guid=9d87b777-0aac-438c-9b47-91e479fea03a</trackback:ping>
      <pingback:server>http://www.samuraiprogrammer.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.samuraiprogrammer.com/blog/PermaLink,guid,9d87b777-0aac-438c-9b47-91e479fea03a.aspx</pingback:target>
      <dc:creator>Greg Varveris</dc:creator>
      <wfw:comment>http://www.samuraiprogrammer.com/blog/CommentView,guid,9d87b777-0aac-438c-9b47-91e479fea03a.aspx</wfw:comment>
      <wfw:commentRss>http://www.samuraiprogrammer.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=9d87b777-0aac-438c-9b47-91e479fea03a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.samuraiprogrammer.com/blog/content/binary/WindowsLiveWriter/Microoptimizationorjustgoodcodingpractic_DBB8/cheetah_2.jpg">
            <img style="border-right-width: 0px; margin: 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="cheetah" border="0" alt="cheetah" align="left" src="http://www.samuraiprogrammer.com/blog/content/binary/WindowsLiveWriter/Microoptimizationorjustgoodcodingpractic_DBB8/cheetah_thumb.jpg" width="244" height="184" />
          </a> This
is a common topic and I thought I’d write up some thoughts I have on it.  In-fact,
I was just working with a customer on improving their code reviews and what they should
be checking for and the question arose - “Should performance be targeted during a
code review?”  It’s an interesting question.  I’m a big fan of performance
testing early and often and not waiting until the end of a dev cycle but code reviews,
IMO, should focus on logic, maintainability and best practices.  I may be in
the minority and if you look around the web, you’ll see varying opinions on the topic.  <a href="http://msdn.microsoft.com/en-us/library/ff647802.aspx" target="_blank">For
example, one of the PAG articles states</a>:
</p>
        <blockquote>
          <p>
“Code reviews should be a regular part of your development process. Performance and
scalability code reviews focus on identifying coding techniques and design choices
that could lead to performance and scalability issues. The review goal is to identify
potential performance and scalability issues before the code is deployed. The cost
and effort of fixing performance and scalability flaws at development time is far
less than fixing them later in the product deployment cycle.
</p>
          <p>
Avoid performance code reviews too early in the coding phase because this can restrict
your design options. Also, bear in mind that that performance decisions often involve
tradeoffs. For example, it is easy to reduce maintainability and flexibility while
striving to optimize code.”
</p>
        </blockquote>
        <p>
As I mentioned above, I am a huge proponent of performance analysis and optimization
many times throughout a typical product development cycle.  I can say with a
fair amount of certainty that if you don’t build performance reviews into your project
plan at regular intervals, you will hit some problem (or multiple problems) in production
and have to refactor some code.  
</p>
        <p>
Circling back to the original question, though, are code reviews the place for performance
analysis?  Typically, I’d recommend using them to squash little bits of bad code
but maintainability and code-cleanliness should be first and foremost in your minds. 
That said, if you see a pattern that <strong>you know </strong>can be improved, by
all means bring it up.  What’s an example of that type of situation?  
</p>
        <p>
Let’s take a look at predicates, <a href="http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx" target="_blank">specifically
their usage in the Find method of a List&lt;T&gt;</a>.  If you’re not aware,
the Find() method performs a linear search through all of the items until it finds
the first match – then it returns.  This makes it a O(n) operation where “n”
is the number of items in the list.  Basically, this means that the more items
you have in the list, the longer a Find() operation can potentially take.  So,
if we slam about 10,000 elements into a list:
</p>
        <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
          <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
            <span style="color: #0000ff">private</span>
            <span style="color: #0000ff">static</span> List&lt;Data&gt;
LoadList()<br />
{<br />
List&lt;Data&gt; myList = <span style="color: #0000ff">new</span> List&lt;Data&gt;();<br /><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i
= 0; i &lt; 10000; i++)<br />
{<br />
myList.Add(<span style="color: #0000ff">new</span> Data() { Id = <span style="color: #006080">"Id"</span> +
i.ToString(), 
<br />
Value = <span style="color: #006080">"Value"</span> + i.ToString() });<br />
}<br /><br /><span style="color: #0000ff">return</span> myList;<br />
}</pre>
          <br />
        </div>
        <p>
Then, if someone wants to return the instance of the Data class that contains an Id
of say “Id10000”, they might write the following code:
</p>
        <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
          <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
            <span style="color: #0000ff">static</span> Data
Find1(List&lt;Data&gt; myList, <span style="color: #0000ff">string</span> idToFind)<br />
{<br />
Data data = myList.Find(s =&gt; 
<br />
s.Id.ToLower() == 
<br />
idToFind.ToLower());<br /><br /><span style="color: #0000ff">return</span> data;<br />
}</pre>
          <br />
        </div>
        <p>
Now, keep in mind that the predicate is executed for each element in the List&lt;T&gt;
until it finds the instance you care about.  With that in mind, we would probably
want to refactor out the “idToFind.ToLower()” above the predicate since that value
isn’t changing.  So, you might end-up with something like this:
</p>
        <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
          <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
            <span style="color: #0000ff">static</span> Data
Find2(List&lt;Data&gt; myList, <span style="color: #0000ff">string</span> idToFind)<br />
{<br /><br />
idToFind = idToFind.ToLower();<br /><br />
Data data = myList.Find(s =&gt; 
<br />
s.Id.ToLower() == 
<br />
idToFind);<br /><br /><span style="color: #0000ff">return</span> data;<br />
}</pre>
          <br />
        </div>
        <p>
Another route you may want to go is just to use the string.Equals(…) method to perform
the comparison.  That would look like:
</p>
        <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
          <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
            <span style="color: #0000ff">static</span> Data
Find3(List&lt;Data&gt; myList, <span style="color: #0000ff">string</span> idToFind)<br />
{<br /><br />
Data data = myList.Find(s =&gt; 
<br /><span style="color: #0000ff">string</span>.Equals(<br />
s.Id, 
<br />
idToFind, 
<br />
StringComparison.<br />
InvariantCultureIgnoreCase)<br />
);<br /><br /><span style="color: #0000ff">return</span> data;<br /><br />
}</pre>
          <br />
        </div>
        <p>
Fact is, the last method <strong>IS</strong> the fastest way to perform the operation. 
I can say that without even needing to run it through a profiler.  But if you
don’t believe me…   <span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 14pt arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"></span></p>
        <p>
          <table style="border-bottom-style: none; border-right-style: none; border-collapse: collapse; border-top-style: none; font-size: 14pt; border-left-style: none" border="0" cellspacing="0" cellpadding="0">
            <tbody>
              <tr>
                <td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" bgcolor="#000000" height="29" width="406">
                  <div style="text-indent: 0pt">
                    <span style="font-family: calibri; color: rgb(255,255,255); font-size: 11pt">
                      <b>Function
Name</b>
                    </span>
                  </div>
                </td>
                <td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" bgcolor="#000000" width="175">
                  <div style="text-indent: 0pt">
                    <span style="font-family: calibri; color: rgb(255,255,255); font-size: 11pt">
                      <b>Elapsed</b>
                    </span>
                  </div>
                  <div style="text-indent: 0pt">
                    <span style="font-family: calibri; color: rgb(255,255,255); font-size: 11pt">
                      <b>Inclusive
Time</b>
                    </span>
                  </div>
                </td>
              </tr>
              <tr>
                <td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" height="14" width="406">
                  <div style="text-indent: 0pt">
                    <span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt">...Find1(System.Collections.Generic.List`1&lt;....Data&gt;,string)</span>
                  </div>
                </td>
                <td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" width="175">
                  <div style="text-indent: 0pt" align="right">
                    <span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt">6.34</span>
                  </div>
                </td>
              </tr>
              <tr>
                <td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" height="14" width="406">
                  <div style="text-indent: 0pt">
                    <span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt">...Find2(System.Collections.Generic.List`1&lt;....Data&gt;,string)</span>
                  </div>
                </td>
                <td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" width="175">
                  <div style="text-indent: 0pt" align="right">
                    <span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt">4.47</span>
                  </div>
                </td>
              </tr>
              <tr>
                <td style="border-bottom: rgb(51,51,153) 1px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" height="14" width="406">
                  <div style="text-indent: 0pt">
                    <span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt">...Find3(System.Collections.Generic.List`1&lt;....Data&gt;,string)</span>
                  </div>
                </td>
                <td style="border-bottom: rgb(51,51,153) 1px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" width="175">
                  <div style="text-indent: 0pt" align="right">
                    <span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt">3.65</span>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </p>
        <div>That’s something I might put into the category of a micro-optimization AND just
good coding practice.  But is this something that should be caught during a code
review?  I’d say “yes” because logically it all makes sense and none of the solutions
would really hurt maintainability or readability of the code.
</div>
        <div> 
</div>
        <div>So, I’d tag this as a good coding practice.  Other thoughts on the topic?
</div>
        <div> 
</div>
        <div>Enjoy!
</div>
        <div>
        </div>
      </body>
      <title>Micro optimization or just good coding practice?</title>
      <guid isPermaLink="false">http://www.samuraiprogrammer.com/blog/PermaLink,guid,9d87b777-0aac-438c-9b47-91e479fea03a.aspx</guid>
      <link>http://www.samuraiprogrammer.com/blog/2010/08/21/MicroOptimizationOrJustGoodCodingPractice.aspx</link>
      <pubDate>Sat, 21 Aug 2010 04:36:27 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.samuraiprogrammer.com/blog/content/binary/WindowsLiveWriter/Microoptimizationorjustgoodcodingpractic_DBB8/cheetah_2.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="cheetah" border="0" alt="cheetah" align="left" src="http://www.samuraiprogrammer.com/blog/content/binary/WindowsLiveWriter/Microoptimizationorjustgoodcodingpractic_DBB8/cheetah_thumb.jpg" width="244" height="184" /&gt;&lt;/a&gt; This
is a common topic and I thought I’d write up some thoughts I have on it.&amp;#160; In-fact,
I was just working with a customer on improving their code reviews and what they should
be checking for and the question arose - “Should performance be targeted during a
code review?”&amp;#160; It’s an interesting question.&amp;#160; I’m a big fan of performance
testing early and often and not waiting until the end of a dev cycle but code reviews,
IMO, should focus on logic, maintainability and best practices.&amp;#160; I may be in
the minority and if you look around the web, you’ll see varying opinions on the topic.&amp;#160; &lt;a href="http://msdn.microsoft.com/en-us/library/ff647802.aspx" target="_blank"&gt;For
example, one of the PAG articles states&lt;/a&gt;:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
“Code reviews should be a regular part of your development process. Performance and
scalability code reviews focus on identifying coding techniques and design choices
that could lead to performance and scalability issues. The review goal is to identify
potential performance and scalability issues before the code is deployed. The cost
and effort of fixing performance and scalability flaws at development time is far
less than fixing them later in the product deployment cycle.
&lt;/p&gt;
&lt;p&gt;
Avoid performance code reviews too early in the coding phase because this can restrict
your design options. Also, bear in mind that that performance decisions often involve
tradeoffs. For example, it is easy to reduce maintainability and flexibility while
striving to optimize code.”
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
As I mentioned above, I am a huge proponent of performance analysis and optimization
many times throughout a typical product development cycle.&amp;#160; I can say with a
fair amount of certainty that if you don’t build performance reviews into your project
plan at regular intervals, you will hit some problem (or multiple problems) in production
and have to refactor some code.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
Circling back to the original question, though, are code reviews the place for performance
analysis?&amp;#160; Typically, I’d recommend using them to squash little bits of bad code
but maintainability and code-cleanliness should be first and foremost in your minds.&amp;#160;
That said, if you see a pattern that &lt;strong&gt;you know &lt;/strong&gt;can be improved, by
all means bring it up.&amp;#160; What’s an example of that type of situation?&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
Let’s take a look at predicates, &lt;a href="http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx" target="_blank"&gt;specifically
their usage in the Find method of a List&amp;lt;T&amp;gt;&lt;/a&gt;.&amp;#160; If you’re not aware,
the Find() method performs a linear search through all of the items until it finds
the first match – then it returns.&amp;#160; This makes it a O(n) operation where “n”
is the number of items in the list.&amp;#160; Basically, this means that the more items
you have in the list, the longer a Find() operation can potentially take.&amp;#160; So,
if we slam about 10,000 elements into a list:
&lt;/p&gt;
&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; List&amp;lt;Data&amp;gt;
LoadList()&lt;br /&gt;
{&lt;br /&gt;
List&amp;lt;Data&amp;gt; myList = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;Data&amp;gt;();&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i
= 0; i &amp;lt; 10000; i++)&lt;br /&gt;
{&lt;br /&gt;
myList.Add(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Data() { Id = &lt;span style="color: #006080"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt; +
i.ToString(), 
&lt;br /&gt;
Value = &lt;span style="color: #006080"&gt;&amp;quot;Value&amp;quot;&lt;/span&gt; + i.ToString() });&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; myList;&lt;br /&gt;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt;
Then, if someone wants to return the instance of the Data class that contains an Id
of say “Id10000”, they might write the following code:
&lt;/p&gt;
&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Data
Find1(List&amp;lt;Data&amp;gt; myList, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; idToFind)&lt;br /&gt;
{&lt;br /&gt;
Data data = myList.Find(s =&amp;gt; 
&lt;br /&gt;
s.Id.ToLower() == 
&lt;br /&gt;
idToFind.ToLower());&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; data;&lt;br /&gt;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt;
Now, keep in mind that the predicate is executed for each element in the List&amp;lt;T&amp;gt;
until it finds the instance you care about.&amp;#160; With that in mind, we would probably
want to refactor out the “idToFind.ToLower()” above the predicate since that value
isn’t changing.&amp;#160; So, you might end-up with something like this:
&lt;/p&gt;
&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Data
Find2(List&amp;lt;Data&amp;gt; myList, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; idToFind)&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
idToFind = idToFind.ToLower();&lt;br /&gt;
&lt;br /&gt;
Data data = myList.Find(s =&amp;gt; 
&lt;br /&gt;
s.Id.ToLower() == 
&lt;br /&gt;
idToFind);&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; data;&lt;br /&gt;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt;
Another route you may want to go is just to use the string.Equals(…) method to perform
the comparison.&amp;#160; That would look like:
&lt;/p&gt;
&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Data
Find3(List&amp;lt;Data&amp;gt; myList, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; idToFind)&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
Data data = myList.Find(s =&amp;gt; 
&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Equals(&lt;br /&gt;
s.Id, 
&lt;br /&gt;
idToFind, 
&lt;br /&gt;
StringComparison.&lt;br /&gt;
InvariantCultureIgnoreCase)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; data;&lt;br /&gt;
&lt;br /&gt;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt;
Fact is, the last method &lt;strong&gt;IS&lt;/strong&gt; the fastest way to perform the operation.&amp;#160;
I can say that without even needing to run it through a profiler.&amp;#160; But if you
don’t believe me…&amp;#160;&amp;#160; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: 14pt arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;table style="border-bottom-style: none; border-right-style: none; border-collapse: collapse; border-top-style: none; font-size: 14pt; border-left-style: none" border="0" cellspacing="0" cellpadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" bgcolor="#000000" height="29" width="406"&gt;
&lt;div style="text-indent: 0pt"&gt;&lt;span style="font-family: calibri; color: rgb(255,255,255); font-size: 11pt"&gt;&lt;b&gt;Function
Name&lt;/b&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" bgcolor="#000000" width="175"&gt;
&lt;div style="text-indent: 0pt"&gt;&lt;span style="font-family: calibri; color: rgb(255,255,255); font-size: 11pt"&gt;&lt;b&gt;Elapsed&lt;/b&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style="text-indent: 0pt"&gt;&lt;span style="font-family: calibri; color: rgb(255,255,255); font-size: 11pt"&gt;&lt;b&gt;Inclusive
Time&lt;/b&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" height="14" width="406"&gt;
&lt;div style="text-indent: 0pt"&gt;&lt;span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt"&gt;...Find1(System.Collections.Generic.List`1&amp;lt;....Data&amp;gt;,string)&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" width="175"&gt;
&lt;div style="text-indent: 0pt" align="right"&gt;&lt;span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt"&gt;6.34&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" height="14" width="406"&gt;
&lt;div style="text-indent: 0pt"&gt;&lt;span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt"&gt;...Find2(System.Collections.Generic.List`1&amp;lt;....Data&amp;gt;,string)&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td style="border-bottom: rgb(0,0,0) 0px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" width="175"&gt;
&lt;div style="text-indent: 0pt" align="right"&gt;&lt;span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt"&gt;4.47&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="border-bottom: rgb(51,51,153) 1px solid; border-left: rgb(51,51,153) 1px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(0,0,0) 0px solid" height="14" width="406"&gt;
&lt;div style="text-indent: 0pt"&gt;&lt;span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt"&gt;...Find3(System.Collections.Generic.List`1&amp;lt;....Data&amp;gt;,string)&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td style="border-bottom: rgb(51,51,153) 1px solid; border-left: rgb(0,0,0) 0px solid; border-top: rgb(51,51,153) 1px solid; border-right: rgb(51,51,153) 1px solid" width="175"&gt;
&lt;div style="text-indent: 0pt" align="right"&gt;&lt;span style="font-family: calibri; color: rgb(0,0,0); font-size: 11pt"&gt;3.65&lt;/span&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;div&gt;That’s something I might put into the category of a micro-optimization AND just
good coding practice.&amp;#160; But is this something that should be caught during a code
review?&amp;#160; I’d say “yes” because logically it all makes sense and none of the solutions
would really hurt maintainability or readability of the code.
&lt;/div&gt;
&lt;div&gt;&amp;#160;
&lt;/div&gt;
&lt;div&gt;So, I’d tag this as a good coding practice.&amp;#160; Other thoughts on the topic?
&lt;/div&gt;
&lt;div&gt;&amp;#160;
&lt;/div&gt;
&lt;div&gt;Enjoy!
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;</description>
      <comments>http://www.samuraiprogrammer.com/blog/CommentView,guid,9d87b777-0aac-438c-9b47-91e479fea03a.aspx</comments>
      <category>.NET</category>
      <category>Best Practice</category>
      <category>C#</category>
      <category>Code Reviews</category>
      <category>Development</category>
      <category>Performance</category>
    </item>
  </channel>
</rss>