<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jim&#039;s Random Notes</title>
	<atom:link href="http://blog.mischel.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mischel.com</link>
	<description></description>
	<lastBuildDate>Sat, 18 May 2013 02:56:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>A couple of LINQ quickies</title>
		<link>http://blog.mischel.com/2013/05/17/a-couple-of-linq-quickies/</link>
		<comments>http://blog.mischel.com/2013/05/17/a-couple-of-linq-quickies/#comments</comments>
		<pubDate>Sat, 18 May 2013 02:56:28 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1852</guid>
		<description><![CDATA[<p>I&#8217;ve recently had occasion to write LINQ versions of two functions I&#8217;d written some time back. It proved instructive.</p> Random numbers that sum to N <p>Doing some testing, I needed an array of 100 random floating point numbers that sum to 1.0. That is, given an array a, (a[0] + a[1] + a[2] ... + <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/05/17/a-couple-of-linq-quickies/">A couple of LINQ quickies</a></span>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve recently had occasion to write LINQ versions of two functions I&#8217;d written some time back. It proved instructive.</p>
<h3>Random numbers that sum to N</h3>
<p>Doing some testing, I needed an array of 100 random floating point numbers that sum to 1.0. That is, given an array <code>a</code>, <code>(a[0] + a[1] + a[2] ... + a[99]) = 1</code>. The easiest way I know of to do that is to generate an array of 100 random values, sum them, and then divide each number by the sum.</p>
<pre><code>    const int ArraySize = 100;
    Random rnd = new Random();
    double[] a = new double[ArraySize];
    double sum = 0;
    // generate the values
    for (int i = 0; i &lt; a.Length; ++i)
    {
        a[i] = rnd.NextDouble();
        sum += a[i];
    }

    // now divide each item by the sum
    for (int i = 0; i &lt; a.Length; ++i)
    {
        a[i] /= sum;
    }
</code></pre>
<p>The math behind this is pretty simple. What you end up with is <code>(a[0]/sum + a[1]/sum + a[2]/sum ... + a[99]/sum)</code>, which works out to <code>sum/sum</code>, which is 1.</p>
<p>Somebody asked me for a LINQ version.</p>
<pre><code>    var temp = (from d in Enumerable.Range(0, ArraySize)
                select rnd.NextDouble()).ToArray();
    var sum = temp.Sum();
    var a = temp.Select(d =&gt; d/sum).ToArray();</code></pre>
<p>An obvious extension to this is generating an array of N values that sum to some arbitrary value. The first part of the algorithm is the same: generate N numbers, and sum them. Then, rather than dividing each number by the sum, divide by <code>(sum/desired_value)</code>. Or, you can multiply by <code>(desired_value/sum)</code>. So the final LINQ expression becomes:</p>
<pre><code>    var a = temp.Select(d =&gt; d*(desiredValue/sum)).ToArray();</code></pre>
<p>And don&#8217;t worry about the division in the inner loop there. The compiler (either the C# compiler, or the JIT compiler) will move it out of the loop.</p>
<h3>Finding palindromes</h3>
<p>At a previous job, one part of the interview process for entry-level programmers was having them solve some simple programming puzzles on the white board. I called it <a href="http://blog.mischel.com/2011/11/05/the-white-board-inquisition/">The White Board Inquisition</a>. A common exercise was to write a function that determines if an input string is a palindrome. We&#8217;d keep it simple and have them look for an exact match. So &#8220;amanaplanacanalpanama&#8221; would be a palindrome, but &#8220;A man, a plan, a canal, Panama!&#8221; would not be. Stripping out the non-alphabetic characters and doing case-insensitive matching is a simple refinement, but we wanted to see how the candidate approached the primary problem.</p>
<p>The simplest way, of course, is to reverse the string and do a comparison, but we wouldn&#8217;t allow them to call a reverse method. If they wanted to reverse the string, they&#8217;d have to do it themselves. Many did. Another common approach is to push the characters onto a stack and then pop them, comparing the popped values with the string characters, in order.</p>
<p>Few candidates tripped to the in-place method of comparing the characters starting from the outside and moving in, like this.</p>
<pre><code>    private bool IsPalindrome(string input)
    {
        int i = 0;
        int j = input.Length - 1;
        while (i &lt; j)
        {
            if (input[i++] != input[j--])
                return false;
        }
        return true;
    }</code></pre>
<p>Somebody recently asked me for a LINQ version of a palindrome finder. The obvious solution is to employ the <code>Reverse</code> extension method and then compare the sequences. But it&#8217;s easy enough to duplicate the above algorithm.</p>
<pre><code>    private bool IsPalindrome(string input)
    {
        return
            Enumerable.Range(0, input.Length / 2)
                        .Select(i =&gt; input[i] == input[input.Length - i - 1])
                        .All(b =&gt; b);
    }</code></pre>
<p>A common misconception is that the LINQ code will do those three operations in sequence: generate a list of indexes, then generate a list of results, and then compare the results. That most definitely is not how the generated code works. If you enable runtime library source stepping, you&#8217;ll see that the sequence of operations follows the looping construct very closely. The LINQ solution is slower, of course, because it&#8217;s working with iterators and such, but it isn&#8217;t <em>stupid</em>.</p>
<p>The more I work with LINQ, the more impressed I am by what I can do with it. It&#8217;s not always the fastest executing solution, but for most of what I do that doesn&#8217;t matter. I find that the expressiveness leads to much shorter, more easily understood, and more reliable code than writing large looping constructs. I imagine that interviewers these days expect candidates to demonstrate a thorough understanding of LINQ. I know that I certainly would.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/05/17/a-couple-of-linq-quickies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Null parameters in extension methods</title>
		<link>http://blog.mischel.com/2013/05/16/null-parameters-in-extension-methods/</link>
		<comments>http://blog.mischel.com/2013/05/16/null-parameters-in-extension-methods/#comments</comments>
		<pubDate>Thu, 16 May 2013 19:16:11 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1848</guid>
		<description><![CDATA[<p>There is the question of how to handle null parameters in extension methods. Consider this extension method that returns the number of digits in a string.</p> public static class MyExtensions { public static int CountDigits(this string input) { var count = 0; foreach (var c in input) { if (char.IsDigit(c)) { ++count; } } return <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/05/16/null-parameters-in-extension-methods/">Null parameters in extension methods</a></span>]]></description>
				<content:encoded><![CDATA[<p>There is the question of how to handle <code>null</code> parameters in extension methods. Consider this extension method that returns the number of digits in a string.</p>
<pre><code>    public static class MyExtensions
    {
        public static int CountDigits(this string input)
        {
            var count = 0;
            foreach (var c in input)
            {
                if (char.IsDigit(c))
                {
                    ++count;
                }
            }
            return count;
        }
    }
</code></pre>
<p>Understand, this is just an example. I realize that I can replace all that code with a single function call, and in fact I wouldn&#8217;t actually write such a simple extension method to replace that one line of code in my program. Bear with me.</p>
<p>After running the code below, <code>result</code> will be equal to 10.</p>
<pre><code>    string foo = "(555)123-4567";
    var result = foo.CountDigits();  // returns 10</code></pre>
<p>So what should happen if I call that method when <code>foo == null</code>? Let&#8217;s see what the Framework does when I call a <code>string</code> method on a <code>null</code> reference.</p>
<pre><code>    string foo = null;
    string lowerFoo = foo.ToLower();</code></pre>
<p>Not surprisingly, that throws <code>NullReferenceException</code>. And, not surprisingly, the <code>CountDigits</code> method also throws <code>NullReferenceException</code> when <code>foo == null</code>. The difference, though, is important. If you look at the exception detail you&#8217;ll find that the call to <code>foo.ToLower</code> reports the exception as being thrown <em>on that line</em>. The stack trace for the call to <code>CountDigits</code> looks like this.</p>
<pre><code>System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=sotesto
  StackTrace:
       at Test.MyExtensions.CountDigits(String input) in c:\Dev\Jim\Testo2012\sotesto\Program.cs:line 37
       at Test.Program.DoStuff() in c:\Dev\Jim\Testo2012\sotesto\Program.cs:line 28</code></pre>
<p>The exception is thrown on the line containing the <code>foreach</code> statement.</p>
<p>If you understand how extension methods work, this shouldn&#8217;t be surprising. After all, the code <code>foo.CountDigits();</code> is really just shorthand for <code>MyExtensions.CountDigits(foo);</code>.</p>
<p>The exception is saying that the error exists in the extension method when in reality the error exists at the call site. This is an important distinction. Imagine that you&#8217;re calling an extension method that exists in a library for which you have no source. If that extension method throws <code>NullReferenceException</code>, you will likely assume that the error is in the extension method. And nothing in the stack trace will tell you otherwise. There is nothing to indicate that it threw that exception because you passed a <code>null</code> parameter.</p>
<p>That is incorrect behavior.</p>
<p>Don&#8217;t believe me? Let&#8217;s see what happens if I rewrite that extension method.</p>
<pre><code>    public static class MyExtensions
    {
        public static int CountDigits(this string input)
        {
            return input.Count(char.IsDigit);
        }
    }</code></pre>
<p>Yes, that duplicates the functionality of the previous version. But if you call it with a <code>null</code> parameter, the result is quite different.</p>
<pre><code>System.ArgumentNullException was unhandled
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: source
  Source=System.Core
  ParamName=source
  StackTrace:
       at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source, Func`2 predicate)
       at Test.MyExtensions.CountDigits(String input) in c:\Dev\Jim\Testo2012\sotesto\Program.cs:line 36
       at Test.Program.DoStuff() in c:\Dev\Jim\Testo2012\sotesto\Program.cs:line 28</code></pre>
<p>Here we see that the <code>Enumerable.Count</code> extension method threw <code>ArgumentNullException</code>, and actually <em>told me why</em>: the <code>source</code> parameter cannot be <code>null</code>. But, again, it looks like the error is due to the <code>CountDigits</code> extension method passing a <code>null</code> argument.</p>
<p>The answer to the question of what to do with <code>null</code> parameters to extension methods is simple: check them and throw <code>ArgumentNullException</code>.</p>
<pre><code>    public static int CountDigits(this string input)
    {
        if (input == null)
        {
            throw new ArgumentNullException("input", "Value cannot be null.");
        }
        return input.Count(char.IsDigit);
    }</code></pre>
<p>Now you know <em>exactly</em> why the error occured: the code that called <code>CountDigits</code> passed a <code>null</code> parameter.</p>
<p>The above is how the Framework methods handle null parameters. It&#8217;s considered a best practice. More importantly, this is the only way to know for sure where the real error lies.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/05/16/null-parameters-in-extension-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Confusing traffic signal</title>
		<link>http://blog.mischel.com/2013/05/15/confusing-traffic-signal/</link>
		<comments>http://blog.mischel.com/2013/05/15/confusing-traffic-signal/#comments</comments>
		<pubDate>Wed, 15 May 2013 12:09:28 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Odds 'n Ends]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1845</guid>
		<description><![CDATA[<p>The two intersections near my office have traffic lights that are unlike any others I&#8217;ve seen, and I fear that they are confusing to drivers and therefore dangerous.</p> <p>On the north and south sides of the intersection there are three lanes: a left turn lane, a through lane, and a right turn lane. On the <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/05/15/confusing-traffic-signal/">Confusing traffic signal</a></span>]]></description>
				<content:encoded><![CDATA[<p>The two intersections near my office have traffic lights that are unlike any others I&#8217;ve seen, and I fear that they are confusing to drivers and therefore dangerous.</p>
<p>On the north and south sides of the intersection there are three lanes: a left turn lane, a through lane, and a right turn lane. On the east and west sides, there are four lanes (two through lanes). All four sides of the intersection have the normal red/yellow/green through lights and a left turn light.</p>
<p>A traditional traffic light is green, turns yellow for a short period, and then turns red. They have been this way for decades&#8211;certainly for the 40+ years I&#8217;ve been aware of the way that traffic lights work. Left turn lights work much the same way. The left turn arrow might work on a different schedule than the through lights, but it traditionally follows the green to yellow to red pattern. Or, it might have only green and off, meaning that you have to yield to oncoming traffic when the light is off, but the oncoming traffic has a red light when the left turn light is green.</p>
<p>The left turn lights at this intersection are different. And confusing. They <em>blink yellow</em> to mean, &#8220;You may turn after yielding to oncoming traffic.&#8221; After the blinking yellow period, they turn green. So drivers have to know that <em>solid</em> yellow means &#8220;red is soon to come,&#8221; but <em>blinking</em> yellow means, &#8220;green is soon to come.&#8221;</p>
<p>It doesn&#8217;t help, of course, that years of experience have taught drivers that in practice a yellow left turn signal means, &#8220;Hurry up through the intersection before the light turns red.&#8221;</p>
<p>Every day I see drivers not fully understanding what that blinking yellow means. Some, when they see the blinking yellow, dash through the intersection heedless of oncoming traffic. I can only conclude that they don&#8217;t understand that the rules have changed and a light might start blinking yellow. Other drivers see the blinking yellow and hesitate when it&#8217;s clear that there is no oncoming traffic and they&#8217;re fully within their rights to proceed.</p>
<p>I realize that blinking yellow means &#8220;proceed with caution.&#8221; But traditionally that&#8217;s done with <em>all</em> of the lanes at an intersection (going the same way, of course). But drivers aren&#8217;t accustomed to seeing blinking yellow on one light, and solid red or green on the other lights. And they&#8217;re <em>certainly</em> not prepared for a blinking yellow light to suddenly go green.</p>
<p>I&#8217;ll grant that I&#8217;ve only seen two crashes (after the fact&#8211;I wasn&#8217;t present for the actual impact) at these intersections in the three months I&#8217;ve been here. But I&#8217;ve seen too many close calls to believe that this unorthodox use of the signal lights is a good idea. I can only hope that those in charge of the traffic lights will see this dangerous situation for what it is and reprogram the lights back to something more reasonable.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/05/15/confusing-traffic-signal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular expressions, optimization, and practicality</title>
		<link>http://blog.mischel.com/2013/05/13/regular-expressions-optimization-and-practicality/</link>
		<comments>http://blog.mischel.com/2013/05/13/regular-expressions-optimization-and-practicality/#comments</comments>
		<pubDate>Tue, 14 May 2013 03:31:28 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1843</guid>
		<description><![CDATA[<p>Suppose you wanted a function that would replace multiple instances of a character with a single instance. So the string &#8220;+++This+is+++++a++test++&#8221; would become &#8220;+This+is+a+test+&#8221;. The easiest way to do it in C# is to employ regular expressions.</p> string testo = "+++This+is+++++a++test++"; string result = Regex.Replace(testo, @"\++", "+"); <p>The leading slash is there because '+' is <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/05/13/regular-expressions-optimization-and-practicality/">Regular expressions, optimization, and practicality</a></span>]]></description>
				<content:encoded><![CDATA[<p>Suppose you wanted a function that would replace multiple instances of a character with a single instance. So the string &#8220;+++This+is+++++a++test++&#8221; would become &#8220;+This+is+a+test+&#8221;. The easiest way to do it in C# is to employ regular expressions.</p>
<pre><code>    string testo = "+++This+is+++++a++test++";
    string result = Regex.Replace(testo, @"\++", "+");</code></pre>
<p>The leading slash is there because <code>'+'</code> is a special character in regular expressions. The slash &#8220;escapes&#8221; the character so that it will be treated as a literal.</p>
<p>By the way, you can&#8217;t do this reliably with <code>String.Replace</code>. If you wrote:</p>
<pre><code>    string result = testo.Replace("++", "+");</code></pre>
<p>You would end up with &#8220;++This+is+++a+test+&#8221;. <code>String.Replace</code> makes a replacement and moves on, so it will miss when the replacement string plus the following characters is equal to the search string.</p>
<p>Curious, I thought I&#8217;d write my own function to remove duplicate characters and see how it fares against the regular expression. The result is this <code>RemoveDupes</code> method.</p>
<pre><code>    public static string RemoveDupes(string text, char dupeChar)
    {
        if (string.IsNullOrEmpty(text))
        {
            return text;
        }

        var result = new StringBuilder(text.Length);
        bool dupe = false;
        for (int x = 0; x < text.Length; ++x)
        {
            char c = text[x];
            if (c == dupeChar)
            {
                if (!dupe)
                {
                    result.Append(c);
                    dupe = true;
                }
            }
            else
            {
                result.Append(c);
                dupe = false;
            }
        }
        return result.ToString();
    }</code></pre>
<p>That's the most straightforward way I could think of to write it, so I wouldn't be surprised if there are some optimizations that would make it faster.</p>
<p>I created the regular expression like this:</p>
<pre><code>    var re = new Regex(@"\++", RegexOptions.Compiled);</code></pre>
<p>And in the timed loop, called it with:</p>
<pre><code>    result = re.Replace(testString, "+");</code></pre>
<p>That should make the regular expression as fast as it can be.</p>
<p>Performance testing was a little bit surprising. The simple <code>RemoveDupes</code> method was 5 to 8 times as fast as the regular expression version, depending on the length of the strings and the percentage of repeated <code>'+'</code> characters in the string.</p>
<p>That the custom code was faster didn't particularly surprise me; I've proven many times that a custom parser can outperform regular expressions. Two or three times as fast is what I expected. Up to eight times as fast came as a surprise.</p>
<p>One million iterations with a string 100 bytes long took the regular expression version 4,767 milliseconds, or 0.004768 ms per call. The same string run through the <code>RemoveDupes</code> method took 793 ms, or 0.000793 ms per call. The custom method is six times as fast, but the absolute difference for those million iterations is only four seconds.</p>
<p>Granted, the absolute differences get larger as the number of iterations or the length of the string increases. The timings (and the differences) increase linearly. If it takes <code>RemoveDupes</code> some <code>N</code> seconds to complete, it will take the regular expression approximately <code>6*N</code> seconds.</p>
<p>But does it really matter? Most likely not. In most cases, sanitizing the input data takes up a very small percentage of the total CPU time. Optimizing it just doesn't yield meaningful performance gains. Unless removing duplicated characters from strings is the most time consuming part of the program, this huge boost in performance would mean nothing.</p>
<p>Would you really notice a forty second difference in the running time of a program that takes an hour?</p>
<p>The other practical matter has to do with time to implement. The regular expression code already works. It took some (small) amount of time to write and test my <code>RemoveDupes</code> method, and the result is a very narrowly-targeted solution: it can remove duplicated characters from a string. Even the seemingly simple modification of letting it de-dupe 2-character strings would be a much more involved job, especially when you take into account the weird edge cases inherent in Unicode. Writing and debugging <em>that</em> would be difficult.</p>
<p>I think I'll stick with regular expressions for this kind of job, even though they're slower than the custom parser. Unless I <em>really</em> need the speed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/05/13/regular-expressions-optimization-and-practicality/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Are jagged arrays faster than rectangular arrays?</title>
		<link>http://blog.mischel.com/2013/05/08/are-jagged-arrays-faster-than-rectangular-arrays/</link>
		<comments>http://blog.mischel.com/2013/05/08/are-jagged-arrays-faster-than-rectangular-arrays/#comments</comments>
		<pubDate>Thu, 09 May 2013 03:49:35 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1838</guid>
		<description><![CDATA[<p>In reading about C# performance optimization, I often encounter the conventional wisdom that jagged arrays are faster than rectangular arrays. That is, given these two arrays,</p> byte[,] rectArray = new byte[NumRows, NumCols]; byte[][] jaggedArray = new byte[NumRows][]; <p>The assertion is that accessing jaggedArray will be faster.</p> <p>I&#8217;ve rarely seen any kind of benchmark to back <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/05/08/are-jagged-arrays-faster-than-rectangular-arrays/">Are jagged arrays faster than rectangular arrays?</a></span>]]></description>
				<content:encoded><![CDATA[<p>In reading about C# performance optimization, I often encounter the conventional wisdom that jagged arrays are faster than rectangular arrays. That is, given these two arrays,</p>
<pre><code>    byte[,] rectArray = new byte[NumRows, NumCols];
    byte[][] jaggedArray = new byte[NumRows][];</code></pre>
<p>The assertion is that accessing <code>jaggedArray</code> will be faster.</p>
<p>I&#8217;ve rarely seen any kind of benchmark to back up that assertion, and of the benchmarks I <em>have</em> seen many are fundamentally flawed. I&#8217;m exploring a project that will make heavy use of arrays, so I thought I&#8217;d look into the relative performance of both array types.</p>
<p>Before I do that, though, I should mention that often you don&#8217;t have a choice of what type of array to use. If your job is to process a two-dimensional array of bytes that somebody passes to your program, you&#8217;re most likely better off processing it in the format that it was given to you. Converting the data to your preferred &#8220;faster&#8221; format and converting it back to the native format is going to take a lot of time: probably more time than you&#8217;ll save with your &#8220;faster&#8221; processing code.</p>
<p>Even if you&#8217;re writing new code, the best array representation is almost always the one that best fits your model. A rectangular array (i.e. <code>byte[,]</code>) usually leads to simpler code, but jagged arrays (i.e. <code>byte[][]</code>) <em>can</em> be better in many situations. Jagged arrays are almost always better when you need an &#8220;array of arrays.&#8221; That is, when the second dimension is variable.</p>
<p>That said, let&#8217;s benchmark some simple operations with the two different types of arrays. The platform I&#8217;m using is:</p>
<ul>
<li>Windows Server 2008 R2</li>
<li>Visual Studio 2012, Update 1</li>
<li>Intel Xeon X5350 @ 2.67 GHz</li>
<li>Program compiled in Release mode with &#8220;Any CPU&#8221;, targeting .NET 4.5</li>
<li>Run in the 64-bit runtime with debugger <em>detached</em></li>
</ul>
<p>I have a simple test harness that runs the test method once to eliminate JIT effects (the time taken for the JIT compiler to compile the method), and then starts a <code>Stopwatch</code> and executes the test method the number of times requested. The reported time is the average of all those runs. Here&#8217;s the test harness:</p>
<pre><code>    private void Test(Action a, string text, int numIterations)
    {
        Console.Write("{0} ... ", text);
        // do it once to eliminate JIT effects
        a();

        // Now time the iterations.
        var sw = Stopwatch.StartNew();
        for (var i = 0; i &lt; numIterations; ++i)
        {
            a();
        }
        sw.Stop();
        Console.WriteLine("{0:N0} ms", sw.ElapsedMilliseconds/numIterations);
    }</code></pre>
<p>In these tests, I&#8217;m using arrays that have 2,736 rows and 10,944 columns. This corresponds to the number of pixels in a 10 megapixel image, with each pixel taking up 3 bytes. The timings I report below are the average of running each method 1,000 times.</p>
<p>The first test is just a simple traversal of the array that sums all the values. I first wrote it like this:</p>
<pre><code>    private int SimpleSum;

    private void SimpleSumRect(byte[,] array, int rows, int cols)
    {
        SimpleSum = 0;
        for (var i = 0; i &lt; rows; ++i)
        {
            for (var j = 0; j &lt; cols; ++j)
            {
                SimpleSum += array[i, j];
            }
        }
    }

    private void SimpleSumJagged(byte[][] array, int rows, int cols)
    {
        SimpleSum = 0;
        for (var i = 0; i &lt; rows; ++i)
        {
            for (var j = 0; j &lt; cols; ++j)
            {
                SimpleSum += array[i][j];
            }
        }
    }</code></pre>
<p>After initializing the arrays, I run the tests like this:</p>
<pre><code>    Test(() =&gt; SimpleSumRect(rectArray, ArrayRows, ArrayCols), "Simple Sum rect", NumIterations);
    Test(() =&gt; SimpleSumJagged(jaggedArray, ArrayRows, ArrayCols), "Simple Sum jagged", NumIterations);</code></pre>
<p>The result was rather interesting:</p>
<pre><code>    Simple Sum rect ... 70 ms
    Simple sum jagged ... 70 ms</code></pre>
<p>No difference? I wasn&#8217;t really surprised by that, and almost dismissed the conventional wisdom as so much myth. But I was also curious because I have great respect for some of the people making the &#8220;jagged is faster&#8221; claim. So I modified the test slightly. Rather than summing to a variable at class scope, I made it a local variable, like this:</p>
<pre><code>    private int SimpleSumRect(byte[,] array, int rows, int cols)
    {
        int SimpleSum = 0;
        for (var i = 0; i &lt; rows; ++i)
        {
            for (var j = 0; j &lt; cols; ++j)
            {
                SimpleSum += array[i, j];
            }
        }
        return SimpleSum;
    }</code></pre>
<p><em>That</em> result was surprising:</p>
<pre><code>    Simple Sum rect ... 39 ms
    Simple sum jagged ... 24 ms</code></pre>
<p>I had no idea that accessing a class-scope variable was so much more expensive than accessing a local variable. I&#8217;d have to look at the generated code (both IL and the JIT-generated machine code) to say for sure what&#8217;s going on here, but apparently accessing that class-scope variable prevents the compiler from making some optimizations. Making the sum a local variable (that the compiler probably keeps in a register) shows the real difference in the arrays&#8217; performance. I thought the jagged array would be a little faster, but I certainly didn&#8217;t expect that much of a difference.</p>
<p>It&#8217;s pretty clear here that traversing a jagged array sequentially is hugely faster than traversing an equivalent rectangular array. The primary reason, it turns out, is array bounds checking. When accessing <code>array[i, j]</code>, the runtime has to check the bounds of both indexes. You would think that it would have to do the same thing for the jagged array, but the compiler optimizes the code to something akin to this:</p>
<pre><code>    private int SimpleSumJagged2(byte[][] array, int rows, int cols)
    {
        var SimpleSum = 0;
        for (var i = 0; i &lt; rows; ++i)
        {
            var theRow = array[i];
            for (var j = 0; j &lt; cols; ++j)
            {
                SimpleSum += theRow[j];
            }
        }
        return SimpleSum;
    }</code></pre>
<p>So the inner loop only has to do one bounds check per iteration.</p>
<p>In addition, a simple array (i.e. <code>byte[]</code>) is treated differently. It is assumed to be 0-based, so that indexing is a matter of <code>arrayBase + (index * size)</code>. The jagged array is a simple array of simple arrays, making indexing faster. See <a href="http://stackoverflow.com/a/468873/56778">this Stack Overflow answer</a> for a little more detail.</p>
<p>Rectangular arrays, on the other hand, can have non-zero bases. Not in C#, but the C# compiler generates code to be used by the runtime <code>Array</code> class implementation. Indexing into one of these arrays requires a little more math: <code>arrayBase + ((i - dim1Base) * (size * cols)) + ((j - dim2Base) * size)</code> (where <code>dim1Base</code> and <code>dim2Base</code> are the base indexes for each dimension). The compiler can optimize <em>some</em> of that, but it should be obvious that it can&#8217;t eliminate all of the extra work.</p>
<blockquote><p>It&#8217;s important not to dismiss the first test as irrelevant. Whereas the second test shows that jagged arrays <em>can be</em> faster than rectangular arrays, the first test shows that other factors can easily defeat any potential gain realized by using the &#8220;more efficient&#8221; data structure. In this case, simply accessing a class-scope variable in the inner loop made the compiler optimizations meaningless.</p></blockquote>
<p>I ran another test that modified each byte in the array. The idea here was to eliminate as much as possible all external factors. Here are the test methods.</p>
<pre><code>    private void SimpleTraversalRect(byte[,] array, int rows, int cols)
    {
        for (var i = 0; i &lt; rows; ++i)
        {
            for (var j = 0; j &lt; cols; ++j)
            {
                array[i, j] = (byte)(255 - array[i, j]);
            }
        }
    }

    private void SimpleTraversalJagged(byte[][] array, int rows, int cols)
    {
        for (var i = 0; i &lt; rows; ++i)
        {
            for (var j = 0; j &lt; cols; ++j)
            {
                array[i][j] = (byte)(255 - array[i][j]);
            }
        }
    }</code></pre>
<p>The timings are pretty much what you expect: 40 ms for the rectangular array and 24 ms for the jagged array.</p>
<p>Then I thought I&#8217;d do a little optimization myself by venturing into the world of unsafe code and pointers. The result looks more like C than C#, but it does show that you <em>can</em> optimize access to a rectangular array.</p>
<pre><code>    private void OptimizedTraversalRect(byte[,] array, int rows, int cols)
    {
        long count = (((long)rows) * cols)/3;
        unsafe
        {
            fixed (byte* pArray = array)
            {
                byte* p = pArray;
                while (count-- &gt; 0)
                {
                    *p++ = (byte)(255 - *p);
                    *p++ = (byte)(255 - *p);
                    *p++ = (byte)(255 - *p);
                }
            }
        }
    }

    private void OptimizedTraversalJagged(byte[][] array, int rows, int cols)
    {
        for (var i = 0; i &lt; rows; ++i)
        {
            unsafe
            {
                int count = cols/3;
                fixed (byte* pArray = array[i])
                {
                    byte* p = pArray;
                    while (count-- &gt; 0)
                    {
                        *p++ = (byte)(255 - *p);
                        *p++ = (byte)(255 - *p);
                        *p++ = (byte)(255 - *p);
                    }
                }
            }
        }
    }</code></pre>
<p>The timings here are exactly what I expected: 24 ms for the rectangular array and 24 ms for the jagged array. Actually, the rectangular array version is <em>slightly</em> faster, on average, because it doesn&#8217;t have to do as much work. Interestingly, if I don&#8217;t unroll the inner loop (i.e. only do one operation per iteration), both versions take <em>longer</em> than the original, un-optimized rectangular array version. The compiler is smarter than you think. I was able to outperform the compiler here only because I know that the array&#8217;s width is a multiple of three, and it was safe to unroll the loop.</p>
<p>Note that there&#8217;s a reason that code is considered unsafe: using the pointer, I circumvent any array bounds checking. Between that and eliminating the indexing calculations, I made traversing the rectangular array a simple sequential walk through memory.</p>
<p>So jagged arrays are faster, right? Well &#8230; maybe. What happens if you access them (semi-) randomly?</p>
<pre><code>    private const int Increment = 1009;
    private void RandomRect(byte[,] array, int rows, int cols)
    {
        int count = rows*cols;
        int row = 0;
        int col = 0;
        while (count-- &gt; 0)
        {
            row = (row + Increment) % rows;
            col = (col + Increment) % cols;
            array[row, col] = (byte)(255 - array[row, col]);
        }
    }

    private void RandomJagged(byte[][] array, int rows, int cols)
    {
        int count = rows * cols;
        int row = 0;
        int col = 0;
        while (count-- &gt; 0)
        {
            row = (row + Increment) % rows;
            col = (col + Increment) % cols;
            array[row][col] = (byte)(255 - array[row][col]);
        }
    }</code></pre>
<p>The timings here are telling: 310 ms for the rectangular array and 412 ms for the jagged array. The reason for the huge difference has to do with how the two array types are represented in memory. The rectangular array is one big block of memory. The jagged array is one block of memory for an array of row references, and a separate block of memory for each row. Accessing an arbitrary element in a jagged array, then, requires two separate lookups in memory that can be scattered all over. The result is approximately twice as many cache misses for the jagged array when compared to the rectangular array.</p>
<p>I understand, of course, that my &#8220;random&#8221; traversal is hardly random. It does, however, provide scattered access to the array elements in a manner that simulates what truly random access would do. I actually wrote a version that used a random number generator, but it took much longer to run and gave almost the same behavior as this simpler version.</p>
<p>So the answer to the question of which type of array is faster is, as you&#8217;ve probably figured out by now, &#8220;It depends.&#8221;</p>
<p><em>If</em> you can guarantee that external factors (like accessing a class-scope variable) won&#8217;t negate the compiler&#8217;s optimizations, then using a jagged array can be 40% faster than using an equivalent rectangular array. But if you&#8217;re accessing the array randomly, a jagged array can be 33% <em>slower</em> than a rectangular array. The question of which is faster is meaningless without context.</p>
<blockquote><p>The machine I ran these tests on is pretty old. I&#8217;ve also run the tests on a Core i7 processor and obtained similar results. The absolute times were faster (the machine is running at 3.4 GHz), but the percentage difference in the speed of the two array types are similar.</p></blockquote>
<h3>Non-speed considerations</h3>
<p>&#8220;Efficiency&#8221; means more than just raw speed. When discussing performance, we often find that speed and memory efficiency are inversely related. That is, doing something faster requires using more memory. That&#8217;s not always true, but it often is. If you&#8217;re concerned about efficiency you need to know not just how fast things are, but also how much memory they take and how that memory is allocated.</p>
<p>In addition to having different performance characteristics, jagged arrays and rectangular arrays are stored differently in memory. A rectangular array consists of a single allocation of size <code>rows * cols * element-size</code>, plus about 50 bytes of overhead for array metadata. A jagged array requires somewhat more memory. It consists of an allocation of size <code>rows * sizeof(IntPtr)</code> (plus metadata), and there is a separate allocation for each row, of size <code>cols * element-size</code>. So consider the array I showed above, which is 2,736 x 10,944. The difference in this large array is about 150 kilobytes, or less than one percent in this 30 megabyte array. Imagine, though, if the rows were only 50 bytes long. The jagged array would consume twice the memory of the rectangular array because the per-row overhead of 50 bytes still applies.</p>
<p>There are advantages to that distributed array representation, however. Prior to .NET 4.5, a rectangular array couldn&#8217;t occupy more than two gigabytes (slightly less, in reality). So an array of bytes with a million rows (actually, 2^20 rows) and 2,048 columns couldn&#8217;t be created. That is, <code>byte[10^20, 2048]</code> would produce <code>OutOfMemoryException</code>, regardless of how much memory you have on your machine.</p>
<p>But you could easily create a <code>short[][]</code> with 10^20 rows and 2,048 columns, because each row would be a paltry two kilobytes in size.</p>
<p>.NET 4.5 introduced the <a href="http://msdn.microsoft.com/en-us/library/hh285054.aspx">gcAllowVeryLargeObjects</a> configuration file element that lets you allocate objects larger than 2 gigabytes, which alleviates <em>some</em> of the problems, but there are still restrictions as pointed out in the documentation. And it&#8217;s often impossible to get a huge chunk of <em>contiguous</em> memory, which the rectangular array needs. So there are still benefits to the jagged array.</p>
<p>So, which to use? Whatever best fits your model. I prefer rectangular arrays in most cases because I find them easier to work with (try initializing a jagged array) and easier to reason about. I also know that I can optimize my array handling code if I need to, using unsafe code and pointers. But every situation is different, and I&#8217;ll happily use a jagged array if the situation calls for it.</p>
<p>Only you can decide which array representation is appropriate for your application. You have to weigh the costs and benefits and pick the one that best meets your needs. Don&#8217;t immediately fall for the simplistic &#8220;jagged is faster&#8221; hype that&#8217;s all too often spouted as a matter of faith without understanding the wider implications.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/05/08/are-jagged-arrays-faster-than-rectangular-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Polling for Cancellation</title>
		<link>http://blog.mischel.com/2013/05/07/polling-for-cancellation/</link>
		<comments>http://blog.mischel.com/2013/05/07/polling-for-cancellation/#comments</comments>
		<pubDate>Wed, 08 May 2013 01:16:54 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1836</guid>
		<description><![CDATA[<p>Last time, I showed how compiler optimization can cause problems with polling for cancellation, and discussed a few alternatives to solving the problem. At the end of the article I mentioned that my simple program is the uncommon case. Very often (perhaps most often?), your program&#8217;s background processing is done by a method in a <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/05/07/polling-for-cancellation/">Polling for Cancellation</a></span>]]></description>
				<content:encoded><![CDATA[<p><a href="http://blog.mischel.com/2013/04/30/how-not-to-poll-for-cancellation/">Last time</a>, I showed how compiler optimization can cause problems with polling for cancellation, and discussed a few alternatives to solving the problem. At the end of the article I mentioned that my simple program is the uncommon case. Very often (perhaps most often?), your program&#8217;s background processing is done by a method in a completely different class, and it&#8217;s common for there to be multiple background tasks running. It&#8217;s likely that you want a single method call or variable setting to signal cancellation for all of the background tasks.</p>
<p>Before I get to that, I want to address a few comments I received on Facebook in response to the last post.</p>
<p>Ron asks why this isn&#8217;t considered a compiler bug. On the face of it, it does look like the compiler is being overly aggressive. However, the compiler doesn&#8217;t know that the loop is executing in a separate thread. The compiler doesn&#8217;t understand that <code>ThreadPool.QueueUserWorkItem</code> starts a new background thread and that it should therefore widen its scope when examining the <code>cancelRequested</code> variable. Expecting the compiler to understand that is unreasonable, and forcing the compiler to <em>assume</em> that a local variable is accessible by multiple threads would eliminate a large number of perfectly valid optimizations. The compiler is behaving correctly here.</p>
<p>Readers Eric and Pete advocate moving the variable to class scope and adding the <code>volatile</code> qualifier. I agree that that&#8217;s the easiest solution and that it would work. I&#8217;d probably even use it if I were writing a simple program like the one in the last post. But I consider it a hack. Whereas Pete is right that an anonymous function closing over local variables muddies the concept of local variables, promoting the variable to class scope completely destroys any concept of information hiding. More importantly, there are the problems with <code>volatile</code> that I pointed out in the article. Those problems are irrelevant in this simple case, but I wouldn&#8217;t want to deal with them in a production program.</p>
<p>More importantly, as you&#8217;ll see below, <code>volatile</code> isn&#8217;t a good general solution.</p>
<p>Let&#8217;s move on from that toy demonstration program to a more likely real world design.</p>
<p>In the program below, the code that&#8217;s executed in the background thread is in a separate class that has no knowledge of or access to the code that calls it. It can&#8217;t access a public <code>cancelRequested</code> flag, so the flag is passed by reference.</p>
<pre><code>    public class Program
    {
        private static void Main(string[] args)
        {
            var p = new Program();
            p.DoStuff();
            Console.WriteLine("Press Enter to end program.");
            Console.ReadLine();
        }

        private void DoStuff()
        {
            bool cancelRequested = false;

            // Start tasks
            ThreadPool.QueueUserWorkItem((s) =&gt;
                {
                    var task1 = new Task1Class();
                    task1.DoTask1(ref cancelRequested);
                });

            Console.WriteLine("Press Enter to stop thread.");
            Console.ReadLine();

            // set cancel flag
            cancelRequested = true;
            Console.WriteLine("Cancel requested!");
        }
    }

    public class Task1Class
    {
        public void DoTask1(ref bool cancelRequested)
        {
            Console.WriteLine("Task1 started.");
            while (!cancelRequested)
            {
                // do stuff
            }
            Console.WriteLine("Task1 exit.");
        }
    }</code></pre>
<p>If you run that in the debugger, it will work as expected. But run it without the debugger attached and the thread never exits! Once again, the compiler has optimized things.</p>
<p>Moving the <code>cancelRequested</code> flag to the outer scope doesn&#8217;t help, nor does moving it to the outer scope and marking it as <code>volatile</code>. In fact, adding <code>volatile</code> results in this compiler warning:</p>
<blockquote><p>&#8220;a reference to a volatile field will not be treated as volatile.&#8221;</p></blockquote>
<p>As with the example in my previous post, doing certain things in the loop will prevent the compiler from applying the optimization that reveals this programming bug. But do you really want to count on knowing what those rules are? For every platform your code will be running on, in all versions past, present, and future? I didn&#8217;t think so.</p>
<p><strong>Passing the cancel flag by reference is a latent bug.</strong> Don&#8217;t do it.</p>
<blockquote><p>I have to admit here that I gave some bad advice to my friend who first asked me about this. I was convinced that the compiler wouldn&#8217;t perform that optimization on a reference parameter. I was wrong, as this little example shows. I still don&#8217;t fully understand the optimization rules, but I&#8217;ve come to the conclusion that the compiler assumes single-threaded code in most situations. That makes sense, actually. The compiler would have to avoid a whole host of optimizations if it <em>couldn&#8217;t</em> assume single-threaded code.</p></blockquote>
<p>If you&#8217;re writing multi-threaded code, you need to use synchronization primitives in order to control things. Prior to .NET 4.0, a common technique was to use a <a href="http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx">ManualResetEvent</a>, like this:</p>
<pre><code>    private void DoStuff()
    {
        ManualResetEvent cancelRequested = new ManualResetEvent(false); // &lt;---- 

        // Start tasks
        ThreadPool.QueueUserWorkItem((s) =&gt;
            {
                var task1 = new Task1Class();
                task1.DoTask1(cancelRequested);                         // &lt;----
            });

        Console.WriteLine("Press Enter to stop thread.");
        Console.ReadLine();

        // set cancel flag
        cancelRequested.Set();
        Console.WriteLine("Cancel requested!");
    }

    public class Task1Class
    {
        public void DoTask1(ManualResetEvent cancelRequested)           // &lt;----
        {
            Console.WriteLine("Task1 started.");
            while (!cancelRequested.WaitOne(0))                         // &lt;----
            {
                // do stuff
            }
            Console.WriteLine("Task1 exit.");
        }
    }</code></pre>
<p>The lines with &#8220;<code>// &lt;----</code>&#8221; comments are the only ones that change.</p>
<p>You can still use that technique in .NET 4.0 or later. You can get slightly better performance using <a href="http://msdn.microsoft.com/en-us/library/system.threading.manualreseteventslim.aspx">ManualResetEventSlim</a>. I see only two problems with using the events this way.</p>
<p>First, the expression <code>!cancelRequested.WaitOne(0)</code> isn&#8217;t exactly clear. It&#8217;s not as bad as the <code>Interlocked.CompareExchange</code> example from my last post, but it&#8217;s definitely not as clear as <code>!cancelRequested</code>.</p>
<p>More importantly, <em>any</em> thread can call <code>cancelRequested.Set</code> to cancel the entire operation. Granted, background tasks <em>shouldn&#8217;t</em> do that, but they <em>could</em>. It would be better if we could pass a read-only token of some sort.</p>
<p>.NET 4.0 formalized the concept of <a href="http://msdn.microsoft.com/en-us/library/dd997364(v=vs.100).aspx">Cancellation</a>, implemented with the <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource(v=vs.100).aspx">CancellationTokenSource</a> and <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(v=vs.100).aspx">CancellationToken</a> classes. Using these classes, you can implement cancellation in a standard, safe, and reliable manner.</p>
<p>The basic idea is that the main program creates a <code>CancellationTokenSource</code> instance, and passes the <code>Token</code> member (a <code>CancellationToken</code>) to the methods that want to check for cancellation. I&#8217;ll let you read up on the details.</p>
<p>Here&#8217;s an example.</p>
<pre><code>    private void DoStuff()
    {
        CancellationTokenSource cancel = new CancellationTokenSource();

        // Start tasks
        ThreadPool.QueueUserWorkItem((s) =&gt;
            {
                var task1 = new Task1Class();
                task1.DoTask1(cancel.Token);
            });

        Console.WriteLine("Press Enter to stop thread.");
        Console.ReadLine();

        // set cancel flag
        cancel.Cancel();
        Console.WriteLine("Cancel requested!");
    }

    public class Task1Class
    {
        public void DoTask1(CancellationToken cancelToken)
        {
            Console.WriteLine("Task1 started.");
            while (!cancelToken.IsCancellationRequested)
            {
                // do stuff
            }
            Console.WriteLine("Task1 exit.");
        }
    }</code></pre>
<p>That looks a lot cleaner to me. I can tell just by looking at the code <em>exactly</em> what is going on. Furthermore, there is no way that one of the tasks can inadvertently cancel the entire process because there&#8217;s no way for them to set the cancel flag. They can only query it.</p>
<p>Another advantage of this method (and the one that uses events) over the reference flag (which doesn&#8217;t work, but even if it did) is that you can pass the <code>CancellationToken</code> to a class&#8217;s constructor so that the class can cache it. That way, it doesn&#8217;t have to pass the cancellation token to every internal method that might need it. For example:</p>
<pre><code>    private void DoStuff()
    {
        CancellationTokenSource cancel = new CancellationTokenSource();

        // Start tasks
        ThreadPool.QueueUserWorkItem((s) =&gt;
            {
                var task1 = new Task1Class(cancel.Token);
                task1.DoTask1();
            });

        Console.WriteLine("Press Enter to stop thread.");
        Console.ReadLine();

        // set cancel flag
        cancel.Cancel();
        Console.WriteLine("Cancel requested!");
    }

    public class Task1Class
    {
        private readonly CancellationToken _cancelToken;

        public Task1Class(CancellationToken cancelToken)
        {
            _cancelToken = cancelToken;
        }

        public void DoTask1()
        {
            Console.WriteLine("Task1 started.");
            DoSubtask1();
            if (!_cancelToken.IsCancellationRequested)
            {
                DoSubtask2();
            }
            Console.WriteLine("Task1 exit.");
        }

        private void DoSubtask1()
        {
            while (!_cancelToken.IsCancellationRequested)
            {
                // do stuff
            }
        }

        private void DoSubtask2()
        {
            while (!_cancelToken.IsCancellationRequested)
            {
                // do stuff
            }
        }
    }</code></pre>
<p>You can&#8217;t store a reference in C# (you can store <em>an instance of a reference type</em>, but that&#8217;s a different thing), so there wouldn&#8217;t be any way to do this using a simple <code>Boolean</code> flag. You can&#8217;t write, for example:</p>
<pre><code>    private ref bool cancelRequested;    // this doesn't compile!</code></pre>
<p>The most important thing I learned from this investigation is:</p>
<p><strong>If you&#8217;re writing multi-threaded code, use synchronization primitives for all inter-thread communication.</strong></p>
<p>Furthermore, I&#8217;ve shown that <code>volatile</code> <em>is not a synchronization primitive.</em></p>
<p>If you&#8217;re writing for .NET 4.0 or later, then take advantage of <a href="http://msdn.microsoft.com/en-us/library/dd997364(v=vs.100).aspx">Cancellation</a>. Otherwise, use a <code>ManualResetEvent</code> or <code>ManualResetEventSlim</code> to approximate that functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/05/07/polling-for-cancellation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How not to poll for cancellation</title>
		<link>http://blog.mischel.com/2013/04/30/how-not-to-poll-for-cancellation/</link>
		<comments>http://blog.mischel.com/2013/04/30/how-not-to-poll-for-cancellation/#comments</comments>
		<pubDate>Tue, 30 Apr 2013 17:18:46 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1832</guid>
		<description><![CDATA[<p>Imagine you start a thread to perform some long-running task that you want the ability to cancel. For example, you might start that thread while your main program is waiting on user input, and then cancel that thread once the user has pressed Enter. It&#8217;s an easy thing to code up.</p> private static void Main(string[] <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/04/30/how-not-to-poll-for-cancellation/">How not to poll for cancellation</a></span>]]></description>
				<content:encoded><![CDATA[<p>Imagine you start a thread to perform some long-running task that you want the ability to cancel. For example, you might start that thread while your main program is waiting on user input, and then cancel that thread once the user has pressed Enter. It&#8217;s an easy thing to code up.</p>
<pre><code>    private static void Main(string[] args)
    {
        bool cancelRequested = false;
        Console.WriteLine("Starting thread.");
        ThreadPool.QueueUserWorkItem((s) =&gt;
            {
                Console.WriteLine("Thread started.");
                int counter = 0;
                while (!cancelRequested)
                {
                    // do some work here
                    ++counter;
                }
                Console.WriteLine("Thread stopped: {0}", counter);
            });
        Console.WriteLine("Press Enter to stop thread.");
        Console.ReadLine();
        cancelRequested = true;
        Console.WriteLine("Press Enter again to exit the program: ");
        Console.ReadLine();
    }</code></pre>
<p>If you run that program under the debugger, you&#8217;ll find that it works just fine in Debug and in Release mode. When you press Enter, you&#8217;ll see the &#8220;Thread stopped&#8221; message as expected. However, if you run without the debugger attached, you&#8217;ll find that the thread never terminates! What&#8217;s going on?</p>
<p>Simply put, compiler optimization. The JIT compiler sees that nothing in the loop can modify the <code>cancelRequested</code> variable, so it generates code to load the value and keep it in a register. The thread never references that variable again.</p>
<p>Interestingly, placing a <code>Thread.Sleep(0)</code> in the loop disables that optimization, as does adding a <code>lock</code> statement. Other function calls inside the loop might also prevent that optimization, as will sufficiently complex calculations. If the exact rules for what prevents that optimization are written anywhere, I&#8217;ve been unable to find them. The <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms228593.aspx">C# Language Specification</a> hints at some things, but I was unable to find any explicit language.</p>
<p>Absent explicit language in the specification, I&#8217;m hesitant to rely on the current behavior because it could change in the next version of the compiler or on a different platform.</p>
<p>There are various ways to force the compiler to examine memory at every iteration of the loop, but the options are limited when working with a <code>Boolean</code>. <code>Interlocked.CompareExchange</code> came immediately to mind, except that there isn&#8217;t an overload that takes a <code>ref bool</code>. I could make the variable an <code>int</code>, and write:</p>
<pre><code>    while (Interlocked.CompareExchange(ref cancelRequested, 1, 1) == 0)</code></pre>
<p>Besides being ugly, it&#8217;s not at all clear what&#8217;s going on there. I&#8217;ve <em>written</em> stuff like that and six months later had to come back and figure out what&#8217;s happening. If you had to go look up <a href="http://msdn.microsoft.com/en-us/library/801kt583.aspx">Interlocked.CompareExchange</a> and puzzle out what&#8217;s happening, I think you&#8217;ll agree that this is a less than ideal solution.</p>
<p>Those of you with some more experience might say, &#8220;Make it volatile!&#8221; There are some problems with that. First, you can&#8217;t mark a local variable with the <a href="http://msdn.microsoft.com/en-us/library/x13ttww7.aspx">volatile</a> keyword. I&#8217;d have to promote the variable to class scope. That works, sure, but it&#8217;s dissatisfying having to move a variable to an outer scope just so I can make it work the way I want. It just <em>feels</em> wrong.</p>
<p>A major problem with <code>volatile</code> is that it applies different semantics to variable access, but does it at the declaration site. I don&#8217;t know, when I&#8217;m writing code to access a variable, that doing so will involve <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff540496(v=vs.85).aspx">acquire semantics</a>. It would be like stepping back to the bad old days of Pascal, where passing a reference (<code>var</code>) parameter looked the same as passing a parameter by value. That is, given this procedure:</p>
<pre><code>    procedure Frob(var fooby: Integer);</code></pre>
<p>The code to call it doesn&#8217;t explicitly state that the parameter is passed by reference:</p>
<pre><code>    Frob(foo);</code></pre>
<p>I rather like having to specify <code>ref</code> in C#, and I don&#8217;t like the implicit change in memory access semantics that comes along with <code>volatile</code>.</p>
<p>At least <code>Interlocked.CompareExchange</code> tells you that there&#8217;s something special about that <code>cancelRequested</code> variable, even if the logic behind what&#8217;s happening is confusing.</p>
<p>More importantly, people who know a lot more than I do about C#, .NET, and multithreading in general caution against the use of <code>volatile</code> for several reasons, one of which is that it probably doesn&#8217;t work the way you think it works or do what you think it does. See, for example, Eric Lippert&#8217;s <a href="http://blogs.msdn.com/b/ericlippert/archive/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three.aspx">Atomicity, volatility and immutability are different, part three</a> and Joe Duffy&#8217;s <a href="http://www.bluebytesoftware.com/blog/2010/12/04/SayonaraVolatile.aspx">Sayonara volatile</a>.</p>
<p>Based on those comments, <code>volatile</code> is not the thing I want to use when trying to write portable and reliable code. I would suggest that you, too, avoid it.</p>
<p>All that said, there are other ways I could solve this problem. But the simple program I showed at the start of this entry is a pretty rare case. Real programs often have multiple background tasks running, all of which are subject to cancellation either individually or as a group, and a simple <code>Boolean</code> variable is not a very good way to control such things.</p>
<p>There is a much more flexible and reliable way, which I&#8217;ll talk about in my next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/04/30/how-not-to-poll-for-cancellation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Wear your carving glove</title>
		<link>http://blog.mischel.com/2013/04/24/wear-your-carving-glove/</link>
		<comments>http://blog.mischel.com/2013/04/24/wear-your-carving-glove/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 01:31:42 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Carving]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1827</guid>
		<description><![CDATA[<p>Saturday evening, Debra and I were over at a friend&#8217;s place. There were eight or ten of us, total, sitting or standing around, just conversing. I was also whittling on a piece of Ash, making another little dog. I don&#8217;t know if my attention lapsed (possible) or if the knife just slipped (more likely), but <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/04/24/wear-your-carving-glove/">Wear your carving glove</a></span>]]></description>
				<content:encoded><![CDATA[<p>Saturday evening, Debra and I were over at a friend&#8217;s place. There were eight or ten of us, total, sitting or standing around, just conversing. I was also whittling on a piece of Ash, making another <a href="http://mischel.com/whittlepup/">little dog</a>. I don&#8217;t know if my attention lapsed (possible) or if the knife just slipped (more likely), but suddenly I was cutting into my left hand.</p>
<p>Surprisingly enough, it didn&#8217;t really hurt. I stared at it for a second, surprised, then stood up and grabbed a paper towel to stop the bleeding. Then to the kitchen sink to rinse it, thinking I&#8217;d just clean it a bit, bandage it up, and go back to my carving. I&#8217;ve had plenty of minor cuts, so I wasn&#8217;t terribly worried.</p>
<p>I only needed a brief look at the laceration while I was running it under the faucet to know that this one would need stitches. My friend handed me a wash cloth, we secured it with duct tape, and Debra drove me to the emergency room.</p>
<p>The second surprise (the first was that I cut myself so badly) was that the emergency room was not full at 9:00 on a Saturday night. But then, Round Rock is kind of a sleepy place. I walked up to the check-in desk. The triage nurse was safely ensconced behind a window.</p>
<p>Nurse: &#8220;How can I help you?&#8221;</p>
<p>Me (holding up left hand): &#8220;I cut myself rather badly.&#8221;</p>
<p>Nurse: &#8220;The duct tape didn&#8217;t fix it?&#8221;</p>
<p>Me (laughing): &#8220;No, but it&#8217;s holding things together for now.&#8221;</p>
<p>Less than a minute later, I was sitting in an exam room with a RN removing an impromptu bandage while a nursing student looked on. When she got it uncovered she said, &#8220;That&#8217;s not so bad, but recovery is gonna suck.&#8221; I had what is (apparently commonly) called a suturable laceration. It was still bleeding a bit, so she had me apply a little pressure while we waited for the nurse practitioner to come along and stitch me up.</p>
<p>And waited. &#8230; And waited.</p>
<p>I&#8217;m not sure what he used to deaden the area, but it sure hurt like heck going in. I couldn&#8217;t feel the needle going in, but it sure hurt when he pushed the contents into my hand. But within a minute my thumb felt like it&#8217;d been shot up by the dentist, and I couldn&#8217;t feel a thing when he started stitching.</p>
<p>The cut, by the way, is about 7 centimeters (2-3/4 inches) long, running from the base of my thumb to the center of my wrist, and almost a centimeter deep. If you&#8217;re morbidly curious, you can <a href="http://blog.mischel.com/wp-content/uploads/2013/04/382A0003.jpg">see the result</a>.</p>
<p>Then we got to wait a even longer. Debra and I got some amusement out of all the forms they brought in, and I laughed out loud when I had to sign a document saying that I received a copy of the hospital&#8217;s no smoking policy. Oh, and they gave me a 10 mg <a href="http://www.drugs.com/norco.html">Norco</a> tablet and told me not to drive, and a prescription for antibiotics and more Norco. I filled the antibiotic prescription but decided against the Norco. It didn&#8217;t hurt <em>that</em> bad.</p>
<p>All in all it took about 45 minutes from the time we walked in the door until my hand was stitched up. It took another 45 minutes or an hour before they finally brought the bill, took my payment, and then came and put on the bandage before I could walk out the door. Gotta love bureaucracy.</p>
<p>And all because I wasn&#8217;t wearing my <a href="http://www.woodcarvers.com/gloves.htm">carving glove</a>. The glove is Kevlar, and probably wouldn&#8217;t have completely prevented the cut, but it most likely would have made the difference between a surface scratch and a trip to the emergency room.</p>
<p>I&#8217;d been lax, not wearing the glove because I thought I was good enough not to need it. Only takes one slip of the blade, though, to ruin your day. I&#8217;ll be wearing the glove from now on.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/04/24/wear-your-carving-glove/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to time code</title>
		<link>http://blog.mischel.com/2013/04/24/how-to-time-code/</link>
		<comments>http://blog.mischel.com/2013/04/24/how-to-time-code/#comments</comments>
		<pubDate>Wed, 24 Apr 2013 17:53:27 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1825</guid>
		<description><![CDATA[<p>Many Stackoverflow questions amount to nothing more than, &#8220;Which of these is faster?&#8221; or &#8220;Why is this so slow?&#8221; Very often, the person posting the question will include code that outputs some timing results. More often than not, the timing code is just wrong. Sometimes fatally so.</p> <p>By the way, most &#8220;Which is faster&#8221; questions <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/04/24/how-to-time-code/">How to time code</a></span>]]></description>
				<content:encoded><![CDATA[<p>Many <a href="http://stackoverflow.com">Stackoverflow</a> questions amount to nothing more than, &#8220;Which of these is faster?&#8221; or &#8220;Why is this so slow?&#8221; Very often, the person posting the question will include code that outputs some timing results. More often than not, the timing code is just wrong. Sometimes fatally so.</p>
<p>By the way, most &#8220;Which is faster&#8221; questions are non-starters. You shouldn&#8217;t ask before you&#8217;ve done the benchmark yourself. Read on.</p>
<p>If you want accurate timing information, you have to do it the right way. If you do it the wrong way, your results will be at best unreliable. Fortunately, doing things the right way is pretty easy.</p>
<p>But before I go any further, I feel obligated to ask the most important question: Why do you care? Before you start adding code to profile small parts of your application, you need to ask yourself if it&#8217;s really that important. Or are you just wasting your time worrying about the performance of something that is already &#8220;fast enough?&#8221; I strongly suggest that you read Eric Lippert&#8217;s, <a href="http://ericlippert.com/2012/12/17/performance-rant/">Which is faster?</a></p>
<p>Now, if you <em>really<em> need to time your code. . .</em></em></p>
<p>The information below is for .NET, and the code samples are in C#. You should follow the same techniques if you&#8217;re writing in Visual Basic .NET or Managed C++, and probably any other .NET language. The general techniques are true of pretty much any other platform as well, although the specifics will be much different.</p>
<p>When doing benchmarks, run them against a release build with no debugger attached. In Visual Studio, be sure to select the Release build, and either press <strong>Ctrl+F5</strong> to run, or select <strong>Debug -&gt; Start Without Debugging</strong>. Timings gathered from a debug build or from having the debugger attached (even in a Release build) will be inaccurate. You&#8217;ll often find that there is little difference between Debug and Release builds when the debugger is attached.</p>
<p>Do not use <code>DateTime.Now</code>, <code>DateTime.UtcNow</code>, or anything else based on <code>DateTime</code> as your time source. There are two reasons for this. First, the resolution of <code>DateTime</code> is somewhat questionable, but probably not much better than 15 milliseconds. Secondly, the clock can change on you. I once had a bit of code take -54 minutes (yes, it completed 54 minutes before it started) to execute because it started four minutes before 2:00 AM on &#8220;Fall back&#8221; day (Daylight Saving Time autumn adjustment date) and finished six minutes later. So start time was 01:56:00 and end time was 01:02:00. You don&#8217;t control the system clock, so don&#8217;t depend on it.</p>
<p>Use the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx">System.Diagnostics.Stopwatch</a> class for all elapsed time measurements. That&#8217;s based on the Windows high performance timer, which is the most accurate interval timer you&#8217;re likely to get on a Windows box unless you have special hardware. Accept no substitutes.</p>
<p>With <code>Stopwatch</code>, timings are easy. First, add this <code>using</code> statement to the top of your source file:</p>
<pre><code>    using System.Diagnostics;</code></pre>
<p>Then, surround the code you want to time with a <code>Stopwatch</code>:</p>
<pre><code>    Stopwatch sw = Stopwatch.StartNew(); // creates and starts the stopwatch
    //
    // put the code you want to time here
    //
    sw.Stop();  // stop the watch.
    Console.WriteLine("Elapsed time = {0} milliseconds", sw.ElapsedMilliseconds);</code></pre>
<p>That&#8217;s all there is to it!</p>
<p>I usually like to report times in milliseconds (1/1000 second), simply because the usable range is reasonable. If I&#8217;m timing something that takes less than a millisecond to run, I need to do it many times to get a good average. A million iterations of any non-trivial loop will likely take longer than a few milliseconds. On the same note, a full day is 86,400 seconds or 86,400,000 milliseconds: a number that is still reasonably easy to work with. If I&#8217;m timing something that takes longer than a full day to run, I might decide to use seconds as my comparison.</p>
<p>One other nice thing about <code>Stopwatch</code> is that you can have many of them, each timing a discrete part of your program. For example:</p>
<pre><code>    Stopwatch totalTime = Stopwatch.StartNew();

    Stopwatch pass1Time = Stopwatch.StartNew();
    DoPass1();
    pass1Time.Stop();

    Stopwatch pass2Time = Stopwatch.StartNew();
    DoPass2();
    pass2Time.Stop();

    totalTime.Stop();
</code></pre>
<p>You then have separate values for the total time and the time to execute each pass.</p>
<p>If you&#8217;ve done all of that and you still haven&#8217;t found an answer, <em>then</em> post a question. But include your timing code so that those of us who are willing to help you find an answer aren&#8217;t groping around in the dark. And be sure to mention that you ran in Release mode without the debugger attached.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/04/24/how-to-time-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How not to parse a file name</title>
		<link>http://blog.mischel.com/2013/04/17/how-not-to-parse-a-file-name/</link>
		<comments>http://blog.mischel.com/2013/04/17/how-not-to-parse-a-file-name/#comments</comments>
		<pubDate>Wed, 17 Apr 2013 18:31:12 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mischel.com/?p=1821</guid>
		<description><![CDATA[<p>I&#8217;ve seen some pretty crazy code over the years, and this snippet that&#8217;s supposed to strip the extension from a Windows file name ranks right up there with the best of them.</p> string GetFileNameWithoutExtension(string filename) { string result = filename; char[] charArray = filename.ToCharArray(); Array.Reverse(charArray); string s1 = new string(charArray); string[] ext = s1.Split(new char[] <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.mischel.com/2013/04/17/how-not-to-parse-a-file-name/">How not to parse a file name</a></span>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve seen some pretty crazy code over the years, and this snippet that&#8217;s supposed to strip the extension from a Windows file name ranks right up there with the best of them.</p>
<pre><code>    string GetFileNameWithoutExtension(string filename)
    {
        string result = filename;
        char[] charArray = filename.ToCharArray();
        Array.Reverse(charArray);
        string s1 = new string(charArray);
        string[] ext = s1.Split(new char[] {'.'}, 2);
        if (ext.Length &gt; 1)
        {
            char[] charArray2 = ext[1].ToCharArray();
            Array.Reverse(charArray2);
            result = new string(charArray2);
        }
        return result;
    }
</code></pre>
<p>Perhaps the most important thing to understand about this code is that it&#8217;s totally unnecessary. There is a method, <a href="http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx"><code>Path.GetFileNameWithoutExtension</code></a>, which has been in the .NET Framework since version 1.1, that does <em>exactly</em> what the name implies.</p>
<p>But assuming the author didn&#8217;t know about that, why would he reverse, split, fold, spindle, and mutilate the string just to get the part after the last period?</p>
<blockquote><p>When I was a kid, I was fascinated by that expression, &#8220;fold, spindle, or mutilate.&#8221; I knew what fold and mutilate were, but I just couldn&#8217;t figure out <em>spindle</em>. One day I was at the dry cleaners with my mom and a new employee was learning how to work the cash register. The experienced employee said, &#8220;&#8230;and then you put the ticket on the spindle,&#8221; and pushed the ticket onto the sharp pointy thing there on the counter. I still remember the feeling of the light going on in my head.</p></blockquote>
<p>Anyway, back to the topic at hand.</p>
<p>This is the wonkiest code I&#8217;ve ever seen to do such a simple job. In a nutshell, it turns a filename, say &#8220;c:\folder\subdir\filename.ext&#8221; into &#8220;txe.emanelif\ridbus\redlof\:c&#8221;. Then, the call to <code>string.Split</code> splits it on the dot, giving two strings:</p>
<pre><code>    "txe"
    "emanelif\ridbus\redlof\:c"</code></pre>
<p>The code then takes the second of the two strings, reverses it, and that&#8217;s the result.</p>
<p>Convoluted in the extreme. Idiotically clever.</p>
<p>I guess the author had never heard of <a href="http://msdn.microsoft.com/en-us/library/system.string.lastindexof.aspx"><code>String.LastIndexOf</code></a>, either. With that method, you can duplicate the code above in two lines:</p>
<pre><code>    int dotPos = fileName.LastIndexOf('.');
    result = (dotPos == -1) ? fileName : fileName.SubString(0, dotPos);</code></pre>
<p>Yep. That&#8217;s all his code does.</p>
<p>By the way, there&#8217;s a bug in his code and in mine. Given a file name like &#8220;c:\folder\subdir.001\filename_without_extension&#8221;, both will give a result of &#8220;c:\folder\subdir&#8221;.</p>
<p>To do it right, you have to take the slashes into account, too:</p>
<pre><code>    string GetFileNameWithoutExtension(string filename)
    {
        int dotPos = filename.LastIndexOf('.');
        // if there's no dot, there's no extension
        if (dotPos == -1)
        {
            return filename;
        }
        int slashPos = filename.LastIndexOf('\\');
        // if the last slash is after the last dot, then there's no extension
        if (slashPos &gt; dotPos)
        {
            return filename;
        }
        return filename.Substring(0, dotPos);
    }</code></pre>
<p>I sure hope the guy who wrote that original code isn&#8217;t being <em>paid</em> to write computer programs. I pity his poor clients, living with those ticking time bombs in their code. Frightening.</p>
<p>Or perhaps I should hope for more people writing such code. I&#8217;ve made a lot of money over the years fixing crazy stuff like that.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mischel.com/2013/04/17/how-not-to-parse-a-file-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
