tor 2002-09-12 klockan 11.27 skrev Jan Kybic: > Hello, > I have another question regarding the optimisation of Haskell code: > I have a relatively inexpensive function generating a long list, imagine > something like (I simplified a lot): > > l = [ i*i*i | i <- [0..n] ] -- for very large n > > This long list is consumed several times in the program: > > x1 = f1 l > x2 = f2 x1 l > x3 = f3 x2 l > > I found that the list l is calculated just once and that the > computational time is dominated by the allocations and garbage > collection. I want to try to force l to be generated on-the-fly > every time it is needed, to see if it improves performance. > What is a good way to do it? Would something like > > unsafePerformIO $ return l > > do the job? Isn'it there any flag for the compiler (ghc) to suggest > this optimisation? Thank you for your feedback.
The easiest way is to make it a function
l _ = [ i*i*i | i <- [0..n] ] -- for very large n
x1 = f1 (l ())
x2 = f2 x1 (l ())
x3 = f3 x2 (l ())
() can be any value really.
Regards,
Martin
--
Martin Norb�ck [EMAIL PROTECTED]
Kapplandsgatan 40 +46 (0)708 26 33 60
S-414 78 G�TEBORG http://www.dtek.chalmers.se/~d95mback/
SWEDEN OpenPGP ID: 3FA8580B
signature.asc
Description: PGP signature
