[Haskell-cafe] idea for avoiding temporaries

Claus Reinke claus.reinke at talk21.com
Mon Mar 12 11:03:28 EDT 2007


> to handle the 2d indexing, i replaced readArray m (i,j) by readMatrix m (i,j):
> 
>    {-# INLINE readMatrix #-}
>    readMatrix m ij = unsafeRead m (unsafeIndex matrixBounds ij)
> 
>    matrixBounds :: ((Int,Int),(Int,Int))
>    matrixBounds = ((1,1),(n,n))

i'm still trying to understand why unsafeIndex is so much faster than index.
to prepare for further experiments, i tried to include the default implementation
for method index in my source (which includes all the other optimisations discussed
here), as myindex:

    {-# INLINE readMatrix #-}
    readMatrix m ij = unsafeRead m (index matrixBounds ij)

    myindex b i | inRange b i = unsafeIndex b i 
                | otherwise   = error "Error in array index"

now, the measurement that confuses me is this:

    time ./CG array 100000

    readMatrix calls index:  16s
    readMatrix calls myindex:  9s

so just calling an in-module copy of the default code for index, with bounds-check,
is almost as fast as calling unsafeIndex, and almost twice as fast as calling the official
index..

can anyone explain this effect?
claus



More information about the Haskell-Cafe mailing list