Using Psyco to speed up python execution to the same level as the compilation language, psycopython
This article describes how to use Psyco to speed up python execution to the same level as the compilation language. The specific implementation method is as follows:
1. Install PsycoIt has two installation methods: source code and binary code:
If you use the source code for installation, you need to call the python setup. py install command in the source code directory to compile and generate the psyco sub-directory, and then copy the sub-directory to the site-packages directory of python.
If you use the binary code method to install, download the appropriate binary file according to the python and psyco version table in the URL list, decompress the package to generate a psyco-1.x directory, copy the entire psyco directory under this directory to the site-packages directory of python.
Ii. Instructions for useAdd the following two sentences before the source file for efficiency optimization:
Copy codeThe Code is as follows: import psyco
Psyco. full ()
In addition, you can use psyco. profile () to analyze large programs to determine which functions are most worth compiling.
The psyco. log () function is used to record information obtained by profile (). It can be run more quickly next time.
Psyco. bind (myfunc) specifies that the function myfunc can be compiled to achieve more refined control than full.
Psyco. proxy (f) creates a new function whose code is compiled by f to obtain the binary code.
Iii. Example:
The psyco_test.py file code is as follows:
Copy codeThe Code is as follows :#! /Usr/bin/python
# Filename: psyco_test.py
Import math, timeit, psyco
Def TestA ():
Res, loopcnt = 0.0, 100
For I in range (loopcnt ):
For j in range (loopcnt ):
For k in range (loopcnt ):
Res = res + math. sin (I + j + k)
If _ name _ = '_ main __':
TestB = psyco. proxy (TestA)
Ta = timeit. Timer ("TestA ()", "from _ main _ import TestA ")
Tb = timeit. Timer ("TestB ()", "from _ main _ import TestB ")
Print ("TestA (): %. 2fs" % (ta. timeit (10 )))
Print ("TestB (): %. 2fs" % (tb. timeit (10 )))
The running result is as follows:
Copy codeThe Code is as follows: jobin @ jobin-desktop :~ /Work/python/psyco $ python psyco_test.py
TestA (): 4.41 s
TestB (): 1.63 s
The execution speed of functions processed by psyco is about 4 times faster, similar to what the author claims.
I hope this article will help you with Python programming.
Which of Ruby and Python processes faster?
Well. Some people say ruby is faster. It's just fast development. Some people say python is faster. Ruby is developing later, and in some cases it is indeed faster than python. However, python is written in C at the underlying layer. Its script execution efficiency is quite high. These gaps are negligible.
Therefore, the speed of a program depends first on the algorithm. You can test the execution speed. Ruby is indeed developing fast in some fields. For example, the famous ruby on rail. And some ruby testing tools. There are also several well-known python programs, although not that epoch-making. But it is enough. In general, there are more python users and more supported libraries.
Python used to have a psyco optimization library and then converted it to pypy. However, the old version of psyco is easier to use. After that, the python speed is greatly improved. It's hard to say it's better than ruby. In specific fields, the optimization is obvious.
At the beginning, I chose ruby or python, but I still spent some time investigating it. In the end, ruby is somewhat "primitive", which is not open and flexible enough.
Language is a carrier used to express the ideas of programmers. In this regard, I feel that python is more powerful. Strong expressiveness. It is more suitable for me at that time. So I finally chose python.
The script language can be run only by the interpreter. Why can Python be used to write Pypy, which is more efficient than CPython?
However, since C is too close to the underlying layer, the JIT interpreter has many restrictions. (For example, No 64-bit JIT interpreter can be implemented by Psyco until the project dies.) Pypy is another way of thinking. It first implements a Python subset (note that it is not a complete python), called RPython. Then, we use RPython to implement the Python JIT interpreter. This RPython itself does not rely on the runtime interpreter, but is directly translated into C code (in fact, it can be translated into multiple types of target code, such as Java and C #) before compilation, essentially, it is a compilation language. Therefore, programs written in RPython will eventually be compiled with the code at the cost, which is essentially different from those written in C. Thanks to the powerful optimization capabilities of RPython, the final compilation result of RPython is basically equivalent to the efficiency of Direct Writing in C. The JIT interpreter implemented by this program is naturally not slow. The JIT Technology ensures the Efficiency Improvement of the Python program running on this interpreter.