On Wed, 29 Sep 2004 15:58:24 -0700, Greg Buchholz <[EMAIL PROTECTED]> wrote:

<snip>

Just for the heck of it, I'd thought I'd try to write a naive 1-pass version of the program. It turned out to be 4.5 times slower than the original...

-- compiled with:  ghc -O2 -ddump-simpl -fvia-c  -o wc_fold wc_fold.hs

import IO

main = do   file <- getContents
            putStrLn (show (foldl wc (0,0,0) file))

wc :: (Int,Int,Int) -> Char -> (Int, Int, Int)
wc (l,w,c) '\n' = (l+1,w  ,c+1)
wc (l,w,c) ' '  = (l  ,w+1,c+1)
wc (l,w,c)  x   = (l  ,w  ,c+1)

use strictness is the trick to exploit the tail-recursion. There is possibly a better way but:

data C = C !Int !Int !Int deriving Show
wc' :: C  -> Char -> C
wc' (C l w c) '\n' = C (l+1) w     (c+1)
wc' (C l w c) ' '  = C l     (w+1) (c+1)
wc' (C l w c)  x   = C l     w     (c+1)

is significant faster. The results on a 12MB file:
original:
1,699,541,396 bytes allocated in the heap
340,796,888 bytes copied during GC
 75,415,872 bytes maximum residency (8 sample(s))
        146 Mb total memory in use
  Total time    5.92s  (  6.09s elapsed)

wc: (yours)
  I have not enough memory :-(
wc':
535,286,872 bytes allocated in the heap
187,340,032 bytes copied during GC
    135,696 bytes maximum residency (130 sample(s))
          2 Mb total memory in use
  Total time    2.45s  (  2.53s elapsed)

Cheers,
 Georg

--

---- Georg Martius,  Tel: (+49 34297) 89434 ----
------- http://www.flexman.homeip.net ---------
_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to