Ackermann very inefficient with Haskell/GHC

后端 未结 7 911
小鲜肉
小鲜肉 2020-12-23 13:58

I try computing Ackermann(4,1) and there\'s a big difference in performance between different languages/compilers. Below are results on my

7条回答
  •  旧时难觅i
    2020-12-23 14:05

    This performance issue (except for GHC RTS bug obviously) seems to be fixed now on OS X 10.8 after Apple XCode update to 4.6.2. I can still reproduce it on Linux (I have been testing with GHC LLVM backend though), but not any more on OS X. After I updated the XCode to 4.6.2, the new version seems to have affected the GHC backend code generation for Ackermann substantially (from what I remember from looking at object dumps pre-update). I was able to reproduce the performance issue on Mac before XCode update - I don't have the numbers but they were surely quite bad. So, it seems that XCode update improved the GHC code generation for Ackermann.

    Now, both C and GHC versions are quite close. C code:

    int ack(int m,int n){
    
      if(m==0) return n+1;
      if(n==0) return ack(m-1,1);
      return ack(m-1, ack(m,n-1));
    
    }
    

    Time to execute ack(4,1):

    GCC 4.8.0: 2.94s
    Clang 4.1: 4s
    

    Haskell code:

    ack :: Int -> Int -> Int
    ack 0 n = n+1
    ack m 0 = ack (m-1) 1
    ack m n = ack (m-1) (ack m (n-1))
    

    Time to execute ack 4 1 (with +RTS -kc1M):

    GHC 7.6.1 Native: 3.191s
    GHC 7.6.1 LLVM: 3.8s 
    

    All were compiled with -O2 flag (and -rtsopts flag for GHC for RTS bug workaround). It is quite a head scratcher though. Updating XCode seems to have made a big difference with optimization of Ackermann in GHC.

提交回复
热议问题