Talk about pypy in Python and Pythonpypy

Source: Internet
Author: User

Talk about pypy in Python and Pythonpypy

PyPy is a virtual machine project, mainly divided into two parts: a Python implementation and a compiler

The first part of PyPy: Python implemented in Python

In fact, this is not accurate. It should be accurate to say that it is Python implemented using rPython. rPython is a subset of Python. Although rPython is not a complete Python, however, the Python implementation written in rPython can be used to explain the complete Python language.

The second part of PyPy: Compiler

This is a rPython compiler, or the compiler has an rPython front-end. Currently, there is only one front-end, but there are many backend servers. That is to say, this compiler supports many target languages, important: C, pencil, JavaScript...

The first part is regarded as pypy (1) and the second part as pypy (2)

Why do you need both at the same level? You can think like this: PyPy (1) is an interpreter written in RPython, so it can load your Python code and compile it into bytecode. However, to run the interpreter written in RPython, it must be interpreted by another Python implementation. We can directly use CPython to run this interpreter. But this is not quick enough to replace it. We use PyPy (2) to compile this PyPy interpreter and generate other platforms (such as C, JVM, or CLI) the code runs on our machine and the JIT feature is added. JIT can convert bytecode into a machine language. pypy is fast because it integrates the JIT tracking optimization compiler.

Pypy performance test

Cpython2.7.6, pyston0.2, and pypy2.2.1 use the minibenchmarks and microbenchmarks in the pyston source code directory.
The comparison results are as follows:

 


Cpython2.7.6

Pyston0.2

Microbenchmarks




Attribute_lookup.py

258.544 s

200.387 s

2.667 s

Attrs. py

0.622 s

1.658 s

0.086 s

Closures. py

0.485 s

6.658 s

0.058 s

Empty_loop.py

3.532 s

19.248 s

0.248 s

Fib2.py

3.375 s

0.669 s

0.804 s

Fib. py

3.696 s

0.636 s

0.864 s

Function_calls.py

5.283 s

0.878 s

0.303 s

Gcj_2014_2_ B .py

1.527 s

45.803 s

0.276 s

Gcj_2014_3_ B .py

0.022 s

0.174 s

0.069 s

Iteration. py

0.185 s

1.242 s

0.062 s

Lcg. py

2.910 s

9.097 s

0.235 s

Listcomp_bench.py

10.132 s

56.170 s

1.379 s

Nested. py

0.368 s

6.828 s

0.057 s

Polymorphism. py

4.358 s

4.390 s

14.260 s

Prime_summing.py

20.197 s


43.779 s

1.250 s

Pydigits. py

0.034 s

Failed

0.039 s

Repatching. py

0.475 s

0.384 s

0.061 s

Simple_sum.py

0.075 s

0.578 s

0.040 s

Sort. py

2.216 s

4.587 s

0.135 s

Thread_contention.py

6.486 s

8.133 s

0.240 s

Thread_uncontended.py

1.324 s

5.823 s

0.238 s

Unwinding. py

1.082 s

93.180 s

4.481 s

Vecf_add.py

9.890 s

Failed

0.059 s

Vecf_dot.py

4.944 s

8.434 s

0.062 s





Minibenchmarks




Allgroup. py

0.836 s

Failed

18.804 s

Chaos. py

26.268 s

Failed

1.392 s

Fannkuch_med.py

0.990 s

1.898 s

0.325 s

Fannkuch. py

10.952 s

20.834 s

2.057 s

Go. py

53.787 s

Failed

33.638 s

Interp2.py

5.521 s

10.124 s

0.701 s

Interp. py

10.863 s

5.035 s

0.563 s

Nbody_med.py

3.132 s

6.642 s

0.601 s

Nbody. py

12.677 s

25.540 s

1.470 s

Nq. py

29.879 s

Failed

44.418 s

Raytrace. py

11.608 s

Failed

1.228 s

Spectral_norm.py:

14.388 s

118.309 s

1.333 s

In addition to data with color backgrounds, pypy compilation has the fastest testing results, of which 15 program code test results are less than a tenth of Cpython's.

Pypy Defects

It can be seen that pypy has a great advantage in implementing python, but many company's python projects are still not implemented using pypy because

Pypy has a defect: C has poor scalability. A simple understanding is that if a python program is mixed with C/C ++ code and the C/C ++ library is called, pypy

Not supported or pypy runs much slower. Nowadays, many projects adopt C/C ++/Python hybrid programming.

However, pypy also has its own C/C ++-compatible method (but it does not completely solve the problem of poor scalability). pypy has two methods: ctypes and cffi.

For C extension, the following are some simple program experiments:

Use ctypes to implement C ++ and python mixed programming. First, write one. cpp is then called in the python file, and Cpython and pypy can be used for compilation and execution respectively. It indicates that ctypes supports C ++ extension.

This time, pypy is used to run faster than Cpython.

Summary

The above is all about pypy in Python in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.