title: Why is a generator produced by yield faster than a generator produced by xrange?
body: <p>I was investigating Python generators and decided to run a little experiment.</p>
  
  <pre><code>TOTAL = 100000000
  def my_sequence():
      i = 0
      while i &lt; TOTAL:
          yield i
          i += 1
  
  def my_list():
      return range(TOTAL)
  
  def my_xrange():
      return xrange(TOTAL)    
  </code></pre>
  
  <p>Memory usage (using psutil to get process RSS memory) and time taken (using time.time()) are shown below after running each method several times and taking the average:</p>
  
  <pre><code>sequence_of_values = my_sequence() # Memory usage: 6782976B  Time taken: 9.53674e-07 s
  
  sequence_of_values2 = my_xrange() # Memory usage: 6774784B  Time taken: 2.14576e-06 s
  
  list_of_values = my_list() # Memory usage: 3266207744B  Time taken: 1.80253s
  </code></pre>
  
  <p>I noticed that producing a generator by using xrange is consistently (slightly) slower than that by using yield. Why is that so? </p>