Skip to content

Commit

Permalink
Time the system PRNG, for the lulz.
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Sep 19, 2009
1 parent e5f6633 commit 74ea0e8
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions tests/T.hs
Expand Up @@ -9,7 +9,8 @@ import Control.Monad.ST
import Data.Time.Clock.POSIX
import Data.Array.Vector
import Statistics.RandomVariate
import System.Random.Mersenne
import qualified System.Random as R
import qualified System.Random.Mersenne as M

getTime :: IO Double
getTime = (fromRational . toRational) `fmap` getPOSIXTime
Expand All @@ -21,9 +22,9 @@ time act = do
e <- ret `seq` getTime
return (e-s, ret)

count = 250 * 1000000
count = 100 * 1000000

type T = Word32
type T = Double

--summ :: [T] -> T
--summ = foldl' (+) 0
Expand All @@ -36,6 +37,18 @@ loop n act = go 0 0
d <- act
go (i+1) (s+d)

system :: IO Double
system = do
gen <- R.getStdGen
let a :*: g = loop 0 0 gen
R.setStdGen g
return a
where loop !i !a !g | i == k = a :*: g
| otherwise = let (b,h) = R.random g
in loop (i+1) (a+b) h
k = count `div` 100


mwc :: Word32 -> IO T
mwc k = return $! runST go where
go = do
Expand All @@ -50,11 +63,13 @@ mwca k = return $! runST go where

mersenne :: IO T
mersenne = do
gen <- getStdGen
loop count (random gen)
gen <- M.getStdGen
loop count (M.random gen)

main = do
forM_ [1..3] $ \n -> do
putStr "system: "
print =<< time system
putStr "mwc: "
print =<< time (mwc n)
putStr "mwca: "
Expand Down

0 comments on commit 74ea0e8

Please sign in to comment.