[Haskell-cafe] Generate random UArray in constant memory space.

Vasyl Pasternak vasyl.pasternak at gmail.com
Tue Feb 9 18:48:26 EST 2010


Bryan,

mwc-random is really fast. But it eats to much memory. My previous
attempts were to reduce total number of allocations. This package made
this possible, but it increases total memory usage.

Here is the code:

import Text.Printf
import System.Random.MWC
import Control.Applicative
import System.Environment
import Data.Array.Vector
import Control.Monad.ST

randomListU size = runST $ flip uniformArray size =<< create

main = do
  [size] <- map read <$> getArgs
  printf "%d\n" $ (sumU (randomListU size) :: Int)

And sample tests:

./mwcvec 10000000 +RTS -s
-9198901858466039191
         165,312 bytes allocated in the heap
             688 bytes copied during GC
          17,184 bytes maximum residency (1 sample(s))
          19,680 bytes maximum slop
              78 MB total memory in use (1 MB lost due to fragmentation)


./mwcvec 100000000 +RTS -s
2242701120799374676
         165,704 bytes allocated in the heap
             688 bytes copied during GC
          17,184 bytes maximum residency (1 sample(s))
          19,680 bytes maximum slop
             764 MB total memory in use (12 MB lost due to fragmentation)

./mwcvec 1000000000 +RTS -s
mwcvec: out of memory (requested 8000634880 bytes)

I don't know exactly, but is this a normal behavior ?

Thank you,
Vasyl

2010/2/9 Bryan O'Sullivan <bos at serpentine.com>:
> On Tue, Feb 9, 2010 at 4:18 AM, Vasyl Pasternak <vasyl.pasternak at gmail.com>
> wrote:
>>
>> I tried to generate memory-efficient list of random numbers, so I've
>> used uvector library for this task.
>
> Use the mwc-random package. It provides a function that does exactly this,
> and produces better quality random numbers with much higher performance
> (1000x faster) than System.Random or even mersenne-random.


More information about the Haskell-Cafe mailing list