ghc and signal processing
Jeremy Shaw
jeremy.shaw at lindows.com
Sun Feb 22 18:49:44 EST 2004
Hello,
I was thinking about using haskell to do some 'realtime' audio signal
processing, and am trying to figure out how close to the holy grail of
"C" code I can get when it comes to speed. Currently, it looks like I
might be looking at running 10 times slower. Is there stuff I could do
to get better performance out of haskell?
In example 1, I create an unitialized 1.8MB array of type 'IOUArray
Int Word8', and write it to a file. This takes a mere 0.056 seconds
(on an Athlon 600).
module Main where
import Data.Array
import Data.Array.IO
import System.IO
main = do h <- openBinaryFile "test.b" WriteMode
a <- newArray_ (1,1800000)
hPutArray h a 1800000
In example 2, I apply a filter to the array (in this case, the filter
is just the id function). This takes around 1.06 seconds.
main = do h <- openBinaryFile "test.b" WriteMode
a <- newArray_ (1,1800000)
b <- mapArray id a
hPutArray h b 1800000
If I apply the 'filter' twice, the time increases to 2.58 seconds:
main = do h <- openBinaryFile "test.b" WriteMode
a <- newArray_ (1,1800000)
b <- mapArray id a
c <- mapArray id b
hPutArray h c 1800000
By comparison, the following c program runs in 0.10 seconds:
#include <stdio.h>
int main (int argc, char *argv[])
{
FILE *fp;
char *a;
char *b;
int i;
a = (char *)malloc (1800000);
b = (char *)malloc (1800000);
for (i = 0; i < 1800000; i++) {
b[i] = a[i];
}
fp = fopen("test.b","w");
fwrite(b, 1, 1800000, fp);
fclose(fp);
free(b);
free(a);
return 0;
}
I compile using ghc from cvs as follows:
ghc -O2 -fvia-C Test.hs -o test
I also tried just -O, and no -fvia-C, and the performance was the
same. Without -O, it was about 5 times slower.
I compiled the c code with:
gcc -O2 test.c -o test.
I am using ghc from cvs head and gcc 2.95 on FreeBSD.
Are there secret options I should enable on the compiler? Or perhaps
there is a faster way than using mapArray and unboxed arrays?
Thanks!
Jeremy Shaw.
More information about the Glasgow-haskell-users
mailing list