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 <<a href="mailto:Christian.Maeder@dfki.de">Christian.Maeder@dfki.de</a>><br>
Subject: Re: Proposal: Make intersperse lazier<br>
To: Daniel Fischer <<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>><br>
Cc: <a href="mailto:libraries@haskell.org">libraries@haskell.org</a><br>
Message-ID: <<a href="mailto:4C9767EE.902@dfki.de">4C9767EE.902@dfki.de</a>><br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
Am 17.09.2010 20:17, schrieb Daniel Fischer:<br>
> Okay, if you don't then I do :)<br>
> I've benchmarked a couple of variants:<br>
><br>
> module Interspersing where<br>
><br>
> isgo :: a -> [a] -> [a]<br>
> isgo _ [] = []<br>
> isgo s (x:xs) = x : go xs<br>
> where<br>
> go [] = []<br>
> go (y:ys) = s : y: go ys<br>
><br>
> isrec :: a -> [a] -> [a]<br>
> isrec s l = case l of<br>
> [] -> l<br>
> (x:r) -> x : if null r then r else (s : isrec s r)<br>
><br>
<br>
[..]<br>
<br>
> Results:<br>
> With -O2:<br>
> unsurprisingly, isgo and ispreplc have nearly identical means in each run,<br>
> about 33.6 ms for the small benchmark and 153 ms for the large.<br>
> isprepD is slightly slower, 33.9 ms resp 155 ms.<br>
> isprepM and isprepT are a little slower again, 34.4 ms resp 157 ms.<br>
> 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 "prepend" or the local "go" 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 "r"<br>
twice:<br>
<br>
isrec2 :: a -> [a] -> [a]<br>
isrec2 s l = case l of<br>
[] -> l<br>
x : r -> myGo s x r<br>
<br>
myGo :: a -> a -> [a] -> [a]<br>
myGo s x r = x : case r of<br>
[] -> r<br>
y : t -> 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 "s : myGo s y t" to "(s :) $! myGo s y t".<br>
<br>
After this change or the change "(s :) $! isrec s r" in Daniel'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'm always suspicious that my optimizations may be ghc-version-specific, since I've had occasional experience to the contrary.<br><br>John Lato<br></div></div>