<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Speed up Python without effort using generator expressions</title>
	<atom:link href="http://www.daniel-lemire.com/blog/archives/2008/09/05/speed-up-python-without-effort-using-generator-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daniel-lemire.com/blog/archives/2008/09/05/speed-up-python-without-effort-using-generator-expressions/</link>
	<description>Daniel Lemire's blog is about life in academia, research in Computer Science, wondering how we can reconcile fast databases and algorithms with the informal and asemantic nature of the world around us. It is broadcasted from Montreal (Canada).</description>
	<pubDate>Fri, 09 Jan 2009 05:22:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Daniel Lemire</title>
		<link>http://www.daniel-lemire.com/blog/archives/2008/09/05/speed-up-python-without-effort-using-generator-expressions/comment-page-1/#comment-50157</link>
		<dc:creator>Daniel Lemire</dc:creator>
		<pubDate>Fri, 19 Sep 2008 15:53:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.daniel-lemire.com/blog/?p=1268#comment-50157</guid>
		<description>Running code through cProfile is not fair since it adds an overhead for each function call.

Run these commands instead:

echo "sum([x for x in xrange(1000000)])" &gt; t1.py

echo "sum((x for x in xrange(1000000)))" &gt; t2.py

python t1.py

python t2.py</description>
		<content:encoded><![CDATA[<p>Running code through cProfile is not fair since it adds an overhead for each function call.</p>
<p>Run these commands instead:</p>
<p>echo &#8220;sum([x for x in xrange(1000000)])&#8221; > t1.py</p>
<p>echo &#8220;sum((x for x in xrange(1000000)))&#8221; > t2.py</p>
<p>python t1.py</p>
<p>python t2.py</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andres</title>
		<link>http://www.daniel-lemire.com/blog/archives/2008/09/05/speed-up-python-without-effort-using-generator-expressions/comment-page-1/#comment-50156</link>
		<dc:creator>Andres</dc:creator>
		<pubDate>Fri, 19 Sep 2008 14:47:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.daniel-lemire.com/blog/?p=1268#comment-50156</guid>
		<description>Hi Daniel! 
Reading this post I try myself to do it. The results were not the same as you, I post it here. I work on python and I think that is correct that this happens, because generators in some operations are more expensive that simple list. when you have to read a big file generators are the solution, but in other cases lists are the solution!

cProfile.run('sum((x for x in xrange(1000000)))')
         1000004 function calls in 1.305 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000001    0.565    0.000    0.565    0.000 :1()
        1    0.000    0.000    1.305    1.305 :1()
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.740    0.740    1.305    1.305 {sum}


cProfile.run('sum([x for x in xrange(1000000)])')
         3 function calls in 0.540 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.357    0.357    0.540    0.540 :1()
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.182    0.182    0.182    0.182 {sum}</description>
		<content:encoded><![CDATA[<p>Hi Daniel!<br />
Reading this post I try myself to do it. The results were not the same as you, I post it here. I work on python and I think that is correct that this happens, because generators in some operations are more expensive that simple list. when you have to read a big file generators are the solution, but in other cases lists are the solution!</p>
<p>cProfile.run(&#8217;sum((x for x in xrange(1000000)))&#8217;)<br />
         1000004 function calls in 1.305 CPU seconds</p>
<p>   Ordered by: standard name</p>
<p>   ncalls  tottime  percall  cumtime  percall filename:lineno(function)<br />
  1000001    0.565    0.000    0.565    0.000 :1()<br />
        1    0.000    0.000    1.305    1.305 :1()<br />
        1    0.000    0.000    0.000    0.000 {method &#8216;disable&#8217; of &#8216;_lsprof.Profiler&#8217; objects}<br />
        1    0.740    0.740    1.305    1.305 {sum}</p>
<p>cProfile.run(&#8217;sum([x for x in xrange(1000000)])&#8217;)<br />
         3 function calls in 0.540 CPU seconds</p>
<p>   Ordered by: standard name</p>
<p>   ncalls  tottime  percall  cumtime  percall filename:lineno(function)<br />
        1    0.357    0.357    0.540    0.540 :1()<br />
        1    0.000    0.000    0.000    0.000 {method &#8216;disable&#8217; of &#8216;_lsprof.Profiler&#8217; objects}<br />
        1    0.182    0.182    0.182    0.182 {sum}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlos</title>
		<link>http://www.daniel-lemire.com/blog/archives/2008/09/05/speed-up-python-without-effort-using-generator-expressions/comment-page-1/#comment-50129</link>
		<dc:creator>Carlos</dc:creator>
		<pubDate>Fri, 05 Sep 2008 16:30:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.daniel-lemire.com/blog/?p=1268#comment-50129</guid>
		<description>Not only that, but expression 2 doesn't leak a variable!

$ python 
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&#62;&#62;&#62; i
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'i' is not defined
&#62;&#62;&#62; sum([i for i in xrange(100000)])
4999950000L
&#62;&#62;&#62; i
99999
&#62;&#62;&#62; sum(j for j in xrange(100000))
4999950000L
&#62;&#62;&#62; j
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'j' is not defined
&#62;&#62;&#62;</description>
		<content:encoded><![CDATA[<p>Not only that, but expression 2 doesn&#8217;t leak a variable!</p>
<p>$ python<br />
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)<br />
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin<br />
Type &#8220;help&#8221;, &#8220;copyright&#8221;, &#8220;credits&#8221; or &#8220;license&#8221; for more information.<br />
&gt;&gt;&gt; i<br />
Traceback (most recent call last):<br />
  File &#8220;&#8221;, line 1, in<br />
NameError: name &#8216;i&#8217; is not defined<br />
&gt;&gt;&gt; sum([i for i in xrange(100000)])<br />
4999950000L<br />
&gt;&gt;&gt; i<br />
99999<br />
&gt;&gt;&gt; sum(j for j in xrange(100000))<br />
4999950000L<br />
&gt;&gt;&gt; j<br />
Traceback (most recent call last):<br />
  File &#8220;&#8221;, line 1, in<br />
NameError: name &#8216;j&#8217; is not defined<br />
&gt;&gt;&gt;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
