title: Python built-in sum function vs. for loop performance
body: <p>I noticed that Python's built-in <code>sum</code> function is roughly 3x faster than a for loop when summing a list of 1 000 000 integers:</p>
  
  <pre><code>import timeit
  
  def sum1():
      s = 0
      for i in range(1000000):
          s += i
      return s
  
  def sum2():
      return sum(range(1000000))
  
  print 'For Loop Sum:', timeit.timeit(sum1, number=10)
  print 'Built-in Sum:', timeit.timeit(sum2, number=10)
  
  # Prints:
  # For Loop Sum: 0.751425027847
  # Built-in Sum: 0.266746997833
  </code></pre>
  
  <p>Why is that? How is <code>sum</code> implemented?</p>