
Hello Haskell-Cafe! I've read about Control.Parallel and wanted to give it a try. Here's what I wrote: ----------- import Control.Parallel import Data.List splitList :: [Integer] -> [[Integer]] splitList = unfoldr f where f [] = Nothing f ~x = Just (splitAt 30000 x) map' :: (a->b) -> [a] -> [b] map' f (x:xs) =fx `par` (seq mfxs (fx:mfxs)) where fx = f x mfxs = map' f xs map' f [] = [] product' :: [Integer] -> Integer product' = foldr1 (\x y -> seq x (x*y)) fak :: Integer -> Integer fak n = product' $ map' product' (splitList [1..n]) main = putStrLn $ seq (fak 1000000) "done" ----------- This computes 1000000!. This version takes 8m29.189s to execute. Replace foldr1 with foldr and that goes down to 7m4.315s. Replace product' with the Prelude product and it takes only 6m17.685s. Why is that so? I'm using ghc 6.8.1 on Mac OS X. Regards, Adrian

This computes 1000000!. This version takes 8m29.189s to execute. Replace foldr1 with foldr and that goes down to 7m4.315s. Replace product' with the Prelude product and it takes only 6m17.685s. Why is that so? I'm using ghc 6.8.1 on Mac OS X.
I'm guessing that the speedup with the Prelude product is because it's a left fold. foldr goes to the end of the list and starts applying backwards, up the thunks. A left fold just keeps an accumulator as it walks down. The foldr1 to foldr speedup might be because foldr1 has three cases, to foldr's two: http://haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-List.html#fol... AGL -- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org 650-283-9641
participants (2)
-
Adam Langley
-
Adrian Neumann