Today, the Psyco module, the Psyco module, enables your Python program to run as fast as C.
Python is said to be easy to use, but performance is much worse than some compiled languages (such as C), where you can write Fibonacci sequence calculations in C and Python languages, and calculate the elapsed time:
C Language Program
Copy Code code as follows:
int fib (int n) {
if (n < 2)
return n;
Else
return fib (n-1) + fib (n-2);
}
int main () {
FIB (40);
return 0;
}
It 's written in Python.
Copy Code code as follows:
def fib (n):
If n < 2:
return n
Else
return fib (n-1) + fib (n-2)
FIB (40)
Run Time
Copy Code code as follows:
$ time./fib
3.099s
$ time Python fib.py
16.655s
Can see the running time or a little gap, here is about 5 times times the gap, now introduced Psyco:
Psyco is an extension module of the Python language, it can optimize the program code in time, and can improve the execution speed of the program, especially when there is a lot of cyclic operation in the program. was first developed by Armin Rigo, and later by Christian Tismer maintenance and continue to improve.
Psyco can be run on a 32-bit gnu/linux, BSD, Mac OS X, Microsoft Windows platform. Psyco is written in C language and is encoded only for 32-bit platforms. The current development effort has been discontinued and replaced by PyPy, while PyPy also provides support for 64-bit systems. Psyco can be automatically optimized when the Python interpreter compiles code, uses C to implement it, and makes some special optimizations for looping operations. After these optimizations, the performance of the program will be improved, especially in cross-platform environments.
Install Psyco
Copy Code code as follows:
sudo apt-get install Python-psyco
Or to the official Web download installation package, easy install installation can be used.
Using the Psyco module
Copy Code code as follows:
Import Psyco
Psyco.full ()
def fib (n):
If n < 2:
return n
Else
return fib (n-1) + fib (n-2)
FIB (40)
Run results
Copy Code code as follows:
$ time Python fib.py
3.190s
Improve Your code
Now add most of my Python code with the following scripts to increase the speed of the operation using Psyco:
Copy Code code as follows:
Try
Import Psyco
Psyco.full ()
Except Importerror:
Pass # Psyco not installed so continue as usual