title: Python string concatenation loop
body: <p>I've been surprised by the results provided by %timeit for these two implementations :</p>
  
  <pre><code>def f1():                                                                       
      s = ''                                                                    
      for i in range(len(values)):                                                
          s += str(values[i][0])                                                
          s += '\t'                                                             
          s += str(values[i][1])                                    
          s += '\r\n'                                                           
      return s  
  </code></pre>
  
  <p>and</p>
  
  <pre><code>def f2():                                                                       
      return ''.join((                                                            
                  str(ts) + '\t' + str(v) + '\r\n'                                  
                  for ts, v in values                                             
              ))  
  </code></pre>
  
  <p>knowing that <code>values</code> is a list of approx 2400 tuples. <code>f1()</code> is the original code I found in a script written by a colleague more used to C/C++ that to Python at the time he wrote it, and <code>f2</code> is IMHO the more Pythonic style I would have written for the same processing.</p>
  
  <p>I would have expected that <code>f2</code> being a lot faster that <code>f1</code>, mainly because <code>f1</code> uses a lot of concatenations and string re-allocations but it occurs that <code>%timeit</code> gives the same magnitude order for both ones (approx. 18ns), and more surprisingly, giving <code>f2</code> 1ns faster sometimes 1ns.</p>
  
  <p>What could be the explanation for such a result ?</p>
  
  <p>[EDITED Jul 14] fixed f1 for the <code>str</code> override by the local variable with the same name. This error was not present in the profiled code however.</p>