<br><br><div><span class="gmail_quote">On 2/10/07, <b class="gmail_sendername"><a href="mailto:apfelmus@quantentunnel.de">apfelmus@quantentunnel.de</a></b> &lt;<a href="mailto:apfelmus@quantentunnel.de">apfelmus@quantentunnel.de
</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Creighton Hogg wrote:<br>&gt; Hello Haskell-ers,<br>&gt; So a friend and I were thinking about making code faster in Haskell, and I
<br>&gt; was wondering if there was a way to improve the following method of<br>&gt; generating the list of all prime numbers.&nbsp;&nbsp;It takes about 13 seconds to<br>&gt; run, meanwhile my friend&#39;s C version took 0.1.<br>&gt; I&#39;d love to learn a bit more about how to optimize Haskell code.
<br><br>Of course, the best optimization is a better algorithm. In case this is<br>what you&#39;re after, have a look at<br><br>&nbsp;&nbsp; Colin Runciman, Lazy Wheel Sieves and Spirals of Primes<br>&nbsp;&nbsp; <a href="http://citeseer.ist.psu.edu/runciman97lazy.html">
http://citeseer.ist.psu.edu/runciman97lazy.html</a></blockquote><div><br>&lt;snip helpfullness&gt; <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The glitches may have been unintentional, but the flaw intentionally<br>degrades performance: you should not use a strict all&#39; but the lazy<br><br>&nbsp;&nbsp;all prop = foldr (\y z -&gt; prop y &amp;&amp; z) True<br><br>from the Prelude. The point is that the lazy version stops inspecting
<br>the elements of the remaining list whenever (prop y) turns False for the<br>first time. This is because &amp;&amp; is lazy:<br><br>&nbsp;&nbsp;False &amp;&amp; x = False<br><br>for whatever x we supply. For example, take the list
<br><br>&nbsp;&nbsp;[True, False, True, True] ++ replicate 100 True<br><br>Here, all returns False after inspecting the first two elements while<br>all&#39; inspects every one of the 104 list elements just to return False<br>afterwards. As every second number is even, your all&#39; is busy wasting
<br>time like crazy.</blockquote><div><br>Okay, this is sinking in a good bit better, thank you.&nbsp; I think I&#39;ve had a conceptual block about what laziness really means.<br><br><br><br><br></div><br></div>