[Haskell-beginners] Parallelism?
Michael Craig
mkscrg at gmail.com
Thu Dec 1 06:50:15 CET 2011
I was writing some parallel code (asynchronous database writes for an event
logger, but that's besides the point), and it seemed like the parallelized
version (i.e. compiled with -threaded -with-rtsopts=-N2) wasn't running
fast enough. I boiled it down to a dead-simple test:
import Control.Concurrent
import Data.Time.Clock.POSIX
import System.Environment
main :: IO ()
main = do
n <- getArgs >>= return . read . head
t1 <- getPOSIXTime
work n
t2 <- getPOSIXTime
putStrLn $ show $ t2 - t1
putStrLn $ show $ (fromIntegral n :: Double)
/ (fromRational . toRational $ t2 - t1)
work :: Integer -> IO ()
work n = do
forkIO $ putStrLn $ seq (fact n) "Done"
putStrLn $ seq (fact n) "Done"
fact :: Integer -> Integer
fact 1 = 1
fact n = n * fact (n - 1)
(I know this is not the best way to time things but I think it suffices for
this test.)
Compiled with ghc --make -O3 test.hs, ./test 500000 runs for 74 seconds.
Compiling with ghc --make -O3 -threaded -with-rtsopts=-N, ./test 500000
runs for 82 seconds (and seems to be using 2 cpu cores instead of just 1,
on a 4-core machine). What gives?
Mike S Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111201/d3589684/attachment.htm>
More information about the Beginners
mailing list