python function call with/without list comprehension : <p>I have below two functions:</p>

<pre><code>def foo(n=50000):
    return sum(i*i for i in range(n))  # just called sum() directly without 

def bar(n=50000):
    return sum([i*i for i in range(n)])  # passed constructed list to sum()
</code></pre>

<p>I was hoping that <code>foo</code> will run faster then <code>bar</code> but I have checked in ipython with <code>%%timeit</code> that <code>foo</code> is taking slightly longer then <code>bar</code> </p>

<pre><code>In [2]: %%timeit
   ...: foo(50000)
   ...: 
100 loops, best of 3: 4.22 ms per loop

In [3]: %%timeit
   ...: bar(50000)
   ...: 
100 loops, best of 3: 3.45 ms per loop
In [4]: %%timeit
   ...: foo(10000000)
   ...: 
1 loops, best of 3: 1.02 s per loop

In [5]: %%timeit
   ...: bar(10000000)
   ...: 
1 loops, best of 3: 869 ms per loop
</code></pre>

<p>The difference increases as I increase value of n hence I tried to check function with <code>dis.dis(foo)</code> and <code>dis.dis(bar)</code> but it was identical.</p>

<p>So what would be the cause of such time difference between both methods?</p>
