Performance problem with Euler problem and recursion on Int64 types

后端 未结 6 1499
[愿得一人]
[愿得一人] 2020-12-16 15:38

I\'m currently learning Haskell using the project Euler problems as my playground. I was astound by how slow my Haskell programs turned out to be compared to similar program

6条回答
  •  天涯浪人
    2020-12-16 16:26

    Hm, I installed a fresh Haskell platform with 7.0.3, and get roughly the following core for your program (-ddump-simpl):

    Main.$warcLength' =
      \ (ww_s1my :: GHC.Prim.Int64#) (ww1_s1mC :: GHC.Prim.Int64#)
        (ww2_s1mG :: GHC.Prim.Int64#) (ww3_s1mK :: GHC.Prim.Int64#) ->
        case {__pkg_ccall ghc-prim hs_gtInt64 [...]
               ww_s1my ww1_s1mC GHC.Prim.realWorld#
    [...]
    

    So GHC has realized that it can unpack your integers, which is good. But this hs_getInt64 call looks suspiciously like a C call. Looking at the assembler output (-ddump-asm), we see stuff like:

    pushl %eax
    movl 76(%esp),%eax
    pushl %eax
    call _hs_gtInt64
    addl $16,%esp
    

    So this looks very much like every operation on the Int64 get turned into a full-blown C call in the backend. Which is slow, obviously.

    The source code of GHC.IntWord64 seems to verify that: In a 32-bit build (like the one currently shipped with the platform), you will have only emulation via the FFI interface.

提交回复
热议问题