title: LINQ method for adding items to a dictionary
body: <p>I'm trying to learn a bit more about LINQ by implementing Peter Norvig's <a href="http://norvig.com/spell-correct.html" rel="noreferrer">spelling corrector</a> in C#.</p>
  
  <p>The first part involves taking a large <a href="http://norvig.com/big.txt" rel="noreferrer">file of words</a> (about 1 million) and putting it into a dictionary where the <code>key</code> is the word and the <code>value</code> is the number of occurrences.</p>
  
  <p>I'd normally do this like so:</p>
  
  <pre><code>foreach (var word in allWords)                                                    
  {           
      if (wordCount.ContainsKey(word))
          wordCount[word]++;
      else
          wordCount.Add(word, 1);
  }
  </code></pre>
  
  <p>Where <code>allWords</code> is an <code>IEnumerable&lt;string&gt;</code></p>
  
  <p>In LINQ I'm currently doing it like this:</p>
  
  <pre><code>var wordCountLINQ = (from word in allWordsLINQ
                           group word by word
                           into groups
                           select groups).ToDictionary(g =&gt; g.Key, g =&gt; g.Count());  
  </code></pre>
  
  <p>I compare the 2 dictionaries by looking at all the <code>&lt;key, value&gt;</code> and they're identical, so they're producing the same results.</p>
  
  <p>The <code>foreach</code> loop takes <strong>3.82 secs</strong> and the LINQ query takes <strong>4.49 secs</strong></p>
  
  <p>I'm timing it using the Stopwatch class and I'm running in RELEASE mode. I don't think the performance is bad I was just wondering if there was a reason for the difference.</p>
  
  <p>Am I doing the LINQ query in an inefficient way or am I missing something?</p>
  
  <p><strong>Update:</strong> here's the full benchmark code sample:</p>
  
  <pre><code>public static void TestCode()
  {
      //File can be downloaded from http://norvig.com/big.txt and consists of about a million words.
      const string fileName = @"path_to_file";
      var allWords = from Match m in Regex.Matches(File.ReadAllText(fileName).ToLower(), "[a-z]+", RegexOptions.Compiled)
                     select m.Value;
  
      var wordCount = new Dictionary&lt;string, int&gt;();
      var timer = new Stopwatch();            
      timer.Start();
      foreach (var word in allWords)                                                    
      {           
          if (wordCount.ContainsKey(word))
              wordCount[word]++;
          else
              wordCount.Add(word, 1);
      }
      timer.Stop();
  
      Console.WriteLine("foreach loop took {0:0.00} ms ({1:0.00} secs)\n",
              timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000.0);
  
      //Make LINQ use a different Enumerable (with the exactly the same values), 
      //if you don't it suddenly becomes way faster, which I assmume is a caching thing??
      var allWordsLINQ = from Match m in Regex.Matches(File.ReadAllText(fileName).ToLower(), "[a-z]+", RegexOptions.Compiled)
                     select m.Value;
  
      timer.Reset();
      timer.Start();
      var wordCountLINQ = (from word in allWordsLINQ
                              group word by word
                              into groups
                              select groups).ToDictionary(g =&gt; g.Key, g =&gt; g.Count());  
      timer.Stop();
  
      Console.WriteLine("LINQ took {0:0.00} ms ({1:0.00} secs)\n",
              timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000.0);                     
  }
  </code></pre>