神奇的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!

 

相关文章:

  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-09-05
  • 2021-12-14
  • 2021-08-01
  • 2021-09-15
猜你喜欢
  • 2022-01-09
  • 2021-10-26
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2021-08-15
  • 2021-10-06
相关资源
相似解决方案