Performance problem with Euler problem and recursion on Int64 types

后端 未结 6 1514
[愿得一人]
[愿得一人] 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条回答
  •  猫巷女王i
    2020-12-16 16:31

    The normal optimization flag for performance concerned code is -O2. What you used, -O, does very little. -O3 doesn't do much (any?) more than -O2 - it even used to include experimental "optimizations" that often made programs notably slower.

    With -O2 I get performance competitive with Java:

    tommd@Mavlo:Test$ uname -r -m
    2.6.37 x86_64
    tommd@Mavlo:Test$ ghc --version
    The Glorious Glasgow Haskell Compilation System, version 7.0.3
    
    tommd@Mavlo:Test$ ghc -O2 so.hs
    [1 of 1] Compiling Main             ( so.hs, so.o )
    Linking so ...
    tommd@Mavlo:Test$ time ./so
    843298604
    
    real    0m4.948s
    user    0m4.896s
    sys     0m0.000s
    

    And Java is about 1 second faster (20%):

    tommd@Mavlo:Test$ time java ArcLength
    843298604
    3880
    
    real    0m3.961s
    user    0m3.936s
    sys     0m0.024s
    

    But an interesting thing about GHC is it has many different backends. By default it uses the native code generator (NCG), which we timed above. There's also an LLVM backend that often does better... but not here:

    tommd@Mavlo:Test$ ghc -O2 so.hs -fllvm -fforce-recomp
    [1 of 1] Compiling Main             ( so.hs, so.o )
    Linking so ...
    tommd@Mavlo:Test$ time ./so
    843298604
    
    real    0m5.973s
    user    0m5.968s
    sys     0m0.000s
    

    But, as FUZxxl mentioned in the comments, LLVM does much better when you add a few strictness annotations:

    $ ghc -O2 -fllvm -fforce-recomp so.hs
    [1 of 1] Compiling Main             ( so.hs, so.o )
    Linking so ...
    tommd@Mavlo:Test$ time ./so
    843298604
    
    real    0m4.099s
    user    0m4.088s
    sys     0m0.000s
    

    There's also an old "via-c" generator that uses C as an intermediate language. It does well in this case:

    tommd@Mavlo:Test$ ghc -O2 so.hs -fvia-c -fforce-recomp
    [1 of 1] Compiling Main             ( so.hs, so.o )
    
    on the commandline:
        Warning: The -fvia-c flag will be removed in a future GHC release
    Linking so ...
    ttommd@Mavlo:Test$ ti
    tommd@Mavlo:Test$ time ./so
    843298604
    
    real    0m3.982s
    user    0m3.972s
    sys     0m0.000s
    

    Hopefully the NCG will be improved to match via-c for this case before they remove this backend.

提交回复
热议问题