loop performance bug

Donald Bruce Stewart dons at cse.unsw.edu.au
Tue Mar 15 01:57:56 EST 2005


duncan.coutts:
> This sort of code runs very slowly when compared to the equivalent in C:

This example uses unboxing and primops over Lemmih's and seems to run a bit
faster:

    Lemmih's loops:
    ./a.out  1.35s user 0.00s system 99% cpu 1.359 total

    This code:
    ./a.out  0.99s user 0.00s system 98% cpu 1.008 total

    C:
    ./a.out  0.14s user 0.00s system 101% cpu 0.138 total

> import GHC.Base
> import GHC.IOBase
> import GHC.Prim
> import GHC.Pack
> 
> data M = M (MutableByteArray# RealWorld)
> 
> main = do
>     (M a) <- IO $ \s -> case newByteArray# (4#*#100#*#100#) s of (# s',ar #) -> (# s',M ar #)
>     doFromTo 0 9999 $ \_ ->
>       doFromTo 0 99 $ \y ->
>         doFromTo 0 99 $ \x -> do
>           IO $ \s ->
>             case writeIntArray# (unsafeCoerce# a) (x *# (y +# 1#)) s of
>                s' -> (# s', () #)
> 
> doFromTo (I# from) (I# to) action = do
>     let loop n | n ># to   = return ()
>                | otherwise = action n >> loop (n+#1#)
>     loop from

Changing the index calculation (x *# (y +# 1#) to a constant halves the
runtime, again, though that may not be very useful information ;)
 
-- Don



More information about the Glasgow-haskell-bugs mailing list