[Haskell-cafe] Performance problem with random numbers

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Oct 13 09:56:33 EDT 2007

On Oct 13, 2007, at 6:52 , ntupel wrote:

> On Fri, 2007-10-12 at 20:25 -0700, Stefan O'Rear wrote:
>> On Sat, Oct 13, 2007 at 12:09:57AM +0200, ntupel wrote:
>>> setup :: (Ord a, IArray a2 a, IArray a1 e, Num a) => [e] -> [a] - 
>>> > (a1 Int e, a1 Int e, a2 Int a)
>>> calcAlias :: (Ord e, Num e, IArray a e, Ix i, IArray a2 e1,  
>>> IArray a1 e1) => a2 i e1 -> a1 i e1 -> a i e -> [i] -> [i] -> (a1  
>>> i e1, a i e)
>>> next :: (IArray a2 e1, IArray a e1, Ord e, IArray a1 e, RandomGen  
>>> t, Random e) => (a Int e1, a2 Int e1, a1 Int e) -> t -> (e1, t)
>>> randomList :: (Random e, RandomGen t1, IArray a2 e, Ord e, IArray  
>>> a t, IArray a1 t) => (a Int t, a1 Int t, a2 Int e) -> t1 -> [t]
> ...
>> I would try specializing to StdGen, UArray, and Int, for RandomGen,
>> IArray, and Random respectively.
> Thanks for your reply Stefan. Unfortunately I could measure only a
> relatively small improvement by changing to concrete types, e.g. using
> COST CENTRE                    MODULE               %time %alloc
> random                         Random                60.0   54.5
> randomR                        Random                20.0   23.3
> next                           Random                17.5   17.0
> main                           Main                   1.9    2.5
> randomList                     Random                 0.6    2.8

Now you need to start forcing things; given laziness, things tend to  
only get forced when in IO, which leads to time being accounted to  
the routine where the forcing happened.  If random / randomR are  
invoked with large unevaluated thunks, their forcing will generally  
be attributed to them, not to functions within the thunks.

(Yes, this means profiling lazy programs is a bit of a black art.)

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

More information about the Haskell-Cafe mailing list