This is a multi-part message in MIME format.
--------------D60EAED940B337DE04784023
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Kevin Atkinson wrote:
> The file Main.hs contains a small test script demonstrating how
> PrimArrays can be faster than arrays with bound checking. Although it
> is difficult to tell, as the garbage collector gets in the way of my
> benchmarks, accumPrimArray is about 50% to 33% faster than the normal
> accumArray and my implementation of accumArray also seams to be a little
> faster than GHC implantation. I tried profiling the various accums
> however I can't seam to get meaningful results form the CVS version of
> ghc. (Is the profiler still not working correctly or is it me?).
After modifying Main.hs to test each of the accums one at a time it
seams that my accum takes about as much time as ghc accum and the
accumPrimArray is twice as fast as the other two.
As I suspected the fancy indexes and bound checking come at a high
price.
I attached the modified Main.hs so that you can try it out for yourself.
--
Kevin Atkinson
[EMAIL PROTECTED]
http://metalab.unc.edu/kevina/
--------------D60EAED940B337DE04784023
Content-Type: text/plain; charset=us-ascii;
name="Main.hs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="Main.hs"
module Main (main, prim, norm, orig, expr) where
import Prelude hiding (null, lookup)
import Mutable
import Assoc
import Container
import MutPrimArray
import MutAltArray
import STExtras
import Random
import System
import qualified Array
import Ix
import PrelBase
import PrelST
import PrimArrayDefn hiding (freeze', unsafeFreeze', thaw')
#ifdef __GLASGOW_HASKELL__
import Eval -- this should really not be necessary, however the CVS
-- version of ghc (June 27, 1999) requires it.
-- I think it is a bug.
import CPUTime
num :: Int
num = 100000
#define scc(n) _scc_ n
#else
getCPUTime :: IO Integer
getCPUTime = return 0
num :: Int
num = 1000
#define scc(n)
#endif
l :: [Int]
l = take num$ rs (mkStdGen 13)
rs g = case randomR (0,98) g of (x,g') -> x : rs g'
main :: IO ()
main = do bench$ print$ scc("gen") seq (sum l) "Evaluating l"
x <- getArgs
case head x of
"orig" -> bench$ scc("orig") orig l
"prim" -> bench$ scc("prim") prim l
"norm" -> bench$ scc("norm") norm l
bench com = do s <- getCPUTime
com
f <- getCPUTime
print$ (f-s) `div` (10^9)
prim l = do let a :: PrimArray Int Int
a = accumPrimArray (+) 0 100 $ zip l (repeat 1)
putStr "prim\n"
print$ elems a
norm l = do let a :: Array Int Int
a = accumArray (+) 0 (0,99) $ zip l (repeat 1)
putStr "norm\n"
print$ elems a
orig l = do let a :: Array.Array Int Int
a = Array.accumArray (+) 0 (0,99) $ zip l (repeat 1)
putStr "orig\n"
print$ Array.elems a
{-
expr l = do let els = do m <- mlistPrimArray 100 (take 100 $ repeat 0)
maccum (+) m (1,
melems m
putStr "expr\n"
print$ runST els
-}
expr l = do let a :: PrimArray Int Int
a = accumPrimArray (+) 0 100 [(1,a!2),(2,a!3),(3,a!4),(4,100)]
putStr "expr\n"
print$ elems a
maccum' f (M m _) l = ST$ \s -> (# z s l, () #)
where z s [] = s
z s ((I# ix,el):t) =
case readArray# m ix s of
(# s, x #) -> let r = case x `f` el of r -> seq r r
in case writeArray# m ix r s of
s -> z s t
mcount' (M m _) l = ST$ \s -> (# z s l, () #)
where z s [] = s
z s ((I# ix):t) =
case readArray# m ix s of
(# s, x #) -> case writeArray# m ix (x+1) s of
s -> z s t
--------------D60EAED940B337DE04784023--