Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance evalRandIO的怪异表演_Performance_Haskell_Random - Fatal编程技术网

Performance evalRandIO的怪异表演

Performance evalRandIO的怪异表演,performance,haskell,random,Performance,Haskell,Random,我在evalRandIO上遇到了一些奇怪的性能问题。以下是违规代码: import Control.Monad.Random inf :: (RandomGen g, Random a) => Rand g [a] inf = sequence $ repeat $ getRandom many :: (RandomGen g, Random a) => Int -> Rand g [a] many n = sequence $ replicate n $ getRando

我在evalRandIO上遇到了一些奇怪的性能问题。以下是违规代码:

import Control.Monad.Random

inf :: (RandomGen g, Random a) => Rand g [a]
inf = sequence $ repeat $ getRandom

many :: (RandomGen g, Random a) => Int -> Rand g [a]
many n = sequence $ replicate n $ getRandom

main = do
  m <- evalRandIO $ many 1000000 :: IO [Bool]
  i <- evalRandIO $ inf :: IO [Bool]
  putStrLn $ show $ take 5 m 
  putStrLn $ show $ take 5 i

无法在MacOS X 10.6.4上复制:

botanix:~ stolz$ ghci rand.hs 
GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling Main             ( rand.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Loading package old-locale-1.0.0.2 ... linking ... done.
Loading package time-1.1.4 ... linking ... done.
Loading package random-1.0.0.2 ... linking ... done.
Loading package transformers-0.2.2.0 ... linking ... done.
Loading package mtl-2.0.1.0 ... linking ... done.
Loading package MonadRandom-0.1.6 ... linking ... done.
[True,False,False,True,True]
[False,True,False,True,False]
更新:D'oh,编译时行为不同,所以我吃我的话:

botanix:~ stolz$ ./a.out 
[False,True,True,False,True]
Stack space overflow: current size 8388608 bytes.

答案比看起来要简单。当你写作时:

 m <- evalRandIO $ many 1000000 :: IO [Bool]

m我已经在ghci和ghc版本6.12.3中试用过了。我使用的是OSX10.6.Hm,这里是10.6.4。图书馆的版本如何?您可以发布ghci运行的完整输出吗?我得到的结果与nitromaster101、GHC 7.0.2、Linux相同。@Tener:结果是“什么都没发生,我按了Ctrl-C”,还是堆栈溢出?堆栈溢出。我认为“什么都没发生”是不对的,程序实际上是在计算种子值。好的,你中断了运行。只是为了好玩,你能让它运行直到它自己退出吗(如果它真的退出的话)?程序在毫无限制地消耗内存。是的,我在想这是否和中的问题一样。但这不能解释堆栈溢出。当然,重复重新播种应该使用恒定的堆栈空间。我想知道是否有泄漏的地方,但我不能使剖析足够详细。
botanix:~ stolz$ ./a.out 
[False,True,True,False,True]
Stack space overflow: current size 8388608 bytes.
 m <- evalRandIO $ many 1000000 :: IO [Bool]
import Control.Monad.Random

inf :: (RandomGen g, Random a) => Rand g [a]
inf = sequence $ repeat $ getRandom

many :: (RandomGen g, Random a) => Int -> Rand g [a]
many n = sequence $ replicate n $ getRandom

main = do
  newGen' <- newStdGen
  m <- evalRandIO $ many 1000000 :: IO [Bool]
  setStdGen newGen'
  i <- evalRandIO $ inf :: IO [Bool]
  putStrLn $ show $ take 5 m 
  putStrLn $ show $ take 5 i