efficiency question

Hal Daume III hdaume@ISI.EDU
Fri, 8 Feb 2002 14:14:58 -0800 (PST)


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).

 - hal

--
Hal Daume III

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