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

MigMit miguelimo38 at yandex.ru
Mon Jun 11 12:09:01 CEST 2012


Well, it's not "do" notation, since replacing "standard g" with "standard g >>= return" gives the same poor performance. I wonder if it has something to do with error checking.

On 11 Jun 2012, at 13:38, Dmitry Dzhus wrote:

> 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?).
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list