python haskell 使用对比_与Euler项目的速度比较:C,Python,Erlang,Haskell

关于Python优化,除了使用PyPy(在代码零更改的情况下实现了令人印象深刻的加速)之外,您还可以使用PyPy的翻译工具链来编译兼容RPython的版本,或者使用Cython来构建扩展模块,这两者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

Cython 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密集型代码似乎是优化程序的一种简便方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值