Message: 1<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Date: Mon, 20 Sep 2010 15:55:58 +0200<br>
From: Christian Maeder &lt;<a href="mailto:Christian.Maeder@dfki.de">Christian.Maeder@dfki.de</a>&gt;<br>
Subject: Re: Proposal: Make intersperse lazier<br>
To: Daniel Fischer &lt;<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>&gt;<br>
Cc: <a href="mailto:libraries@haskell.org">libraries@haskell.org</a><br>
Message-ID: &lt;<a href="mailto:4C9767EE.902@dfki.de">4C9767EE.902@dfki.de</a>&gt;<br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
Am 17.09.2010 20:17, schrieb Daniel Fischer:<br>
&gt; Okay, if you don&#39;t then I do :)<br>
&gt; I&#39;ve benchmarked a couple of variants:<br>
&gt;<br>
&gt; module Interspersing where<br>
&gt;<br>
&gt; isgo :: a -&gt; [a] -&gt; [a]<br>
&gt; isgo _ [] = []<br>
&gt; isgo s (x:xs) = x : go xs<br>
&gt;   where<br>
&gt;     go [] = []<br>
&gt;     go (y:ys) = s : y: go ys<br>
&gt;<br>
&gt; isrec :: a -&gt; [a] -&gt; [a]<br>
&gt; isrec s l = case l of<br>
&gt;               [] -&gt; l<br>
&gt;               (x:r) -&gt; x : if null r then r else (s : isrec s r)<br>
&gt;<br>
<br>
[..]<br>
<br>
&gt; Results:<br>
&gt; With -O2:<br>
&gt; unsurprisingly, isgo and ispreplc have nearly identical means in each run,<br>
&gt; about 33.6 ms for the small benchmark and 153 ms for the large.<br>
&gt; isprepD is slightly slower, 33.9 ms resp 155 ms.<br>
&gt; isprepM and isprepT are a little slower again, 34.4 ms resp 157 ms.<br>
&gt; isrec lags behind, 43.4 ms resp. 193 ms.<br>
<br>
I also did some benchmarking. It made no difference if ones uses a<br>
global function &quot;prepend&quot; or the local &quot;go&quot; function. (Also prepend is<br>
not faster if written using a worker.)<br>
<br>
The function isrec seem to be rewritten to a form that does not test &quot;r&quot;<br>
twice:<br>
<br>
isrec2 :: a -&gt; [a] -&gt; [a]<br>
isrec2 s l = case l of<br>
  [] -&gt; l<br>
  x : r -&gt; myGo s x r<br>
<br>
myGo :: a -&gt; a -&gt; [a] -&gt; [a]<br>
myGo s x r = x : case r of<br>
    [] -&gt; r<br>
    y : t -&gt; s : myGo s y t<br>
<br>
(making myGo local makes it worse)<br>
<br>
myGo produces a non-empty list. Therefore it is safe to change<br>
the recursive call &quot;s : myGo s y t&quot; to &quot;(s :) $! myGo s y t&quot;.<br>
<br>
After this change or the change &quot;(s :) $! isrec s r&quot; in Daniel&#39;s isrec<br>
function, these function are almost as fast as isgo.<br>
<br>
Cheers Christian<br></blockquote><div><br>Which compiler/version are you using for your tests?  Is the behavior the same for other compilers/versions?  In particular, is it similar for ghc-6.10, ghc-6.12, and ghc-HEAD?<br>
<br>When benchmarking variants like this, I&#39;m always suspicious that my optimizations may be ghc-version-specific, since I&#39;ve had occasional experience to the contrary.<br><br>John Lato<br></div></div>