编辑以回答问题
我做对了吗?
几乎,正如 cmets 所说,您构建了一大堆 1+(1+(1+...)) - 改用严格的累加器或为您处理事情的更高级别的函数。还有其他一些小事情,比如定义一个函数来比较第二个元素,而不是使用maximumBy (comparing snd),但这更具风格。
按原样,是正确的 Haskell 方法吗?
这是可以接受的惯用 Haskell 代码。
我怎样才能让它更快?
请参阅我的以下基准。欧拉性能问题的最常见答案是:
- 使用 -O2(和以前一样)
- 尝试 -fllvm(GHC NCG 次优)
- 使用 worker/wrappers 来减少参数,或者在您的情况下,利用累加器。
- 使用快速/不可装箱的类型(如果您可以使用 Int 代替 Integer,则使用 Int64(如果需要可移植性等)。
- 当所有值都是正数时,使用
rem 而不是mod。对于您的情况,了解或发现 div 倾向于编译成比 quot 慢的东西也很有用。
另外,对于内存使用,递归实际上是如何在底层实现的?
至于它是如何使用内存的?
这两个问题都非常广泛。完整的答案可能需要解决惰性评估、尾调用优化、工作人员转换、垃圾收集等问题。我建议您随着时间的推移更深入地探索这些答案(或者希望这里有人给出我正在避免的完整答案)。
原始帖子 - 基准数字
原文:
$ ghc -O2 so.hs ; time ./so
[1 of 1] Compiling Main ( so.hs, so.o )
Linking so ...
(837799,525)
real 0m5.971s
user 0m5.940s
sys 0m0.019s
对collatzLength 使用带有累加器的辅助函数:
$ ghc -O2 so.hs ; time ./so
[1 of 1] Compiling Main ( so.hs, so.o )
Linking so ...
(837799,525)
real 0m5.617s
user 0m5.590s
sys 0m0.012s
使用Int 而不是默认为Integer - 使用类型签名也更容易阅读!
$ ghc -O2 so.hs ; time ./so
[1 of 1] Compiling Main ( so.hs, so.o )
Linking so ...
(837799,525)
real 0m2.937s
user 0m2.932s
sys 0m0.001s
使用rem 而不是mod:
$ ghc -O2 so.hs ; time ./so
[1 of 1] Compiling Main ( so.hs, so.o )
Linking so ...
(837799,525)
real 0m2.436s
user 0m2.431s
sys 0m0.001s
使用quotRem 而不是rem 然后div:
$ ghc -O2 so.hs ; time ./so
[1 of 1] Compiling Main ( so.hs, so.o )
Linking so ...
(837799,525)
real 0m1.672s
user 0m1.669s
sys 0m0.002s
这和之前的问题很像:Speed comparison with Project Euler: C vs Python vs Erlang vs Haskell
编辑:是的,正如 Daniel Fischer 所建议的那样,使用 .&. 和 shiftR 的位操作改进了 quotRem:
$ ghc -O2 so.hs ; time ./so
(837799,525)
real 0m0.314s
user 0m0.312s
sys 0m0.001s
或者您可以只使用 LLVM 并让它发挥作用(注意这个版本仍然使用 quotRem)
$ time ./so
(837799,525)
real 0m0.286s
user 0m0.283s
sys 0m0.002s
LLVM 实际上做得很好,只要你避免 mod 的丑陋,并使用 rem 或 even 优化基于守卫的代码,与手动优化的 .&. 和 @987654351 一样好@。
获得比原来快约 20 倍的结果。
编辑:人们很惊讶 quotRem 在Int 面前表现得和位操作一样好。代码已包含在内,但我不清楚这一惊喜:仅仅因为某些事情可能是负面的,并不意味着您无法使用非常相似的位操作来处理它,这些位操作在正确的硬件上可能具有相同的成本。 nextStep 的所有三个版本的性能似乎相同(ghc -O2 -fforce-recomp -fllvm、ghc 版本 7.6.3、LLVM 3.3、x86-64)。
{-# LANGUAGE BangPatterns, UnboxedTuples #-}
import Data.Bits
collatzLength :: Int -> Int
collatzLength x| x == 1 = 1
| otherwise = go x 0
where
go 1 a = a + 1
go x !a = go (nextStep x) (a+1)
longestChain :: (Int, Int) -> Int -> Int -> (Int,Int)
longestChain (num, numLength) bound !counter
| counter >= bound = (num, numLength)
| otherwise = longestChain (longerOf (num,numLength) (counter, collatzLength counter)) bound (counter + 1)
--I know this is a messy function, but I was doing this problem just
--for myself, so I didn't bother making some utility functions for it.
--also, I split the big line in half to display on here nicer, would
--it actually run with this line split?
longerOf :: (Int,Int) -> (Int,Int) -> (Int,Int)
longerOf (a1,a2) (b1,b2)| a2 > b2 = (a1,a2)
| otherwise = (b1,b2)
{-# INLINE longerOf #-}
nextStep :: Int -> Int
-- Version 'bits'
nextStep n = if 0 == n .&. 1 then n `shiftR` 1 else 3*n+1
-- Version 'quotRem'
-- nextStep n = let (q,r) = quotRem n 2 in if r == 0 then q else 3*n+1
-- Version 'almost the original'
-- nextStep n | even n = quot n 2
-- | otherwise = 3*n + 1
{-# INLINE nextStep #-}
main = print (longestChain (0,0) 1000000 1)