Repository : ssh://darcs.haskell.org//srv/darcs/packages/random On branch : master
http://hackage.haskell.org/trac/ghc/changeset/290fc9f6a2b0a790883f282f6da60d997a11e8f6 >--------------------------------------------------------------- commit 290fc9f6a2b0a790883f282f6da60d997a11e8f6 Author: Ryan Newton <[email protected]> Date: Sun Jun 26 23:47:13 2011 -0400 Rearranged module organization for Benchmark. Added Makefile and more comparison against other RNGs on hackage. >--------------------------------------------------------------- Benchmark/BinSearch.hs | 2 +- Benchmark/Makefile | 24 ++++++++++++++++++++++ Benchmark/SimpleRNGBench.hs | 46 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/Benchmark/BinSearch.hs b/Benchmark/BinSearch.hs index 3118391..6a4901f 100644 --- a/Benchmark/BinSearch.hs +++ b/Benchmark/BinSearch.hs @@ -2,7 +2,7 @@ -- This is a script used for timing the throughput of benchmarks that -- take one argument and have linear complexity. -module Benchmark.BinSearch +module BinSearch ( binSearch ) diff --git a/Benchmark/Makefile b/Benchmark/Makefile new file mode 100644 index 0000000..8a84e64 --- /dev/null +++ b/Benchmark/Makefile @@ -0,0 +1,24 @@ + + +#OPTS= -O2 -Wall -XCPP +OPTS= -O2 -Wall -XCPP -Werror + +all: lib bench + +lib: + (cd .. && ghc $(OPTS) --make System/Random.hs) + +bench: + ghc $(OPTS) -i.. --make SimpleRNGBench.hs + +# When benchmarking against other packages installed via cabal it is +# necessary to IGNORE the System/Random.hs files in the current directory. +# (Otherwise instances of RandomGen are confused.) +bench2: + ghc $(OPTS) -DTEST_COMPETITORS --make SimpleRNGBench.hs + +clean: + rm -f *.o *.hi SimpleRNGBench +# cabal clean +# (cd Benchmark/ && rm -f *.o *.hi SimpleRNGBench) +# (cd System/ && rm -f *.o *.hi SimpleRNGBench) diff --git a/Benchmark/SimpleRNGBench.hs b/Benchmark/SimpleRNGBench.hs index fcaed80..cc92d86 100644 --- a/Benchmark/SimpleRNGBench.hs +++ b/Benchmark/SimpleRNGBench.hs @@ -29,11 +29,14 @@ import Foreign.C.Types import Foreign.ForeignPtr import Foreign.Storable (peek,poke) -import Benchmark.BinSearch import Prelude hiding (last,sum) +import BinSearch #ifdef TEST_COMPETITORS -import System.Random.Mersenne.Pure64 (pureMT) +import System.Random.Mersenne.Pure64 +import System.Random.MWC +import Control.Monad.Primitive +import System.IO.Unsafe #endif ---------------------------------------------------------------------------------------------------- @@ -107,7 +110,17 @@ instance SplittableGen BinRNG where split (BinRNG g) = (BinRNG g1, BinRNG g2) where (g1,g2) = split g -mkBinRNG = BinRNG . mkStdGen + + +#ifdef TEST_COMPETITORS +data MWCRNG = MWCRNG (Gen (PrimState IO)) +-- data MWCRNG = MWCRNG GenIO +instance RandomGen MWCRNG where + -- For testing purposes we hack this to be non-monadic: + next g@(MWCRNG gen) = unsafePerformIO $ + do v <- uniform gen + return (v, g) +#endif ---------------------------------------------------------------------------------------------------- -- Drivers to get random numbers repeatedly. @@ -280,7 +293,27 @@ main = do timeit th freq "System.Random Integers" gen randInteger timeit th freq "System.Random Bools" gen randBool - putStrLn$ "\n Third, timing range-restricted System.Random.randomR:" +#ifdef TEST_COMPETITORS + putStrLn$ "\n Next test other RNG packages on Hackage:" + let gen_mt = pureMT 39852 + randInt2 = random :: RandomGen g => g -> (Int,g) + randFloat2 = random :: RandomGen g => g -> (Float,g) + timeit th freq "System.Random.Mersenne.Pure64 next" gen_mt next + timeit th freq "System.Random.Mersenne.Pure64 Ints" gen_mt randInt2 + timeit th freq "System.Random.Mersenne.Pure64 Floats" gen_mt randFloat2 + +-- gen_mwc <- create + withSystemRandom $ \ gen_mwc -> do + let randInt3 = random :: RandomGen g => g -> (Int,g) + randFloat3 = random :: RandomGen g => g -> (Float,g) + + timeit th freq "System.Random.MWC next" (MWCRNG gen_mwc) next + timeit th freq "System.Random.MWC Ints" (MWCRNG gen_mwc) randInt3 + timeit th freq "System.Random.MWC Floats" (MWCRNG gen_mwc) randFloat3 + +#endif + + putStrLn$ "\n Next timing range-restricted System.Random.randomR:" timeit th freq "System.Random Ints" gen (randomR (-100, 100::Int)) timeit th freq "System.Random Word16s" gen (randomR (-100, 100::Word16)) timeit th freq "System.Random Floats" gen (randomR (-100, 100::Float)) @@ -298,11 +331,6 @@ main = do -- timeit_foreign th freq "rand/store in Haskell loop" (\n ptr -> forM_ [1..n]$ \_ -> do n <- rand; poke ptr n ) -- return () -#ifdef TEST_COMPETITORS - let gen_mt = pureMT 39852 - timeit th freq "System.Random.Mersenne.Pure64 next" gen_mt next -#endif - -- Test with 1 thread and numCapabilities threads: gamut 1 when (numCapabilities > 1) $ do _______________________________________________ Cvs-libraries mailing list [email protected] http://www.haskell.org/mailman/listinfo/cvs-libraries
