Haskell:尾递归函数更慢?

时间:2014-12-05 16:49:23

标签: performance haskell recursion ghc tail

我看到了3个函数来计算https://www.haskell.org/haskellwiki/Performance/Accumulating_parameter

上列表的长度
len :: [a] -> Int    
len [] = 0    
len (x:xs) = len xs + 1   

len' :: [a] -> Int -> Int
len' [] acc = acc
len' (x:xs) acc = len' xs (1 + acc) 

len'' [] acc = acc
len'' (x:xs) acc = len'' xs $! (1 + acc)

在GHCi中,函数len最慢,len''最快。但是当我用GHC编译它时,函数len比len'快,这意味着非尾递归函数比尾递归函数更快。这是什么原因?

详细说明:
    len [1..10000000]:3.05s
    len'[1..10000000] 0:3.52s
    len [1..50000000]:13.67s
    len'[1..50000000] 0:16.13s

0 个答案:

没有答案