<html><body>Thanks for the advice.&nbsp; After taking most of it it is faster.&nbsp; But it is still many times slower than it ought to be!&nbsp; This algorithm should be much faster than simply sorting the list, and yet it is more than twice as slow!<br><br>One note, you said:<br>""""<br>&gt; Increment length.<br>&gt;<br>&gt;&gt;    modifySTRef<br>&gt;&gt;     lengthRef<br>&gt;&gt;     (+1)<br><br>This will create a big thunk for the length, you should use<br><br>  oldLength &lt;- readSTRef lengthRef<br>  writeSTRef lengthRef $! oldLength + 1<br><br>(I'm not sure if a strict modifySTRef exists somewhere...)""""<br><p><br></p><p>Actually, replacing modifySTRef with that code is just a hair slower...&nbsp; Not sure why.</p><p><br></p><p>I'm attaching the super optimized version. Along with the profiler output.&nbsp; I just can't understand what is slow here :(</p><p><br></p><p>Timothy</p><p><br></p><p>---------- Původní zpráva ----------<br>Od: Felipe Almeida Lessa &lt;felipe.lessa@gmail.com&gt;<br>Datum: 3. 9. 2012<br>Předmět: Re: [Haskell-cafe] hstats median algorithm</p><blockquote>On Mon, Sep 3, 2012 at 11:18 AM, Felipe Almeida Lessa<br>&lt;felipe.lessa@gmail.com&gt; wrote:<br>&gt; Ditto for oldLen here.  Also, you can simplify this lambda a lot:<br>&gt;<br>&gt;   import Control.Applicative ((&lt;$&gt;))<br>&gt;<br>&gt;   \(oldLen, oldVal) -&gt;<br>&gt;     let newLen = oldLen + 1<br>&gt;         newVal = (number:) &lt;$&gt; oldVal<br>&gt;     in newLen `seq` newVal `seq` (newLen, newVal)<br><br>Or, using BangPatterns,<br><br>  \(oldLen, oldVal) -&gt;<br>    let !newLen = oldLen + 1<br>        !newVal = (number:) &lt;$&gt; oldVal<br>    in (newLen, newVal)<br><br>Cheers,<br><br>-- <br>Felipe.</blockquote></body></html>