efficiency question

Konst Sushenko konsu@microsoft.com
Fri, 8 Feb 2002 15:45:00 -0800


>=20
> On Friday 08 February 2002 22:14, you wrote:
> > define
> >
> > test1 l =3D
> >     let s1 =3D foldr (+) 1 l
> >         s2 =3D foldr (-) 1 l
> >     in  (s1, s2)
> >
> > test2 l =3D
> >     let s =3D 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=20
> (of course),
> > but test2 is still much slower.
> >
> > i *expected* test2 to be much faster because you're only=20
> traversing the
> > list once.  presumably the two elements "a" and "b" in=20
> test2 could be put
> > in registers and i'd imagine test2 should be faster (it=20
> certainly would be
> > if written in c).
>=20
> I'd say that's because in the second case you also got to=20
> apply the (,),=20
> besides the (+)/(-) constructor during the transversing...
> Am I right?
>=20
> J.A.

My guess is that it is due to the laziness of the addition/subtraction
in (,)