Use Psyco module in Python to optimize running speed _python

Source: Internet
Author: User
Tags in python

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

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.