17 Oct 2012 09:07
poor performance when generating random text
Dmitry Vyal <akamaus <at> gmail.com>
2012-10-17 07:07:41 GMT
2012-10-17 07:07:41 GMT
Hello anyone
I've written a snippet which generates a file full of random strings.
When compiled with -O2 on ghc-7.6, the generation speed is about 2Mb per
second which is on par with interpreted php. That's the fact I find
rather disappointing. Maybe I've missed something trivial? Any
suggestions and explanations are welcome. :)
% cat ext_sort.hs
import qualified Data.Text as T
import System.Random
import Control.Exception
import Control.Monad
import System.IO
import qualified Data.Text.IO as TI
gen_string g = let (len, g') = randomR (50, 450) g
in T.unfoldrN len rand_text (len, g')
where rand_text (0,_) = Nothing
rand_text (k,g) = let (c, g') = randomR ('a','z') g
in Just (c, ((k-1), g'))
write_corpus file = bracket (openFile file WriteMode) hClose $ \h -> do
let size = 100000
sequence $ replicate size $ do
g <- newStdGen
let text = gen_string g
TI.hPutStrLn h text
(Continue reading)
RSS Feed