You are right to be "bugged" (see your last para). Fortunately,
ghc 2.09 (i.e. our current working copy) gives identical runtimes for
all three.
(Might be true of 2.08; I haven't tried.)
Simon
> I'm benchmarking MVar's and other shared memory abstractions, e.g. by
> accessing a variable a number of times:
>
>
> main :: IO ()
> main = do {
> v <- newMVar 0;
> access v 10000000
> } where access v 0 = print "done"
> -- 1) access v n = do {swapMVar v n; access v (n-1)}
> -- 2) access v n = do {takeMVar v; putMVar v n; access v (n-1)}
> -- 3) access v n = do {setVar v n; access v (n-1)}
>
> I'm accessing the MVar's according to 3 different schemes: one using swapMVar
> (1), the other using takeMVar and putMVar (2) and the third by calling a method of
>class
>
> Variable var where
> setVar :: Var a -> a -> IO ()
>
> instance Variable MVar where
> setVar mvar val = do {takeMVar mvar; putMVar mvar val}
>
> These are the results that I get when running the programs:
>
> 1) 42 secs
> 2) 16 secs
> 3) 32 secs
>
> It's quite surprising to see how much time swapMVar costs: I actually
> expected it to be faster than a take followed by a put. I have now
> spend some time getting rid of swapMVar wherever performance matters.
>
> One thing bugs me though: what is causing the overhead when I call the
> class operation, and - how can I get around it! This is quite
> important to the efficency of some of my basic abstractions such as
> reentrant monitors and the like.
Re: Optimizing Haskell programs is hard
Simon L Peyton Jones Fri, 17 Oct 1997 16:47:40 +0200 (MET DST)
- Optimizing Haskell programs is hard Einar Wolfgang Karlsen
- Simon L Peyton Jones
