在Haskell中生成十亿随机双精度的最快方法

时间:2014-09-24 18:57:32

标签: performance haskell random

我正在进行蒙特卡罗模拟,目前正在使用System.Random

import System.Random
main = do
  g <- newStdGen
  let xs = randoms g :: [Double]
  -- normally, I'd do other magic here
  putStrLn $ show $ length $ take 10^9 xs

不幸的是,这需要很长时间,比Python random.random()慢至少5倍,更不用说C rand()调用了。

ghc -O2 -optc-ffast-math -optc-O3

import System.Random
main = do
  g <- newStdGen
  let xs = randoms h :: [Double]
  putStrLn $ show $ length $ take (10^7) xs

需要〜8s vs.(在iPython中)

import random
%timeit len([random.random() for _ in range(10 ** 7)])

需要约1.3秒。我的目标是10亿,但Haskell无法在合理的时间内生成它们。

我还有一个用rand()生成浮点数的C ++程序。它在0.2秒内进行10^7个样本。

如何在Haskell中快速生成[0-1)范围内的随机双打?

理想情况下,GHC生成的程序只会激活rand() POSIX调用并收集到列表中。最干净,最简单的答案最快的代码获胜。 (不,10%的代码加速1%是不值得的。)

1 个答案:

答案 0 :(得分:2)

这里的Mersenne虽然我们在不同的计算机上,但出乎意料地看起来比MWC更快并击败C ++ ;-)。很难看出它会买多少并行,但我最好还是回去工作。

{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -Wall                      #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing   #-}
{-# OPTIONS_GHC -fno-warn-type-defaults    #-}

import System.Random.Mersenne.Pure64

testUniform :: Int -> Double -> PureMT -> Double
testUniform 0 !x _ = x
testUniform n !x gen =
    testUniform (n - 1) (x + y) gen'
  where
    (y, gen') = randomDouble gen

n :: Int
n = 10^7

total :: Double
total = testUniform n 0 (pureMT $ fromIntegral arbSeed)

arbSeed :: Int
arbSeed = 8

mean :: Double
mean = total / fromIntegral n

main :: IO ()
main = print mean

~/Dropbox/Private/Stochastic $ ./MersennePure +RTS -s
0.4999607889729769
     802,924,992 bytes allocated in the heap
         164,240 bytes copied during GC
          44,312 bytes maximum residency (2 sample(s))
          21,224 bytes maximum slop
               1 MB total memory in use (0 MB lost due to fragmentation)

                                    Tot time (elapsed)  Avg pause  Max pause
  Gen  0      1634 colls,     0 par    0.00s    0.01s     0.0000s    0.0000s
  Gen  1         2 colls,     0 par    0.00s    0.00s     0.0001s    0.0002s

  INIT    time    0.00s  (  0.00s elapsed)
  MUT     time    0.11s  (  0.11s elapsed)
  GC      time    0.00s  (  0.01s elapsed)
  EXIT    time    0.00s  (  0.00s elapsed)
  Total   time    0.12s  (  0.12s elapsed)

  %GC     time       4.2%  (5.4% elapsed)

  Alloc rate    7,336,065,126 bytes per MUT second

  Productivity  95.7% of total user, 93.5% of total elapsed