efficiency question

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


Did you try strict +/-? In (,). I am just curious.

> -----Original Message-----
> From: Hal Daume III [mailto:hdaume@ISI.EDU]=20
> Sent: Friday, February 08, 2002 3:53 PM
> To: Konst Sushenko
> Cc: Jorge Adriano; haskell@haskell.org
> Subject: RE: efficiency question
>=20
>=20
> I've tried using a strict fold:
>=20
> foldl' f a []     =3D a
> foldl' f a (x:xs) =3D (foldl' f $! f a x) xs
>=20
> but that has no effect (or minimal effect).
>=20
> --
> Hal Daume III
>=20
>  "Computer science is no more about computers    | hdaume@isi.edu
>   than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
>=20
> On Fri, 8 Feb 2002, Konst Sushenko wrote:
>=20
> > >=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.
> >=20
> > My guess is that it is due to the laziness of the=20
> addition/subtraction
> > in (,)
> > _______________________________________________
> > Haskell mailing list
> > Haskell@haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell
> >=20
>=20
>=20