efficiency question

Hal Daume III hdaume@ISI.EDU
Fri, 8 Feb 2002 15:52:46 -0800 (PST)


I've tried using a strict fold:

foldl' f a []     = a
foldl' f a (x:xs) = (foldl' f $! f a x) xs

but that has no effect (or minimal effect).

--
Hal Daume III

 "Computer science is no more about computers    | hdaume@isi.edu
  than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

On Fri, 8 Feb 2002, Konst Sushenko wrote:

> > 
> > On Friday 08 February 2002 22:14, you wrote:
> > > define
> > >
> > > test1 l =
> > >     let s1 = foldr (+) 1 l
> > >         s2 = foldr (-) 1 l
> > >     in  (s1, s2)
> > >
> > > test2 l =
> > >     let s = foldr (\x (a,b) -> (x+a,x-b)) (1,1) l
> > >     in  s
> > >
> > > why is test1 so much faster than test2 for long lists l (eg
> > > [1..1000000])?  replacing foldr with foldl makes it faster 
> > (of course),
> > > but test2 is still much slower.
> > >
> > > i *expected* test2 to be much faster because you're only 
> > traversing the
> > > list once.  presumably the two elements "a" and "b" in 
> > test2 could be put
> > > in registers and i'd imagine test2 should be faster (it 
> > certainly would be
> > > if written in c).
> > 
> > I'd say that's because in the second case you also got to 
> > apply the (,), 
> > besides the (+)/(-) constructor during the transversing...
> > Am I right?
> > 
> > J.A.
> 
> My guess is that it is due to the laziness of the addition/subtraction
> in (,)
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>