Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, realize that GHCi is an interpreter, and it's not designed to be very fast. To get more useful results you should compile the code with optimizations enabled. This can make a huge difference.</p> <p>Also, for any serious benchmarking of Haskell code, I recommend using <a href="http://hackage.haskell.org/package/criterion" rel="noreferrer">criterion</a>. It uses various statistical techniques to ensure that you're getting reliable measurements.</p> <p>I modified your code to use criterion and removed the print statements so that we're not timing the I/O.</p> <pre><code>import Criterion.Main import Data.Vector as V vector :: IO (Vector Int) vector = do let vec = V.replicate 3000000 10 return vec sumit :: IO Int sumit = do vec &lt;- vector return $ V.sum vec main = defaultMain [bench "sumit" $ whnfIO sumit] </code></pre> <p>Compiling this with <code>-O2</code>, I get this result on a pretty slow netbook:</p> <pre class="lang-none prettyprint-override"><code>$ ghc --make -O2 Sum.hs $ ./Sum warming up estimating clock resolution... mean is 56.55146 us (10001 iterations) found 1136 outliers among 9999 samples (11.4%) 235 (2.4%) high mild 901 (9.0%) high severe estimating cost of a clock call... mean is 2.493841 us (38 iterations) found 4 outliers among 38 samples (10.5%) 2 (5.3%) high mild 2 (5.3%) high severe benchmarking sumit collecting 100 samples, 8 iterations each, in estimated 6.180620 s mean: 9.329556 ms, lb 9.222860 ms, ub 9.473564 ms, ci 0.950 std dev: 628.0294 us, lb 439.1394 us, ub 1.045119 ms, ci 0.950 </code></pre> <p>So I'm getting an average of just over 9 ms with a standard deviation of less than a millisecond. For the larger test case, I'm getting about 100ms.</p> <p>Enabling optimizations is especially important when using the <code>vector</code> package, as it makes heavy use of <em>stream fusion</em>, which in this case is able to eliminate the data structure entirely, turning your program into an efficient, tight loop.</p> <p>It may also be worthwhile to experiment with the new LLVM-based code generator by using <code>-fllvm</code> option. <a href="http://donsbot.wordpress.com/2010/02/21/smoking-fast-haskell-code-using-ghcs-new-llvm-codegen/" rel="noreferrer">It is apparently well-suited for numeric code</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload