[Haskell-cafe] Vedr: To my boss: The code is cool, but it is about 100 times slower than the old one...

Fixie Fixie fixie.fixie at rocketmail.com
Thu Nov 29 20:57:31 CET 2012


Sure, the code is from the url I mentioned, both in C and in Haskell.

It allready seems like the haskell-version should run fast to me - it uses unboxed arrays and even unsafe functions to make it run faster. Well, have a look:

C-version:

#include <stdint.h>#include <stdio.h>#define VDIM 100#define VNUM 100000uint64_t prng (uint64_t w){w ^=w <<13;w ^=w >>7;w ^=w <<17;return w;};void kahanStep (double *s,double *c,double x){double y,t;y =x -*c;t =*s +y;*c =(t -*s)-y;*s =t;}void kahan(double s[],double c[]){for (int i =1;i <=VNUM;i++){uint64_t w =i;for (int j =0;j <VDIM;j++){kahanStep(&s[j],&c[j],w);w =prng(w);}}};int main (int argc,char*argv[]){double acc[VDIM],err[VDIM];for (int i =0;i <VDIM;i++){acc[i]=err[i]=0.0;};kahan(acc,err);printf("[ ");for (int i =0;i <VDIM;i++){printf("%g ",acc[i]);};printf("]\n");};

And the haskell version:

{-# LANGUAGE CPP, BangPatterns #-}moduleMain (main)where#define VDIM 100#define VNUM 100000importData.Array.Base importData.Array.ST importData.Array.Unboxed importControl.Monad.ST importGHC.Word importControl.Monad importData.Bits prng ::Word ->Word
prng w =w' where!w1 =w `xor`(w `shiftL`13)!w2 =w1 `xor`(w1 `shiftR`7)!w' =w2 `xor`(w2 `shiftL`17)typeVec s =STUArray s Int Double kahan ::Vec s ->Vec s ->ST s ()kahan s c =doletinner w j |j <VDIM =do!cj <-unsafeRead c j !sj <-unsafeRead s j let!y =fromIntegral w -cj !t =sj +y !w' =prng w unsafeWrite c j ((t-sj)-y)unsafeWrite s j t inner w' (j+1)|otherwise =return ()forM_ [1..VNUM]$\i ->inner (fromIntegral i)0calc ::ST s (Vec s)calc =dos <-newArray (0,VDIM-1)0c <-newArray (0,VDIM-1)0kahan s c return s main ::IO ()main =print .elems $runSTUArray calc

Cheers,

Felix


________________________________
 Fra: Clark Gaebel <cgaebel at uwaterloo.ca>
Til: Alfredo Di Napoli <alfredo.dinapoli at gmail.com> 
Kopi: Fixie Fixie <fixie.fixie at rocketmail.com>; "haskell-cafe at haskell.org" <haskell-cafe at haskell.org> 
Sendt: Torsdag, 29. november 2012 20.17
Emne: Re: [Haskell-cafe] To my boss: The code is cool, but it is about 100 times slower than the old one...
 

If you can give an example of some underperforming code, I'm sure someone (or several people) on this list would be more than happy to help you make it more performant.

Generally, it doesn't take much. It's all in knowing where to look. Also, if you know performance is key, you should be using the performance-oriented data structures (ByteString, Text, Vector) from the very beginning. Personally, I never find myself using Data.Array, and rarely use String in real code. It's just not worth the performance headaches.

And finally, depending on what you're doing, neither Haskell, nor C might be right for you! Especially with certain numerics-related code, you might find fortran, OpenCL, or CUDA easier to make performant.

Examples of this would be lovely.

  - Clark




On Thu, Nov 29, 2012 at 2:00 PM, Alfredo Di Napoli <alfredo.dinapoli at gmail.com> wrote:

Hi there,
>I'm only an amateur so just my 2 cent: Haskell can be really fast, but reaching that speed can be all but trivial: you need to use different data types (e.g. ByteString vs. the normal String type) relies on "unconventional" IO (e.g. Conduit, Iterateee) and still be ready to go "out of the base", using packages and functions wich are not in base/haskell platform (e.g. mwc-random).
>
>
>My 2 cents :)
>A.
>
>
>On 29 November 2012 18:09, Fixie Fixie <fixie.fixie at rocketmail.com> wrote:
>
>Hi all haskellers
>>
>>
>>I every now and then get the feeling that doing my job code in Haskell would be a good idea.
>>
>>
>>I have tried a couple of times, but each time I seem to run into performance problems - I do lots of heavy computing.
>>
>>
>>The problem seems to be connected to lazy loading, which makes my programs so slow that I really can not show them to anyone. I have tried all tricks in the books, like !, seq, non-lazy datatypes...
>>
>>
>>I was poking around to see if this had changed, then I ran into this forum post: http://stackoverflow.com/questions/9409634/is-indexing-of-data-vector-unboxed-mutable-mvector-really-this-slow
>>
>>
>>The last solution was a haskell program which was in the 3x range to C, which I think is ok. This was in the days of ghc 7.0
>>
>>
>>I then tried compile the programs myself (ghc 7.4.1), but found that now the C program now was more that 100x faster. The ghc code was compiled with both O2 and O3, giving only small differences on my 64-bit Linux box.
>>
>>
>>So it seems something has changed - and even small examples are still not safe when it comes to the lazy-monster. It reminds me of some code I read a couple of years ago where one of the Simons actually fired off a new thread, to make sure a variable was realized.
>>
>>
>>A sad thing, since I am More that willing to go for Haskell if proves to be usable. If anyone can see what is wrong with the code (there are two haskell versions on the page, I have tried the last and fastest one) it would also be interesting.
>>
>>
>>What is your experience, dear haskellers? To me it seems this beautiful language is useless without a better lazy/eager-analyzer.
>>
>>
>>Cheers,
>>
>>
>>Felix
>>_______________________________________________
>>Haskell-Cafe mailing list
>>Haskell-Cafe at haskell.org
>>http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>
>_______________________________________________
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121129/7ea62e0d/attachment.htm>


More information about the Haskell-Cafe mailing list