2016-05-13 15 views
9

Ho il seguente codice. Il costo è di 1 secondo per l'argomento 1000000, ma costa 5 secondi se viene sostituito myEven con la funzione standard even. Ho controllato il codice, lo standard anche la funzione fa esattamente lo stesso di * myEven *.Perché la funzione 'pari' di Haskell rallenta il mio programma?

import Data.Word 
import Data.List 
import System.Environment 

collatzNext :: Word32 -> Word32 
collatzNext a = (if myEven a then a else 3*a+1) `div` 2 

myEven :: (Integral a) => a -> Bool 
myEven a = (a `rem` 2) == 0 

collatzLen :: Word32 -> Int 
collatzLen a0 = length $ takeWhile (/= 1) $ iterate collatzNext a0 

main = do 
    [a0] <- getArgs 
    let max_a0 = (read a0)::Word32 
    print $ maximum $ map (\a0 -> (collatzLen a0, a0)) [1..max_a0] 
+2

solo per essere sicuri - avete letto la [info-page] (https://stackoverflow.com/tags/haskell/info) e compilato il programma con l'opzione '-O2'? – epsilonhalbe

+2

@epsilonhalbe: Questa è una regressione del compilatore (bene, libreria) in 7.10. È stato risolto nella versione 8.0, tuttavia: https://ghc.haskell.org/trac/ghc/ticket/11701 – Zeta

risposta

12

Se si aggiunge {-# NOINLINE myEven #-}, si otterrà lo stesso rallentamento. Il problema è che myEven è definito localmente, quindi la fonte è disponibile per il compilatore ed è in linea. Tutti allocazioni e funzione conversazione viene eliminato:

Main.$wgo1 [InlPrag=[0], Occ=LoopBreaker] 
    :: GHC.Prim.Word# -> GHC.Prim.Int# -> GHC.Prim.Int# 
[GblId, Arity=2, Caf=NoCafRefs, Str=DmdType <S,1*U><L,U>] 
Main.$wgo1 = 
    \ (ww_s6n0 :: GHC.Prim.Word#) (ww1_s6n4 :: GHC.Prim.Int#) -> 
    case ww_s6n0 of wild_X2j { 
     __DEFAULT -> 
     case GHC.Prim.remWord# wild_X2j (__word 2) of _ [Occ=Dead] { 
      __DEFAULT -> 
      Main.$wgo1 
       (GHC.Prim.quotWord# 
       (GHC.Prim.narrow32Word# 
        (GHC.Prim.plusWord# 
         (GHC.Prim.narrow32Word# (GHC.Prim.timesWord# (__word 3) wild_X2j)) 
         (__word 1))) 
       (__word 2)) 
       (GHC.Prim.+# ww1_s6n4 1); 
      __word 0 -> 
      Main.$wgo1 
       (GHC.Prim.quotWord# wild_X2j (__word 2)) (GHC.Prim.+# ww1_s6n4 1) 
     }; 
     __word 1 -> ww1_s6n4 
    } 

Ma even è definito in altro modulo e non è contrassegnato come INLINE o INLINEABLE. Di conseguenza non è inline, e ogni chiamata a even alloca inscatolato Word32:

Main.$wgo1 [InlPrag=[0], Occ=LoopBreaker] 
    :: GHC.Prim.Word# -> GHC.Prim.Int# -> GHC.Prim.Int# 
[GblId, Arity=2, Str=DmdType <S,U><L,U>] 
Main.$wgo1 = 
    \ (ww_s6mz :: GHC.Prim.Word#) (ww1_s6mD :: GHC.Prim.Int#) -> 
    case ww_s6mz of wild_X1W { 
     __DEFAULT -> 
     case even 
       @ Word32 GHC.Word.$fIntegralWord32 (GHC.Word.W32# wild_X1W) 
     of _ [Occ=Dead] { 
      False -> 
      Main.$wgo1 
       (GHC.Prim.quotWord# 
       (GHC.Prim.narrow32Word# 
        (GHC.Prim.plusWord# 
         (GHC.Prim.narrow32Word# (GHC.Prim.timesWord# (__word 3) wild_X1W)) 
         (__word 1))) 
       (__word 2)) 
       (GHC.Prim.+# ww1_s6mD 1); 
      True -> 
      Main.$wgo1 
       (GHC.Prim.quotWord# wild_X1W (__word 2)) (GHC.Prim.+# ww1_s6mD 1) 
     }; 
     __word 1 -> ww1_s6mD 
    } 

noti che evenis specialized per Int e Integer, ma non per Word32, in modo che il problema non si verifica se si utilizza Int.

+5

Yup. Nota che questo sarà risolto [in 8.0] (https://ghc.haskell.org/trac/ghc/ticket/11701). – Zeta

+0

@ Yuras: Grazie. Se modificato in Int, tempo di esecuzione ridotto da 5 a 1,8 s. –

Problemi correlati