title: Python for loop performance in global space vs inside a function
body: <p>I've been trying to understand Python for loop performance, here I found that for loop with same iterations in global space is taking much time compared with one inside a function.</p>
  
  <pre><code>import time
  
  MAX_NUM = 50000000
  
  # Running for loop in global space
  start = time.time()
  
  for i in xrange(MAX_NUM):
      pass
  
  print time.time() - start
  
  
  # Running th same kind of loop within a function
  def foo():
      for i in xrange(MAX_NUM):
          pass
  
  start = time.time()
  foo()
  print time.time() - start
  </code></pre>
  
  <p>When I execute this I've witnessed a huge difference in execution times.</p>
  
  <pre><code>2.00527501106
  0.811304092407
  </code></pre>
  
  <p>I'm wondering that what makes this huge difference in execution time? how for loop performance affects from global space to write inside a function?</p>