[Haskell-cafe] Performance with do notation, mwc-random and unboxed vector

Dmitry Dzhus dima at dzhus.org
Mon Jun 11 11:38:56 CEST 2012


Hello everyone.

I wonder why using do notation with `<-` can ruin the performance.

In essence the problem is that, for some action `f :: m Double`,
running the code (in my case, `standard` from mwc-random).

    f

for million times is fast but the code

    do
      v <- f
      return v

is slower about a hundred times.

Consider this simple source where we generate an unboxed vector with million
pseudo-random numbers:

---- 8< -----
import qualified Data.Vector.Unboxed as VU

import System.Random.MWC
import System.Random.MWC.Distributions (standard)

count = 1000000

main = do
  g <- create
  e' <- VU.replicateM count $ standard g
  return ()
---- >8 -----

Being compiled with -O2, this runs for 0.052 s on my machine.

Changing the replicateM line to use do notation brings the runtime down to 11.257 s!
See below:

---- 8< -----
import qualified Data.Vector.Unboxed as VU

import System.Random.MWC
import System.Random.MWC.Distributions (standard)

count = 1000000

main = do
  g <- create
  e' <- VU.replicateM count $ do
           v <- standard g
           return v
  return ()
---- >8 -----

I don't quite understand why this happens. I'm using GHC 7.4.1 on Linux x86_64 system.

Compiling *both* versions with profiling enabled changes runtime to 5.673 sec,
which is exactly half the runtime of slow version without profiling, and this is awkward
(double calculations occuring in do block?).

Does anybody have an idea if this is a problem with my do, or with mwc-random, or with vector
(my notation disallowing efficient unboxing?).



More information about the Haskell-Cafe mailing list