python与pypy

   神奇的pypy,想了解更多pypy:http://pypy.org/

python官方版本由c语言开发,也叫cpython;pypy通过python编写的(真实是rpython)。

pypy即时编译(just-in-time compile)python脚本,通常执行的速度比cpython更快。

用比较耗时间的算法fib.py,比较pypy与python(cpython)。

1 import sys
2 
3 def fib(n):
4     if n<2:
5         return n
6     else:
7         return fib(n-1)+fib(n-2)
8 if __name__=='__main__':
9     print fib(36)

执行时间比较:

pypy:

1 $ time ./pypy.exe fib.py
2 14930352
3 
4 real    0m1.861s
5 user    0m0.000s
6 sys     0m0.000s

python2.7:

1 $ time python fib.py
2 14930352
3 
4 real    0m11.544s
5 user    0m10.296s
6 sys     0m0.172s

这时间比较没的说,pypy real时间为1.861s而python的为11.544s!

原文地址:https://www.cnblogs.com/wuchaofan/p/3208642.html