[Haskell] Haskell fast (?) arrays

Federico Squartini federico.squartini at googlemail.com
Tue May 1 07:59:01 EDT 2007


I was reading an old post where Hal Daume III was analyzing Haskell
performance for arrays.
He proposed a test program which initializes an array, reverse it a number
of times, and sums the contents.

So I wrote a c++ reference program, a naive haskell version using lists and
I also tweaked a little bit with the IOArray version, which should be the
fastest. Unfortunately there is a  huge performance gap. Haskell is slower
by a factor of ten, even when using imperative style.

C++
time ./arrayC
499
real    0m0.059s
user    0m0.044s
sys    0m0.008s

HASKELL - IOUArray
time ./IOMutArrayUnboxed
499
real    0m0.720s
user    0m0.571s
sys    0m0.019s

HASKELL - list
time ./list
499
real    0m1.845s
user    0m1.770s
sys    0m0.064s


Can anyone suggest a faster version (using whatever data structure)? I like
Haskell very much but I still have to figure out if the slowness of some
code is due to my lack of knowledge or to some intrinsic limitation of the
language (or libraries).

By the way, sorry for the poor quality of the code, I am not a computer
scientist.


-------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------
//compile with
//g++ -o arrayC arrayC.cc
#include <stdio.h>
#include < math.h>



int main()
{
  int array[500001];

  for (int i=0;i<=500000;i++)
    {
    array[i]=(19*i+23)%911;
    }
  int tmp=0;
  for (int cnt=0;cnt<12;cnt++)
    {
      for (int x=0;x<=250000;x++)
        {
          tmp=array[500000-x];
          array[500000-x]=array[x];
          array[x]=tmp;
        }
    }
  int result=0;
  for (int i=0;i<=500000;i++)
    {
      result=result+(array[i]%911);
    }
  result=result % 911;
  printf("%d",result);
  return 0;
}

---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------
-- compile with
-- ghc --make -o list list.hs
module Main
    where

testArray = [ (19*i+23) `mod` 911 |i <- [0..500000]]

sumArrayMod =  foldl (\x y -> (y+x) `mod` 911) 0

main = print $ sumArrayMod$
       reverse$ reverse$ reverse$ reverse$
       reverse$ reverse$ reverse$ reverse$
       reverse$ reverse$ reverse$ reverse$
       reverse$ reverse$ reverse$ reverse$
       testArray


---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------
-- compile with
-- ghc --make -o IOMutArrayUnboxed IOMutArrayUnboxed.hs
module Main
    where

import Monad
import Data.Array.IO
import Data.Array.MArray
import Data.Array.Unboxed

total, semiTotal ::Int
total= 500000
semiTotal=250000


testArray :: IO (IOUArray Int Int)
testArray = newListArray (0,total)  [(19*i+23) `mod` 911 |i <- [0..total]]


reverseArray :: IOUArray Int Int -> IO ()
reverseArray arr = mapM_  (\i -> do oldi <- readArray arr i
                                    oldj <- readArray arr (total-i)
                                    writeArray arr i oldj
                                    writeArray arr (total-i) oldi)
                   [0..semiTotal]

sumArrayMod :: IOUArray Int Int -> IO Int
sumArrayMod arr = foldM (\s i -> do x <- readArray arr i
                                                          return   $!(s+x)
`mod` 911) 0 [0..total]


main::IO()
main = testArray >>= \a ->
       reverseArray a >> reverseArray a >> reverseArray a >> reverseArray a
>>
       reverseArray a >> reverseArray a >> reverseArray a >> reverseArray a
>>
       reverseArray a >> reverseArray a >> reverseArray a >> reverseArray a
>>
       reverseArray a >> reverseArray a >> reverseArray a >> reverseArray a
>>
       sumArrayMod a >>=  print

---------------------------------------------------------------------------------------------------------

Federico
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20070501/2b1c9161/attachment-0001.htm


More information about the Haskell mailing list