我试图在 Haskell 中快速生成随机数据,但是当我尝试使用任何惯用方法时,我会得到低速和大 GC 开销。
这是短代码:
import qualified System.Random.Mersenne as RM
import qualified Data.ByteString.Lazy as BL
import qualified System.IO as SI
import Data.Word
main = do
r <- RM.newMTGen Nothing :: IO RM.MTGen
rnd <- RM.randoms r :: IO [Word8]
BL.hPutStr SI.stdout $ BL.pack rnd
这是快速代码:
import qualified System.Random.Mersenne as RM
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Binary.Put as DBP
import qualified System.IO as SI
import Data.List
import Control.Monad (void, forever)
import Data.Word
main = do
r <- RM.newMTGen Nothing :: IO RM.MTGen
forever $ do
x0 <- RM.random r :: IO Word32
x1 <- RM.random r :: IO Word32
x2 <- RM.random r :: IO Word32
x3 <- RM.random r :: IO Word32
x4 <- RM.random r :: IO Word32
x5 <- RM.random r :: IO Word32
x6 <- RM.random r :: IO Word32
x7 <- RM.random r :: IO Word32
x8 <- RM.random r :: IO Word32
x9 <- RM.random r :: IO Word32
xA <- RM.random r :: IO Word32
xB <- RM.random r :: IO Word32
xC <- RM.random r :: IO Word32
xD <- RM.random r :: IO Word32
xE <- RM.random r :: IO Word32
xF <- RM.random r :: IO Word32
c0 <- RM.random r :: IO Word32
c1 <- RM.random r :: IO Word32
c2 <- RM.random r :: IO Word32
c3 <- RM.random r :: IO Word32
c4 <- RM.random r :: IO Word32
c5 <- RM.random r :: IO Word32
c6 <- RM.random r :: IO Word32
c7 <- RM.random r :: IO Word32
c8 <- RM.random r :: IO Word32
c9 <- RM.random r :: IO Word32
cA <- RM.random r :: IO Word32
cB <- RM.random r :: IO Word32
cC <- RM.random r :: IO Word32
cD <- RM.random r :: IO Word32
cE <- RM.random r :: IO Word32
cF <- RM.random r :: IO Word32
v0 <- RM.random r :: IO Word32
v1 <- RM.random r :: IO Word32
v2 <- RM.random r :: IO Word32
v3 <- RM.random r :: IO Word32
v4 <- RM.random r :: IO Word32
v5 <- RM.random r :: IO Word32
v6 <- RM.random r :: IO Word32
v7 <- RM.random r :: IO Word32
v8 <- RM.random r :: IO Word32
v9 <- RM.random r :: IO Word32
vA <- RM.random r :: IO Word32
vB <- RM.random r :: IO Word32
vC <- RM.random r :: IO Word32
vD <- RM.random r :: IO Word32
vE <- RM.random r :: IO Word32
vF <- RM.random r :: IO Word32
b0 <- RM.random r :: IO Word32
b1 <- RM.random r :: IO Word32
b2 <- RM.random r :: IO Word32
b3 <- RM.random r :: IO Word32
b4 <- RM.random r :: IO Word32
b5 <- RM.random r :: IO Word32
b6 <- RM.random r :: IO Word32
b7 <- RM.random r :: IO Word32
b8 <- RM.random r :: IO Word32
b9 <- RM.random r :: IO Word32
bA <- RM.random r :: IO Word32
bB <- RM.random r :: IO Word32
bC <- RM.random r :: IO Word32
bD <- RM.random r :: IO Word32
bE <- RM.random r :: IO Word32
bF <- RM.random r :: IO Word32
BL.hPutStr SI.stdout $ DBP.runPut $ do
DBP.putWord32be x0
DBP.putWord32be x1
DBP.putWord32be x2
DBP.putWord32be x3
DBP.putWord32be x4
DBP.putWord32be x5
DBP.putWord32be x6
DBP.putWord32be x7
DBP.putWord32be x8
DBP.putWord32be x9
DBP.putWord32be xA
DBP.putWord32be xB
DBP.putWord32be xC
DBP.putWord32be xD
DBP.putWord32be xE
DBP.putWord32be xF
DBP.putWord32be c0
DBP.putWord32be c1
DBP.putWord32be c2
DBP.putWord32be c3
DBP.putWord32be c4
DBP.putWord32be c5
DBP.putWord32be c6
DBP.putWord32be c7
DBP.putWord32be c8
DBP.putWord32be c9
DBP.putWord32be cA
DBP.putWord32be cB
DBP.putWord32be cC
DBP.putWord32be cD
DBP.putWord32be cE
DBP.putWord32be cF
DBP.putWord32be v0
DBP.putWord32be v1
DBP.putWord32be v2
DBP.putWord32be v3
DBP.putWord32be v4
DBP.putWord32be v5
DBP.putWord32be v6
DBP.putWord32be v7
DBP.putWord32be v8
DBP.putWord32be v9
DBP.putWord32be vA
DBP.putWord32be vB
DBP.putWord32be vC
DBP.putWord32be vD
DBP.putWord32be vE
DBP.putWord32be vF
DBP.putWord32be b0
DBP.putWord32be b1
DBP.putWord32be b2
DBP.putWord32be b3
DBP.putWord32be b4
DBP.putWord32be b5
DBP.putWord32be b6
DBP.putWord32be b7
DBP.putWord32be b8
DBP.putWord32be b9
DBP.putWord32be bA
DBP.putWord32be bB
DBP.putWord32be bC
DBP.putWord32be bD
DBP.putWord32be bE
DBP.putWord32be bF
短代码在我的计算机上每秒输出大约 6 兆字节的随机字节。快速代码 - 每秒大约 150 兆字节。
如果我在快速代码中将变量的数量从 64 个减少到 16 个,速度会下降到大约 78 兆字节每秒。
如何在不减慢速度的情况下使这段代码紧凑和惯用?