title: Speed Up Execution ,Python
body: <p><code>for</code> loops are quite expensive when it comes to execution time. I am building a correction algorithm and I've used peter norvig's code of spell correction . I modified it a bit and realized it is taking too long to execute the optimization on thousands of words.</p>
  
  <p>The algorithm checks for 1 and 2 edit distance and corrects it. I've made it 3 . So that might increase the time (I am not sure).  Here is a part of the end where the highest occurring words are used as reference: </p>
  
  <pre><code>def correct(word):
      candidates = (known([word]).union(known(edits1(word)))).union(known_edits2(word).union(known_edits3(word)) or [word]) # this is where the problem is
  
      candidate_new = []
      for candidate in candidates: #this statement isnt the problem
          if soundex(candidate) == soundex(word):
              candidate_new.append(candidate)
      return max(candidate_new, key=(NWORDS.get))
  </code></pre>
  
  <p>And it looks like the statement <code>for candidate in candidates</code> is increasing the execution time. You could easily have a look at the code of peter norvig, Click <a href="http://norvig.com/spell-correct.html" rel="nofollow">here</a>.<br>
  I've figured out the problem. It's in the statement  </p>
  
  <pre><code>candidates = (known([word]).union(known(edits1(word)))
               ).union(known_edits2(word).union(known_edits3(word)) or [word])
  </code></pre>
  
  <p>where ,  </p>
  
  <pre><code>def known_edits3(word):
      return set(e3 for e1 in edits1(word) for e2 in edits1(e1) 
                                        for e3 in edits1(e2) if e3 in NWORDS)  
  </code></pre>
  
  <p>It can be seen that there are 3 for loops inside <code>edits3</code> which increases execution time 3 fold. <code>edits2</code> has 2 for loops . so this is the culprit. </p>
  
  <p>How do I minimize this expression? 
  Could <code>itertools.repeat</code> help out with this one??</p>