<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ikke&#039;s blog &#187; performance</title>
	<atom:link href="http://eikke.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://eikke.com</link>
	<description>&#039;cause this is what I do</description>
	<lastBuildDate>Sun, 13 Feb 2011 14:58:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Python &#8216;all&#8217; odity</title>
		<link>http://eikke.com/python-all-odity/</link>
		<comments>http://eikke.com/python-all-odity/#comments</comments>
		<pubDate>Thu, 01 May 2008 13:57:00 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eikke.com/python-all-odity/</guid>
		<description><![CDATA[[update] Question solved, see bottom of post. Since Python 2.5 the language got a new built-in method &#8216;all&#8217; (and it&#8217;s nephew &#8216;any&#8217;). I wanted to play around with this a little, combined with generators, so I created a little testcase to test performance. Here&#8217;s the test-case: take a list L of X random numbers in [...]]]></description>
			<content:encoded><![CDATA[<p>[update] Question solved, see bottom of post.</p>
<p>Since Python 2.5 the language got a new built-in method &#8216;all&#8217; (and it&#8217;s nephew &#8216;any&#8217;). I wanted to play around with this a little, combined with generators, so I created a little testcase to test performance.</p>
<p>Here&#8217;s the test-case: take a list L of X random numbers in a given range [A, B], and check whether</p>
<ul>
<li>all elements in L are &gt;= A</li>
<li>all elements in L are &gt;= (A + Z) where Z is a number in [0, (B - A)]</li>
</ul>
<p>The first test should always result True, the second test could result to False.</p>
<p>Here&#8217;s the output of a test-run:</p>
<pre>In [1]: import random, sys

In [2]: a = [random.randint(100, sys.maxint) for i in xrange(2000000)]

In [3]: len(a)
Out[3]: 2000000

In [4]: #Check whether all elements are &gt;= 100 

In [5]: %timeit all(i &gt;= 100 for i in a)
10 loops, best of 3: 515 ms per loop

In [6]: %timeit any(i &lt; 100 for i in a)
10 loops, best of 3: 454 ms per loop

In [7]: def f(l):
   ...:     for i in l:
   ...:         if i &lt; 100:
   ...:             return False
   ...:     return True
   ...: 

In [8]: %timeit f(a)
10 loops, best of 3: 292 ms per loop

In [9]: #Same thing for 100000, since now the list shouldn't be completely iterated

In [10]: %timeit all(i &gt;= 100000 for i in a)
100 loops, best of 3: 4.73 ms per loop

In [11]: %timeit any(i &lt; 100000 for i in a)
100 loops, best of 3: 4.29 ms per loop

In [12]: def g(l):
   ....:     for i in l:
   ....:         if i &lt; 100000:
   ....:             return False
   ....:     return True
   ....: 

In [13]: %timeit g(a)
100 loops, best of 3: 2.82 ms per loop

In [14]: #For reference

In [15]: %timeit False in (i &gt;= 100 for i in a)
10 loops, best of 3: 531 ms per loop

In [16]: %timeit False in (i &gt;= 100000 for i in a)
100 loops, best of 3: 5.03 ms per loop</pre>
<p>It&#8217;s as if &#8216;all&#8217;, &#8216;any&#8217; or &#8216;in&#8217; don&#8217;t break/return when a first occurence of False (or True, obviously) is found. Is this the desired behaviour, and if it is, why? The calculation time difference between using all/any/in or a custom-made function (which is, unlike all etc, not written in C) which breaks whenever it can, is pretty astonishing.</p>
<p>[update] Question solved. It&#8217;s pretty normal the function-based approach performs better, since it combines what &#8216;all&#8217; and the generator provided to &#8216;all&#8217; do, taking away the generator function-call overhead. Damn <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/python-all-odity/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

