繁体   English   中英

与 Project Euler 的速度比较:C vs Python vs Erlang vs Haskell

[英]Speed comparison with Project Euler: C vs Python vs Erlang vs Haskell

我将Project Euler中的问题 #12作为一个编程练习,并比较了我在 C、Python、Erlang 和 Haskell 中的(肯定不是最佳的)实现。 为了获得更高的执行时间,我搜索第一个除数超过 1000 个的三角形数,而不是原始问题中所述的 500 个。

结果如下:

C:

lorenzo@enzo:~/erlang$ gcc -lm -o euler12.bin euler12.c
lorenzo@enzo:~/erlang$ time ./euler12.bin
842161320

real    0m11.074s
user    0m11.070s
sys 0m0.000s

Python:

lorenzo@enzo:~/erlang$ time ./euler12.py 
842161320

real    1m16.632s
user    1m16.370s
sys 0m0.250s

带有 PyPy 的 Python:

lorenzo@enzo:~/Downloads/pypy-c-jit-43780-b590cf6de419-linux64/bin$ time ./pypy /home/lorenzo/erlang/euler12.py 
842161320

real    0m13.082s
user    0m13.050s
sys 0m0.020s

二郎:

lorenzo@enzo:~/erlang$ erlc euler12.erl 
lorenzo@enzo:~/erlang$ time erl -s euler12 solve
Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1> 842161320

real    0m48.259s
user    0m48.070s
sys 0m0.020s

哈斯克尔:

lorenzo@enzo:~/erlang$ ghc euler12.hs -o euler12.hsx
[1 of 1] Compiling Main             ( euler12.hs, euler12.o )
Linking euler12.hsx ...
lorenzo@enzo:~/erlang$ time ./euler12.hsx 
842161320

real    2m37.326s
user    2m37.240s
sys 0m0.080s

概括:

  • 丙:100%
  • Python:692%(使用 PyPy 为 118%)
  • Erlang:436%(135% 感谢 RichardC)
  • 哈斯克尔:1421%

我认为 C 有很大的优势,因为它使用 long 进行计算,而不是像其他三个一样使用任意长度的整数。 它也不需要先加载运行时(其他的呢?)。

问题 1: Erlang、Python 和 Haskell 是否会因为使用任意长度的整数而降低速度,或者只要这些值小于MAXINT就不会?

问题 2:为什么 Haskell 这么慢? 是否有关闭刹车的编译器标志还是我的实现? (后者很有可能,因为 Haskell 对我来说是一本带有七个印章的书。)

问题 3:您能否给我一些提示,如何在不改变我确定因素的方式的情况下优化这些实现? 以任何方式优化:更好、更快、更“本机”的语言。

编辑:

问题 4:我的功能实现是否允许 LCO(最后调用优化,又名尾递归消除),从而避免在调用堆栈中添加不必要的帧?

我真的尝试在四种语言中实现尽可能相似的相同算法,尽管我不得不承认我的 Haskell 和 Erlang 知识非常有限。


使用的源代码:

#include <stdio.h>
#include <math.h>

int factorCount (long n)
{
    double square = sqrt (n);
    int isquare = (int) square;
    int count = isquare == square ? -1 : 0;
    long candidate;
    for (candidate = 1; candidate <= isquare; candidate ++)
        if (0 == n % candidate) count += 2;
    return count;
}

int main ()
{
    long triangle = 1;
    int index = 1;
    while (factorCount (triangle) < 1001)
    {
        index ++;
        triangle += index;
    }
    printf ("%ld\n", triangle);
}

#! /usr/bin/env python3.2

import math

def factorCount (n):
    square = math.sqrt (n)
    isquare = int (square)
    count = -1 if isquare == square else 0
    for candidate in range (1, isquare + 1):
        if not n % candidate: count += 2
    return count

triangle = 1
index = 1
while factorCount (triangle) < 1001:
    index += 1
    triangle += index

print (triangle)

-module (euler12).
-compile (export_all).

factorCount (Number) -> factorCount (Number, math:sqrt (Number), 1, 0).

factorCount (_, Sqrt, Candidate, Count) when Candidate > Sqrt -> Count;

factorCount (_, Sqrt, Candidate, Count) when Candidate == Sqrt -> Count + 1;

factorCount (Number, Sqrt, Candidate, Count) ->
    case Number rem Candidate of
        0 -> factorCount (Number, Sqrt, Candidate + 1, Count + 2);
        _ -> factorCount (Number, Sqrt, Candidate + 1, Count)
    end.

nextTriangle (Index, Triangle) ->
    Count = factorCount (Triangle),
    if
        Count > 1000 -> Triangle;
        true -> nextTriangle (Index + 1, Triangle + Index + 1)  
    end.

solve () ->
    io:format ("~p~n", [nextTriangle (1, 1) ] ),
    halt (0).

factorCount number = factorCount' number isquare 1 0 - (fromEnum $ square == fromIntegral isquare)
    where square = sqrt $ fromIntegral number
          isquare = floor square

factorCount' number sqrt candidate count
    | fromIntegral candidate > sqrt = count
    | number `mod` candidate == 0 = factorCount' number sqrt (candidate + 1) (count + 2)
    | otherwise = factorCount' number sqrt (candidate + 1) count

nextTriangle index triangle
    | factorCount triangle > 1000 = triangle
    | otherwise = nextTriangle (index + 1) (triangle + index + 1)

main = print $ nextTriangle 1 1

在 x86_64 Core2 Duo (2.5GHz) 机器上使用GHC 7.0.3gcc 4.4.6Linux 2.6.29 ,使用ghc -O2 -fllvm -fforce-recomp for Haskell 和gcc -O3 -lm for C gcc -O3 -lm

  • 您的 C 例程在 8.4 秒内运行(比您的运行快,可能是因为-O3
  • Haskell 解决方案在 36 秒内运行(由于-O2标志)
  • 您的factorCount'代码未明确键入并默认为Integer (感谢 Daniel 在这里纠正了我的误诊!)。 使用Int给出显式类型签名(无论如何这是标准做法),时间更改为11.1 秒
  • factorCount'您不必要地调用了fromIntegral 修复不会产生任何变化(编译器很聪明,对你来说很幸运)。
  • 您在rem更快且足够的地方使用了mod 这会将时间更改为8.5 秒
  • factorCount'不断应用两个永远不会改变的额外参数( numbersqrt )。 工人/包装器转换为我们提供:
 $ time ./so
 842161320  

 real    0m7.954s  
 user    0m7.944s  
 sys     0m0.004s  

没错, 7.95 秒 始终比 C 解决方案快半秒 没有-fllvm标志,我仍然得到8.182 seconds ,所以在这种情况下 NCG 后端也做得很好。

结论:Haskell 很棒。

结果代码

factorCount number = factorCount' number isquare 1 0 - (fromEnum $ square == fromIntegral isquare)
    where square = sqrt $ fromIntegral number
          isquare = floor square

factorCount' :: Int -> Int -> Int -> Int -> Int
factorCount' number sqrt candidate0 count0 = go candidate0 count0
  where
  go candidate count
    | candidate > sqrt = count
    | number `rem` candidate == 0 = go (candidate + 1) (count + 2)
    | otherwise = go (candidate + 1) count

nextTriangle index triangle
    | factorCount triangle > 1000 = triangle
    | otherwise = nextTriangle (index + 1) (triangle + index + 1)

main = print $ nextTriangle 1 1

编辑:既然我们已经探讨过了,让我们来解决问题

问题 1:erlang、python 和 haskell 是否会因为使用任意长度的整数而降低速度,或者只要这些值小于 MAXINT,它们是否会降低速度?

在 Haskell 中,使用IntegerInt慢,但慢多少取决于执行的计算。 幸运的是(对于 64 位机器) Int就足够了。 为了可移植性,您可能应该重写我的代码以使用Int64Word64 (C 不是唯一具有long语言)。

问题2:为什么haskell这么慢? 是否有关闭刹车的编译器标志还是我的实现? (后者很有可能,因为 Haskell 对我来说是一本带有七个印章的书。)

问题 3:您能否给我一些提示,如何在不改变我确定因素的方式的情况下优化这些实现? 以任何方式优化:更好、更快、更“本机”的语言。

这是我上面的回答。 答案是

  • 0) 通过-O2使用优化
  • 1)尽可能使用快速(特别是:可拆箱)类型
  • 2) rem not mod (经常被遗忘的优化)和
  • 3)worker/wrapper 转换(可能是最常见的优化)。

问题 4:我的功能实现是否允许 LCO 从而避免在调用堆栈中添加不必要的帧?

是的,那不是问题。 干得好,很高兴你考虑到了这一点。

Erlang 实现存在一些问题。 作为下面的基准,我测量的未修改 Erlang 程序的执行时间为 47.6 秒,而 C 代码为 12.7 秒。

如果您想运行计算密集型 Erlang 代码,您应该做的第一件事是使用本机代码。 使用erlc +native euler12编译将时间缩短到 41.3 秒。 然而,这比对此类代码的本机编译预期的加速要低得多(仅 15%),问题在于您使用了-compile(export_all) 这对实验很有用,但所有函数都可能从外部访问的事实导致本机编译器非常保守。 (普通的 BEAM 模拟器不会受到太大影响。)用-export([solve/0]).替换此声明-export([solve/0]). 提供了更好的加速:31.5 秒(几乎是基线的 35%)。

但是代码本身有一个问题:对于factorCount循环中的每一次迭代,你都执行这个测试:

factorCount (_, Sqrt, Candidate, Count) when Candidate == Sqrt -> Count + 1;

C 代码不这样做。 一般来说,在相同代码的不同实现之间进行公平比较可能很棘手,特别是如果算法是数值算法,因为您需要确保它们实际上在做同样的事情。 由于某处的某种类型转换,一个实现中的轻微舍入错误可能会导致它比另一个执行更多的迭代,即使两者最终达到相同的结果。

为了消除这种可能的错误源(并摆脱每次迭代中的额外测试),我按照 C 代码密切建模,重新编写了 factorCount 函数,如下所示:

factorCount (N) ->
    Sqrt = math:sqrt (N),
    ISqrt = trunc(Sqrt),
    if ISqrt == Sqrt -> factorCount (N, ISqrt, 1, -1);
       true          -> factorCount (N, ISqrt, 1, 0)
    end.

factorCount (_N, ISqrt, Candidate, Count) when Candidate > ISqrt -> Count;
factorCount ( N, ISqrt, Candidate, Count) ->
    case N rem Candidate of
        0 -> factorCount (N, ISqrt, Candidate + 1, Count + 2);
        _ -> factorCount (N, ISqrt, Candidate + 1, Count)
    end.

这次重写,没有export_all和本机编译,给了我以下运行时间:

$ erlc +native euler12.erl
$ time erl -noshell -s euler12 solve
842161320

real    0m19.468s
user    0m19.450s
sys 0m0.010s

与 C 代码相比,这还不算太糟糕:

$ time ./a.out 
842161320

real    0m12.755s
user    0m12.730s
sys 0m0.020s

考虑到 Erlang 根本不适合编写数字代码,在这样的程序上只比 C 慢 50% 是相当不错的。

最后,关于你的问题:

问题 1:erlang、python 和 haskell 是否会因为使用任意长度的整数而降低速度,或者只要这些值小于 MAXINT,它们是否会降低速度?

是的,有点。 在 Erlang 中,没有办法说“使用 32/64 位算术和环绕”,因此除非编译器可以证明整数的某些界限(通常不能),否则它必须检查所有计算以查看如果它们可以放入单个标记词中,或者是否必须将它们转换为堆分配的 bignum。 即使在运行时在实践中从未使用过 bignum,也必须执行这些检查。 另一方面,这意味着你知道如果你突然给它比以前更大的输入,算法永远不会因为意外的整数环绕而失败。

问题 4:我的功能实现是否允许 LCO 从而避免在调用堆栈中添加不必要的帧?

是的,您的 Erlang 代码在最后一次调用优化方面是正确的。

关于 Python 优化,除了使用 PyPy(在对代码进行零更改的情况下实现非常令人印象深刻的加速)之外,您还可以使用 PyPy 的翻译工具链来编译符合 RPython 的版本,或使用Cython来构建扩展模块,两者都是在我的测试中,它比 C 版本快,Cython 模块几乎快两倍 作为参考,我还包括 C 和 PyPy 基准测试结果:

C(用gcc -O3 -lm编译)

% time ./euler12-c 
842161320

./euler12-c  11.95s 
 user 0.00s 
 system 99% 
 cpu 11.959 total

pypy 1.5

% time pypy euler12.py
842161320
pypy euler12.py  
16.44s user 
0.01s system 
99% cpu 16.449 total

RPython(使用最新的 PyPy 修订版, c2f583445aee

% time ./euler12-rpython-c
842161320
./euler12-rpy-c  
10.54s user 0.00s 
system 99% 
cpu 10.540 total

赛通 0.15

% time python euler12-cython.py
842161320
python euler12-cython.py  
6.27s user 0.00s 
system 99% 
cpu 6.274 total

RPython 版本有几个关键的变化。 要转换为独立程序,您需要定义target ,在这种情况下是main函数。 预计会接受sys.argv因为它是唯一的参数,并且需要返回一个 int。 您可以使用 translate.py, % translate.py euler12-rpython.py来翻译它,它会转换为 C 并为您编译。

# euler12-rpython.py

import math, sys

def factorCount(n):
    square = math.sqrt(n)
    isquare = int(square)
    count = -1 if isquare == square else 0
    for candidate in xrange(1, isquare + 1):
        if not n % candidate: count += 2
    return count

def main(argv):
    triangle = 1
    index = 1
    while factorCount(triangle) < 1001:
        index += 1
        triangle += index
    print triangle
    return 0

if __name__ == '__main__':
    main(sys.argv)

def target(*args):
    return main, None

Cython 版本被重写为一个扩展模块_euler12.pyx ,我从一个普通的 python 文件导入和调用它。 _euler12.pyx与您的版本基本相同,但有一些额外的静态类型声明。 setup.py 有正常的样板来构建扩展,使用python setup.py build_ext --inplace

# _euler12.pyx
from libc.math cimport sqrt

cdef int factorCount(int n):
    cdef int candidate, isquare, count
    cdef double square
    square = sqrt(n)
    isquare = int(square)
    count = -1 if isquare == square else 0
    for candidate in range(1, isquare + 1):
        if not n % candidate: count += 2
    return count

cpdef main():
    cdef int triangle = 1, index = 1
    while factorCount(triangle) < 1001:
        index += 1
        triangle += index
    print triangle

# euler12-cython.py
import _euler12
_euler12.main()

# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("_euler12", ["_euler12.pyx"])]

setup(
  name = 'Euler12-Cython',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

老实说,我对 RPython 或 Cython 的经验很少,并且对结果感到惊喜。 如果您使用 CPython,在 Cython 扩展模块中编写 CPU 密集型代码似乎是优化程序的一种非常简单的方法。

问题 3:您能否给我一些提示,如何在不改变我确定因素的方式的情况下优化这些实现? 以任何方式优化:更好、更快、更“本机”的语言。

C 实现是次优的(正如 Thomas M. DuBuisson 所暗示的那样),该版本使用 64 位整数(即long数据类型)。 稍后我将调查程序集列表,但根据有根据的猜测,编译代码中存在一些内存访问,这使得使用 64 位整数的速度明显变慢。 就是那个或生成的代码(事实上,您可以在 SSE 寄存器中放入较少的 64 位整数,或者将双精度舍入为 64 位整数的速度较慢)。

这是修改后的代码(简单地用int替换long并且我显式地内联了 factorCount,尽管我认为 gcc -O3 不需要这样做):

#include <stdio.h>
#include <math.h>

static inline int factorCount(int n)
{
    double square = sqrt (n);
    int isquare = (int)square;
    int count = isquare == square ? -1 : 0;
    int candidate;
    for (candidate = 1; candidate <= isquare; candidate ++)
        if (0 == n % candidate) count += 2;
    return count;
}

int main ()
{
    int triangle = 1;
    int index = 1;
    while (factorCount (triangle) < 1001)
    {
        index++;
        triangle += index;
    }
    printf ("%d\n", triangle);
}

运行+计时它给出:

$ gcc -O3 -lm -o euler12 euler12.c; time ./euler12
842161320
./euler12  2.95s user 0.00s system 99% cpu 2.956 total

作为参考,Thomas 在前面的回答中的 haskell 实现给出了:

$ ghc -O2 -fllvm -fforce-recomp euler12.hs; time ./euler12                                                                                      [9:40]
[1 of 1] Compiling Main             ( euler12.hs, euler12.o )
Linking euler12 ...
842161320
./euler12  9.43s user 0.13s system 99% cpu 9.602 total

结论: ghc 是一个很棒的编译器,什么也没有带走,但是 gcc 通常会生成更快的代码。

看看这个博客 在过去一年左右的时间里,他用 Haskell 和 Python 完成了一些 Project Euler 问题,并且他普遍发现Haskell快得多。 我认为在这些语言之间,它更多地与您的流畅性和编码风格有关。

当谈到 Python 速度时,您使用了错误的实现! 试试PyPy ,对于这样的事情,你会发现它要快得多。

通过使用 Haskell 包中的一些函数,您的 Haskell 实现可以大大加快。 在这种情况下,我使用了素数,它只是通过“cabal install primes”安装的;)

import Data.Numbers.Primes
import Data.List

triangleNumbers = scanl1 (+) [1..]
nDivisors n = product $ map ((+1) . length) (group (primeFactors n))
answer = head $ filter ((> 500) . nDivisors) triangleNumbers

main :: IO ()
main = putStrLn $ "First triangle number to have over 500 divisors: " ++ (show answer)

时间:

您的原始程序:

PS> measure-command { bin\012_slow.exe }

TotalSeconds      : 16.3807409
TotalMilliseconds : 16380.7409

改进实施

PS> measure-command { bin\012.exe }

TotalSeconds      : 0.0383436
TotalMilliseconds : 38.3436

正如您所看到的,这个在 38 毫秒内运行在您运行 16 秒的同一台机器上:)

编译命令:

ghc -O2 012.hs -o bin\012.exe
ghc -O2 012_slow.hs -o bin\012_slow.exe

只是为了好玩。 以下是一个更“原生”的 Haskell 实现:

import Control.Applicative
import Control.Monad
import Data.Either
import Math.NumberTheory.Powers.Squares

isInt :: RealFrac c => c -> Bool
isInt = (==) <$> id <*> fromInteger . round

intSqrt :: (Integral a) => a -> Int
--intSqrt = fromIntegral . floor . sqrt . fromIntegral
intSqrt = fromIntegral . integerSquareRoot'

factorize :: Int -> [Int]
factorize 1 = []
factorize n = first : factorize (quot n first)
  where first = (!! 0) $ [a | a <- [2..intSqrt n], rem n a == 0] ++ [n]

factorize2 :: Int -> [(Int,Int)]
factorize2 = foldl (\ls@((val,freq):xs) y -> if val == y then (val,freq+1):xs else (y,1):ls) [(0,0)] . factorize

numDivisors :: Int -> Int
numDivisors = foldl (\acc (_,y) -> acc * (y+1)) 1 <$> factorize2

nextTriangleNumber :: (Int,Int) -> (Int,Int)
nextTriangleNumber (n,acc) = (n+1,acc+n+1)

forward :: Int -> (Int, Int) -> Either (Int, Int) (Int, Int)
forward k val@(n,acc) = if numDivisors acc > k then Left val else Right (nextTriangleNumber val)

problem12 :: Int -> (Int, Int)
problem12 n = (!!0) . lefts . scanl (>>=) (forward n (1,1)) . repeat . forward $ n

main = do
  let (n,val) = problem12 1000
  print val

使用ghc -O3 ,这在我的机器(1.73GHz Core i7)上始终运行在 0.55-0.58 秒内。

C 版本更高效的 factorCount 函数:

int factorCount (int n)
{
  int count = 1;
  int candidate,tmpCount;
  while (n % 2 == 0) {
    count++;
    n /= 2;
  }
    for (candidate = 3; candidate < n && candidate * candidate < n; candidate += 2)
    if (n % candidate == 0) {
      tmpCount = 1;
      do {
        tmpCount++;
        n /= candidate;
      } while (n % candidate == 0);
       count*=tmpCount;
      }
  if (n > 1)
    count *= 2;
  return count;
}

使用gcc -O3 -lm在 main 中将 longs 更改为 ints,这始终在 0.31-0.35 秒内运行。

如果您利用第 n 个三角形数 = n*(n+1)/2,并且 n 和 (n+1) 具有完全不同的质因数分解这一事实,两者都可以运行得更快,因此因子数可以将每一半的数相乘以求出整体的因数数。 以下:

int main ()
{
  int triangle = 0,count1,count2 = 1;
  do {
    count1 = count2;
    count2 = ++triangle % 2 == 0 ? factorCount(triangle+1) : factorCount((triangle+1)/2);
  } while (count1*count2 < 1001);
  printf ("%lld\n", ((long long)triangle)*(triangle+1)/2);
}

会将 c 代码运行时间减少到 0.17-0.19 秒,并且它可以处理更大的搜索——在我的机器上,大于 10000 个因子大约需要 43 秒。 我给感兴趣的读者留下了类似的haskell加速。

问题 1:erlang、python 和 haskell 是否会因为使用任意长度的整数而降低速度,或者只要这些值小于 MAXINT,它们是否会降低速度?

这不太可能。 我不能说太多关于 Erlang 和 Haskell(好吧,下面可能会谈到 Haskell),但我可以指出 Python 中的许多其他瓶颈。 每次程序尝试使用 Python 中的某些值执行操作时,都应该验证这些值是否来自正确的类型,这会花费一些时间。 您的factorCount函数只是多次分配range (1, isquare + 1)的列表,并且运行时, malloc样式的内存分配比在 C 中使用计数器迭代范围要慢得多。值得注意的是, factorCount()被多次调用,因此分配了很多列表。 另外,让我们不要忘记 Python 是解释性的,而 CPython 解释器并没有特别关注优化。

编辑:哦,好吧,我注意到您使用的是 Python 3,因此range()不返回列表,而是返回一个生成器。 在这种情况下,我关于分配列表的观点是错误的:该函数只分配range对象,尽管效率低下,但不如分配具有大量项目的列表效率低下。

问题2:为什么haskell这么慢? 是否有关闭刹车的编译器标志还是我的实现? (后者很有可能,因为 Haskell 对我来说是一本带有七个印章的书。)

你在用拥抱吗? Hugs 是一个相当慢的解释器。 如果你正在使用它,也许你可以在GHC 上获得更好的时间 - 但我只是在思考假设,一个好的 Haskell 编译器在幕后所做的事情非常迷人,而且超出了我的理解:)

问题 3:您能否给我一些提示,如何在不改变我确定因素的方式的情况下优化这些实现? 以任何方式优化:更好、更快、更“本机”的语言。

我会说你在玩一个无趣的游戏。 了解各种语言的最好的部分是尽可能以最不同的方式使用它们:) 但是我离题了,我只是在这一点上没有任何建议。 对不起,我希望有人能在这种情况下帮助你:)

问题 4:我的功能实现是否允许 LCO 从而避免在调用堆栈中添加不必要的帧?

据我所知,您只需要确保您的递归调用是返回值之前的最后一个命令。 换句话说,像下面这样的函数可以使用这样的优化:

def factorial(n, acc=1):
    if n > 1:
        acc = acc * n
        n = n - 1
        return factorial(n, acc)
    else:
        return acc

但是,如果您的函数如下所示,则不会有这样的优化,因为在递归调用之后有一个运算(乘法):

def factorial2(n):
    if n > 1:
        f = factorial2(n-1)
        return f*n
    else:
        return 1

我将一些局部变量中的操作分开,以明确执行哪些操作。 但是,最常见的是将这些函数如下所示,但它们对于我要提出的观点是等效的:

def factorial(n, acc=1):
    if n > 1:
        return factorial(n-1, acc*n)
    else:
        return acc

def factorial2(n):
    if n > 1:
        return n*factorial(n-1)
    else:
        return 1

请注意,由编译器/解释器决定是否进行尾递归。 例如,如果我记得很清楚,Python 解释器就不会这样做(我在我的示例中使用 Python 只是因为它的语法流畅)。 无论如何,如果您发现奇怪的东西,例如带有两个参数的阶乘函数(并且其中一个参数具有accaccumulator等名称),那么现在您知道人们为什么这样做了 :)

使用 Haskell,你真的不需要明确地考虑递归。

factorCount number = foldr factorCount' 0 [1..isquare] -
                     (fromEnum $ square == fromIntegral isquare)
    where
      square = sqrt $ fromIntegral number
      isquare = floor square
      factorCount' candidate
        | number `rem` candidate == 0 = (2 +)
        | otherwise = id

triangles :: [Int]
triangles = scanl1 (+) [1,2..]

main = print . head $ dropWhile ((< 1001) . factorCount) triangles

在上面的代码中,我用常见的列表操作替换了@Thomas 答案中的显式递归。 代码仍然做完全相同的事情,无需我们担心尾递归。 它在我的机器上运行(~ 7.49s )比@Thomas 的答案(~ 7.04s )中的版本慢6% ,使用 GHC 7.6.2,而来自 @Raedwulf 的 C 版本运行 ~ 3.15s 似乎 GHC 在这一年里有所改善。

附注。 我知道这是一个老问题,我从谷歌搜索中偶然发现了它(我现在忘记了我在搜索什么......)。 只是想评论一下关于 LCO 的问题,并表达我对 Haskell 的总体感受。 我想对最佳答案发表评论,但评论不允许代码块。

C 版本的更多数字和解释。 显然这些年来没有人这样做过。 记得给这个答案点赞,这样它就可以让每个人都看到和学习​​。

第一步:作者程序的基准

笔记本电脑规格:

  • CPU i3 M380(931 MHz - 最大省电模式)
  • 4GB内存
  • Win7 64位
  • 微软 Visual Studio 2012 终极版
  • 带有 gcc 4.9.3 的 Cygwin
  • 蟒蛇 2.7.10

命令:

compiling on VS x64 command prompt > `for /f %f in ('dir /b *.c') do cl /O2 /Ot /Ox %f -o %f_x64_vs2012.exe`
compiling on cygwin with gcc x64   > `for f in ./*.c; do gcc -m64 -O3 $f -o ${f}_x64_gcc.exe ; done`
time (unix tools) using cygwin > `for f in ./*.exe; do  echo "----------"; echo $f ; time $f ; done`

.

----------
$ time python ./original.py

real    2m17.748s
user    2m15.783s
sys     0m0.093s
----------
$ time ./original_x86_vs2012.exe

real    0m8.377s
user    0m0.015s
sys     0m0.000s
----------
$ time ./original_x64_vs2012.exe

real    0m8.408s
user    0m0.000s
sys     0m0.015s
----------
$ time ./original_x64_gcc.exe

real    0m20.951s
user    0m20.732s
sys     0m0.030s

文件名是: integertype_architecture_compiler.exe

  • 整数类型现在与原始程序相同(稍后会详细介绍)
  • 架构是 x86 或 x64,具体取决于编译器设置
  • 编译器是 gcc 或 vs2012

第二步:再次调查、改进和基准测试

VS 比 gcc 快 250%。 这两个编译器应该提供相似的速度。 显然,代码或编译器选项有问题。 让我们调查一下!

第一个兴趣点是整数类型。 转换可能很昂贵,而一致性对于更好的代码生成和优化很重要。 所有整数都应该是相同的类型。

现在是intlong的混合混乱。 我们将对此进行改进。 使用什么类型? 最快的。 必须对他们进行基准测试!

----------
$ time ./int_x86_vs2012.exe

real    0m8.440s
user    0m0.016s
sys     0m0.015s
----------
$ time ./int_x64_vs2012.exe

real    0m8.408s
user    0m0.016s
sys     0m0.015s
----------
$ time ./int32_x86_vs2012.exe

real    0m8.408s
user    0m0.000s
sys     0m0.015s
----------
$ time ./int32_x64_vs2012.exe

real    0m8.362s
user    0m0.000s
sys     0m0.015s
----------
$ time ./int64_x86_vs2012.exe

real    0m18.112s
user    0m0.000s
sys     0m0.015s
----------
$ time ./int64_x64_vs2012.exe

real    0m18.611s
user    0m0.000s
sys     0m0.015s
----------
$ time ./long_x86_vs2012.exe

real    0m8.393s
user    0m0.015s
sys     0m0.000s
----------
$ time ./long_x64_vs2012.exe

real    0m8.440s
user    0m0.000s
sys     0m0.015s
----------
$ time ./uint32_x86_vs2012.exe

real    0m8.362s
user    0m0.000s
sys     0m0.015s
----------
$ time ./uint32_x64_vs2012.exe

real    0m8.393s
user    0m0.015s
sys     0m0.015s
----------
$ time ./uint64_x86_vs2012.exe

real    0m15.428s
user    0m0.000s
sys     0m0.015s
----------
$ time ./uint64_x64_vs2012.exe

real    0m15.725s
user    0m0.015s
sys     0m0.015s
----------
$ time ./int_x64_gcc.exe

real    0m8.531s
user    0m8.329s
sys     0m0.015s
----------
$ time ./int32_x64_gcc.exe

real    0m8.471s
user    0m8.345s
sys     0m0.000s
----------
$ time ./int64_x64_gcc.exe

real    0m20.264s
user    0m20.186s
sys     0m0.015s
----------
$ time ./long_x64_gcc.exe

real    0m20.935s
user    0m20.809s
sys     0m0.015s
----------
$ time ./uint32_x64_gcc.exe

real    0m8.393s
user    0m8.346s
sys     0m0.015s
----------
$ time ./uint64_x64_gcc.exe

real    0m16.973s
user    0m16.879s
sys     0m0.030s

整数类型是#include <stdint.h>中的int long int32_t uint32_t int64_tuint64_t

C 中有很多整数类型,加上一些有符号/无符号可以玩,加上编译为 x86 或 x64 的选择(不要与实际的整数大小混淆)。 那是很多版本要编译和运行^^

第三步:理解数字

最终结论:

  • 32 位整数比 64 位整数快约 200%
  • 无符号的64位整数是25%,比签署64位(不幸的是,我有没有解释)快

技巧问题:“C 中 int 和 long 的大小是多少?”
正确答案是: C 中 int 和 long 的大小没有明确定义!

从 C 规范:

int 至少为 32 位
long 至少是一个 int

从 gcc 手册页(-m32 和 -m64 标志):

32 位环境将 int、long 和指针设置为 32 位,并生成可在任何 i386 系统上运行的代码。
64 位环境将 int 设置为 32 位,将 long 和指针设置为 64 位,并为 AMD 的 x86-64 架构生成代码。

从 MSDN 文档(数据类型范围) https://msdn.microsoft.com/en-us/library/s3f49ktz%28v=vs.110%29.aspx

int,4 个字节,也称为有符号
long,4个字节,也称为long int和signed long int

总结:吸取的教训

  • 32 位整数比 64 位整数快。

  • 标准整数类型在 C 和 C++ 中都没有很好的定义,它们因编译器和体系结构而异。 当您需要一致性和可预测性时,请使用#include <stdint.h>uint32_t整数系列。

  • 速度问题解决了。 所有其他语言都落后了数百%,C 和 C++ 再次获胜! 他们总是这样做。 下一个改进将是使用 OpenMP 的多线程:D

查看您的 Erlang 实现。 时间包括启动整个虚拟机、运行您的程序和停止虚拟机。 我很确定设置和停止 erlang 虚拟机需要一些时间。

如果计时是在 erlang 虚拟机本身内完成的,结果会有所不同,因为在这种情况下,我们将只有相关程序的实际时间。 否则,我相信启动和加载 Erlang Vm 的过程加上停止它(正如你把它放在你的程序中)所花费的总时间都包含在你用来计时的方法的总时间中程序正在输出。 考虑使用 erlang 计时本身,当我们想在虚拟机本身timer:tc/1 or timer:tc/2 or timer:tc/3为我们的程序计时时,我们会使用它。 这样,erlang 的结果将排除启动和停止/终止/停止虚拟机所花费的时间。 这就是我的推理,考虑一下,然后再次尝试您的基准。

我实际上建议我们尝试在这些语言的运行时内对程序进行计时(对于具有运行时的语言),以获得精确的值。 例如,C 没有像 Erlang、Python 和 Haskell 那样启动和关闭运行时系统的开销(对此有 98% 的把握 - 我支持更正)。 所以(基于这个推理)我总结说这个基准测试对于运行在运行时系统之上的语言来说不够精确/公平。 让我们用这些更改再做一次。

编辑:此外,即使所有语言都有运行时系统,启动和停止它的开销也会有所不同。 所以我建议我们从运行时系统内进行计时(对于适用的语言)。 众所周知,Erlang VM 在启动时有相当大的开销!

问题 1:Erlang、Python 和 Haskell 是否会因为使用任意长度的整数而失去速度,或者只要这些值小于 MAXINT,它们就不会?

对于 Erlang,问题一的回答是否定的。 最后一个问题是通过适当地使用 Erlang 来回答的,如下所示:

http://bredsaal.dk/learning-erlang-using-projecteuler-net

由于它比您最初的 C 示例快,我猜有很多问题,因为其他人已经详细介绍过。

这个 Erlang 模块在便宜的上网本上执行大约需要 5 秒......它使用 erlang 中的网络线程模型,因此演示了如何利用事件模型。 它可以分布在许多节点上。 而且速度很快。 不是我的代码。

-module(p12dist).  
-author("Jannich Brendle, jannich@bredsaal.dk, http://blog.bredsaal.dk").  
-compile(export_all).

server() ->  
  server(1).

server(Number) ->  
  receive {getwork, Worker_PID} -> Worker_PID ! {work,Number,Number+100},  
  server(Number+101);  
  {result,T} -> io:format("The result is: \~w.\~n", [T]);  
  _ -> server(Number)  
  end.

worker(Server_PID) ->  
  Server_PID ! {getwork, self()},  
  receive {work,Start,End} -> solve(Start,End,Server_PID)  
  end,  
  worker(Server_PID).

start() ->  
  Server_PID = spawn(p12dist, server, []),  
  spawn(p12dist, worker, [Server_PID]),  
  spawn(p12dist, worker, [Server_PID]),  
  spawn(p12dist, worker, [Server_PID]),  
  spawn(p12dist, worker, [Server_PID]).

solve(N,End,_) when N =:= End -> no_solution;

solve(N,End,Server_PID) ->  
  T=round(N*(N+1)/2),
  case (divisor(T,round(math:sqrt(T))) > 500) of  
    true ->  
      Server_PID ! {result,T};  
    false ->  
      solve(N+1,End,Server_PID)  
  end.

divisors(N) ->  
  divisor(N,round(math:sqrt(N))).

divisor(_,0) -> 1;  
divisor(N,I) ->  
  case (N rem I) =:= 0 of  
  true ->  
    2+divisor(N,I-1);  
  false ->  
    divisor(N,I-1)  
  end.

下面的测试是在 Intel(R) Atom(TM) CPU N270 @ 1.60GHz 上进行的

~$ time erl -noshell -s p12dist start

The result is: 76576500.

^C

BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a

real    0m5.510s
user    0m5.836s
sys 0m0.152s

尝试去:

package main

import "fmt"
import "math"

func main() {
    var n, m, c int
    for i := 1; ; i++ {
        n, m, c = i * (i + 1) / 2, int(math.Sqrt(float64(n))), 0
        for f := 1; f < m; f++ {
            if n % f == 0 { c++ }
    }
    c *= 2
    if m * m == n { c ++ }
    if c > 1001 {
        fmt.Println(n)
        break
        }
    }
}

我得到:

原 c 版:9.1690 100%
去: 8.2520 111%

但是使用:

package main

import (
    "math"
    "fmt"
 )

// Sieve of Eratosthenes
func PrimesBelow(limit int) []int {
    switch {
        case limit < 2:
            return []int{}
        case limit == 2:
            return []int{2}
    }
    sievebound := (limit - 1) / 2
    sieve := make([]bool, sievebound+1)
    crosslimit := int(math.Sqrt(float64(limit))-1) / 2
    for i := 1; i <= crosslimit; i++ {
        if !sieve[i] {
            for j := 2 * i * (i + 1); j <= sievebound; j += 2*i + 1 {
                sieve[j] = true
            }
        }
    }
    plimit := int(1.3*float64(limit)) / int(math.Log(float64(limit)))
    primes := make([]int, plimit)
    p := 1
    primes[0] = 2
    for i := 1; i <= sievebound; i++ {
        if !sieve[i] {
            primes[p] = 2*i + 1
            p++
            if p >= plimit {
                break
            }
        }
    }
    last := len(primes) - 1
    for i := last; i > 0; i-- {
        if primes[i] != 0 {
            break
        }
        last = i
    }
    return primes[0:last]
}



func main() {
    fmt.Println(p12())
}
// Requires PrimesBelow from utils.go
func p12() int {
    n, dn, cnt := 3, 2, 0
    primearray := PrimesBelow(1000000)
    for cnt <= 1001 {
        n++
        n1 := n
        if n1%2 == 0 {
            n1 /= 2
        }
        dn1 := 1
        for i := 0; i < len(primearray); i++ {
            if primearray[i]*primearray[i] > n1 {
                dn1 *= 2
                break
            }
            exponent := 1
            for n1%primearray[i] == 0 {
                exponent++
                n1 /= primearray[i]
            }
            if exponent > 1 {
                dn1 *= exponent
            }
            if n1 == 1 {
                break
            }
        }
        cnt = dn * dn1
        dn = dn1
    }
    return n * (n - 1) / 2
}

我得到:

原 c 版:9.1690 100%
thaumkid 的 c 版本:0.1060 8650%
第一个版本:8.2520 111%
第二个版本:0.0230 39865%

我也试过 Python3.6 和 pypy3.3-5.5-alpha:

原 c 版:8.629 100%
thaumkid 的 c 版本:0.109 7916%
Python3.6:54.795 16%
pypy3.3-5.5-alpha:13.291 65%

然后使用以下代码我得到了:

原 c 版:8.629 100%
thaumkid 的 c 版本:0.109 8650%
Python3.6:1.489 580%
pypy3.3-5.5-alpha:0.582 1483%

def D(N):
    if N == 1: return 1
    sqrtN = int(N ** 0.5)
    nf = 1
    for d in range(2, sqrtN + 1):
        if N % d == 0:
            nf = nf + 1
    return 2 * nf - (1 if sqrtN**2 == N else 0)

L = 1000
Dt, n = 0, 0

while Dt <= L:
    t = n * (n + 1) // 2
    Dt = D(n/2)*D(n+1) if n%2 == 0 else D(n)*D((n+1)/2)
    n = n + 1

print (t)

C++11,对我来说 < 20ms -在这里运行

我知道您需要提示来帮助提高您的语言特定知识,但由于这里已经很好地涵盖了这一点,我想我会为那些可能看过 mathematica 对您的问题等评论的人添加一些背景信息,并想知道为什么会这样代码太慢了。

这个答案主要是提供上下文,希望能帮助人们更轻松地评估您的问题/其他答案中的代码。

这段代码只使用了几个(丑陋的)优化,与使用的语言无关,基于:

  1. 每个 traingle 数的形式为 n(n+1)/2
  2. n 和 n+1 互质
  3. 除数的数量是一个乘法函数

#include <iostream>
#include <cmath>
#include <tuple>
#include <chrono>

using namespace std;

// Calculates the divisors of an integer by determining its prime factorisation.

int get_divisors(long long n)
{
    int divisors_count = 1;

    for(long long i = 2;
        i <= sqrt(n);
        /* empty */)
    {
        int divisions = 0;
        while(n % i == 0)
        {
            n /= i;
            divisions++;
        }

        divisors_count *= (divisions + 1);

        //here, we try to iterate more efficiently by skipping
        //obvious non-primes like 4, 6, etc
        if(i == 2)
            i++;
        else
            i += 2;
    }

    if(n != 1) //n is a prime
        return divisors_count * 2;
    else
        return divisors_count;
}

long long euler12()
{
    //n and n + 1
    long long n, n_p_1;

    n = 1; n_p_1 = 2;

    // divisors_x will store either the divisors of x or x/2
    // (the later iff x is divisible by two)
    long long divisors_n = 1;
    long long divisors_n_p_1 = 2;

    for(;;)
    {
        /* This loop has been unwound, so two iterations are completed at a time
         * n and n + 1 have no prime factors in common and therefore we can
         * calculate their divisors separately
         */

        long long total_divisors;                 //the divisors of the triangle number
                                                  // n(n+1)/2

        //the first (unwound) iteration

        divisors_n_p_1 = get_divisors(n_p_1 / 2); //here n+1 is even and we

        total_divisors =
                  divisors_n
                * divisors_n_p_1;

        if(total_divisors > 1000)
            break;

        //move n and n+1 forward
        n = n_p_1;
        n_p_1 = n + 1;

        //fix the divisors
        divisors_n = divisors_n_p_1;
        divisors_n_p_1 = get_divisors(n_p_1);   //n_p_1 is now odd!

        //now the second (unwound) iteration

        total_divisors =
                  divisors_n
                * divisors_n_p_1;

        if(total_divisors > 1000)
            break;

        //move n and n+1 forward
        n = n_p_1;
        n_p_1 = n + 1;

        //fix the divisors
        divisors_n = divisors_n_p_1;
        divisors_n_p_1 = get_divisors(n_p_1 / 2);   //n_p_1 is now even!
    }

    return (n * n_p_1) / 2;
}

int main()
{
    for(int i = 0; i < 1000; i++)
    {
        using namespace std::chrono;
        auto start = high_resolution_clock::now();
        auto result = euler12();
        auto end = high_resolution_clock::now();

        double time_elapsed = duration_cast<milliseconds>(end - start).count();

        cout << result << " " << time_elapsed << '\n';
    }
    return 0;
}

我的台式机平均需要大约 19 毫秒,我的笔记本电脑平均需要 80 毫秒,这与我在这里看到的大多数其他代码相差甚远。 毫无疑问,仍有许多优化可用。

更改: case (divisor(T,round(math:sqrt(T))) > 500) of

到: case (divisor(T,round(math:sqrt(T))) > 1000) of

这将为 Erlang 多进程示例生成正确的答案。

我假设只有当涉及的数字有很多小因子时,因子的数量才会很大。 所以我使用了 thaumkid 的优秀算法,但首先使用了一个永远不会太小的因子计数的近似值。 这很简单:检查最多 29 个的质因数,然后检查剩余的数并计算因数的上限。 使用它来计算因子数量的上限,如果该数字足够高,则计算因子的确切数量。

下面的代码不需要这个假设的正确性,但要快速。 它似乎有效; 只有大约十分之一的数字给出的估计值足够高,需要进行全面检查。

这是代码:

// Return at least the number of factors of n.
static uint64_t approxfactorcount (uint64_t n)
{
    uint64_t count = 1, add;

#define CHECK(d)                            \
    do {                                    \
        if (n % d == 0) {                   \
            add = count;                    \
            do { n /= d; count += add; }    \
            while (n % d == 0);             \
        }                                   \
    } while (0)

    CHECK ( 2); CHECK ( 3); CHECK ( 5); CHECK ( 7); CHECK (11); CHECK (13);
    CHECK (17); CHECK (19); CHECK (23); CHECK (29);
    if (n == 1) return count;
    if (n < 1ull * 31 * 31) return count * 2;
    if (n < 1ull * 31 * 31 * 37) return count * 4;
    if (n < 1ull * 31 * 31 * 37 * 37) return count * 8;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41) return count * 16;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43) return count * 32;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47) return count * 64;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47 * 53) return count * 128;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47 * 53 * 59) return count * 256;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47 * 53 * 59 * 61) return count * 512;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47 * 53 * 59 * 61 * 67) return count * 1024;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47 * 53 * 59 * 61 * 67 * 71) return count * 2048;
    if (n < 1ull * 31 * 31 * 37 * 37 * 41 * 43 * 47 * 53 * 59 * 61 * 67 * 71 * 73) return count * 4096;
    return count * 1000000;
}

// Return the number of factors of n.
static uint64_t factorcount (uint64_t n)
{
    uint64_t count = 1, add;

    CHECK (2); CHECK (3);

    uint64_t d = 5, inc = 2;
    for (; d*d <= n; d += inc, inc = (6 - inc))
        CHECK (d);

    if (n > 1) count *= 2; // n must be a prime number
    return count;
}

// Prints triangular numbers with record numbers of factors.
static void printrecordnumbers (uint64_t limit)
{
    uint64_t record = 30000;

    uint64_t count1, factor1;
    uint64_t count2 = 1, factor2 = 1;

    for (uint64_t n = 1; n <= limit; ++n)
    {
        factor1 = factor2;
        count1 = count2;

        factor2 = n + 1; if (factor2 % 2 == 0) factor2 /= 2;
        count2 = approxfactorcount (factor2);

        if (count1 * count2 > record)
        {
            uint64_t factors = factorcount (factor1) * factorcount (factor2);
            if (factors > record)
            {
                printf ("%lluth triangular number = %llu has %llu factors\n", n, factor1 * factor2, factors);
                record = factors;
            }
        }
    }
}

这将在大约 0.7 秒内找到具有 13824 个因数的第 14,753,024 个三角形,在 34 秒内找到具有 61,440 个因数的第 879,207,615 个三角形数,在 34 秒内找到具有 138,240 个因数的第 12,524,486,975 个三角形数,第 879,207,615 个三角形数在 56,27 秒内有 56,16 个因数,第 407,615 个三角形数在21 分 25 秒(2.4GHz Core2 Duo),因此这段代码平均每个数字只需要 116 个处理器周期。 最后一个三角形数本身大于 2^68,所以

我将“Jannich Brendle”版本修改为 1000 而不是 500。并列出 euler12.bin、euler12.erl、p12dist.erl 的结果。 两个 erl 代码都使用 '+native' 进行编译。

zhengs-MacBook-Pro:workspace zhengzhibin$ time erl -noshell -s p12dist start
The result is: 842161320.

real    0m3.879s
user    0m14.553s
sys     0m0.314s
zhengs-MacBook-Pro:workspace zhengzhibin$ time erl -noshell -s euler12 solve
842161320

real    0m10.125s
user    0m10.078s
sys     0m0.046s
zhengs-MacBook-Pro:workspace zhengzhibin$ time ./euler12.bin 
842161320

real    0m5.370s
user    0m5.328s
sys     0m0.004s
zhengs-MacBook-Pro:workspace zhengzhibin$
#include <stdio.h>
#include <math.h>

int factorCount (long n)
{
    double square = sqrt (n);
    int isquare = (int) square+1;
    long candidate = 2;
    int count = 1;
    while(candidate <= isquare && candidate <= n){
        int c = 1;
        while (n % candidate == 0) {
           c++;
           n /= candidate;
        }
        count *= c;
        candidate++;
    }
    return count;
}

int main ()
{
    long triangle = 1;
    int index = 1;
    while (factorCount (triangle) < 1001)
    {
        index ++;
        triangle += index;
    }
    printf ("%ld\n", triangle);
}

gcc -lm -Ofast euler.c

时间./a.out

2.79s 用户 0.00s 系统 99% cpu 2.794 总计

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM