[commit: random] master: Fixed performance dent for random generating Ints. I can't explain why it was slower than other types using randomBounded, but this patch circumvents the problem by using next directly. (13c12c3)
Ian Lynagh
igloo at earth.li
Sun Jun 26 15:55:53 CEST 2011
Repository : ssh://darcs.haskell.org//srv/darcs/packages/random
On branch : master
http://hackage.haskell.org/trac/ghc/changeset/13c12c3bd2ccf5b079c90e148a6d1e896c0c60ed
>---------------------------------------------------------------
commit 13c12c3bd2ccf5b079c90e148a6d1e896c0c60ed
Author: Ryan Newton <rrnewton at gmail.com>
Date: Sun Jun 26 00:24:46 2011 -0400
Fixed performance dent for random generating Ints. I can't explain why it was slower than other types using randomBounded, but this patch circumvents the problem by using next directly.
>---------------------------------------------------------------
Benchmark/SimpleRNGBench.hs | 4 ++++
System/Random.hs | 5 +----
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/Benchmark/SimpleRNGBench.hs b/Benchmark/SimpleRNGBench.hs
index c5d3b9e..71f55f4 100644
--- a/Benchmark/SimpleRNGBench.hs
+++ b/Benchmark/SimpleRNGBench.hs
@@ -18,6 +18,7 @@ import Control.Monad
import Control.Exception
import Data.IORef
+import Data.Word
import Data.List hiding (last,sum)
import Data.Int
import Data.List.Split
@@ -226,6 +227,7 @@ main = do
let
randInt = random :: RandomGen g => g -> (Int,g)
+ randWord16 = random :: RandomGen g => g -> (Word16,g)
randFloat = random :: RandomGen g => g -> (Float,g)
randCFloat = random :: RandomGen g => g -> (CFloat,g)
randDouble = random :: RandomGen g => g -> (Double,g)
@@ -248,6 +250,7 @@ main = do
putStrLn$ "\n Second, timing System.Random.random at different types:"
timeit th freq "System.Random Ints" gen randInt
+ timeit th freq "System.Random Word16" gen randWord16
timeit th freq "System.Random Floats" gen randFloat
timeit th freq "System.Random CFloats" gen randCFloat
timeit th freq "System.Random Doubles" gen randDouble
@@ -257,6 +260,7 @@ main = do
putStrLn$ "\n Third, 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))
timeit th freq "System.Random CFloats" gen (randomR (-100, 100::CFloat))
timeit th freq "System.Random Doubles" gen (randomR (-100, 100::Double))
diff --git a/System/Random.hs b/System/Random.hs
index f8a7dd7..1364b98 100644
--- a/System/Random.hs
+++ b/System/Random.hs
@@ -285,7 +285,7 @@ instance Random Integer where
randomR ival g = randomIvalInteger ival g
random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
-instance Random Int where randomR = randomIvalIntegral; random = randomBounded
+instance Random Int where randomR = randomIvalIntegral; random = next
instance Random Int8 where randomR = randomIvalIntegral; random = randomBounded
instance Random Int16 where randomR = randomIvalIntegral; random = randomBounded
instance Random Int32 where randomR = randomIvalIntegral; random = randomBounded
@@ -355,15 +355,12 @@ instance Random Float where
rand = fromIntegral (mask24 .&. x)
:: Float
in
--- (rand / 2^24, rng')
(rand / fromIntegral twoto24, rng')
-- Note, encodeFloat is another option, but I'm not seeing slightly
-- worse performance with the following [2011.06.25]:
-- (encodeFloat rand (-24), rng')
where
mask24 :: Int
--- mask24 = 2^24 - 1
-
mask24 = twoto24 - 1
-- RRN: Note, in my tests [2011.06.25] this worked as well as using Data.Bit:
twoto24 = (2::Int) ^ (24::Int)
More information about the Cvs-libraries
mailing list