Python uses the Psyco module to optimize the running speed.

Source: Internet
Author: User

Python uses the Psyco module to optimize the running speed.

Today we will introduce the Psyco module, which enables your Python program to run as fast as the C language.
Python is easy to learn, but its performance is much inferior to that of some compilation languages (such as C). Here, you can use C and Python to compile the Fibonacci series computing program, and calculate the running time:

C Language Program
Copy codeThe Code is 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;
}

Written in Python
Copy codeThe Code is as follows:
Def fib (n ):
If n <2:
Return n
Else:
Return fib (n-1) + fib (n-2)
Fib (40)

Running time
Copy codeThe Code is as follows:
$ Time./fib
3.099 s
$ Time python fib. py
16.655 s

We can see that the running time is still a little different. The difference here is about five times. Now we will introduce Psyco:

Psyco is an extension module of the Python language. It can perform professional algorithm optimization on program code in real time and improve the program execution speed to a certain extent, especially when the program has a large number of cyclic operations. It was first developed by Armin Rigo and subsequently maintained and improved by Christian Tismer.

Psyco can run on 32-bit GNU/Linux, BSD, Mac OS X, and Microsoft Windows platforms. Psyco is written in C language and only coded for the 32-bit platform. Development has been stopped and replaced by PyPy. PyPy also provides support for 64-bit systems. Psyco can be automatically optimized when the Python interpreter compiles the code. It is implemented using C and some special optimizations are made for loop operations. After these optimizations, the program performance will be improved, especially in cross-platform environments.

Install Psyco
Copy codeThe Code is as follows:
Sudo apt-get install python-psyco

You can also download the installation package from the official website and install it with easy install.

Use the Psyco Module
Copy codeThe Code is as follows:
Import psyco
Psyco. full ()
 
Def fib (n ):
If n <2:
Return n
Else:
Return fib (n-1) + fib (n-2)
Fib (40)

Running result
Copy codeThe Code is as follows:
$ Time python fib. py
3.190 s

Improve your code

Now I add the following scripts to most of my Python code to use Psyco to increase the running speed:
Copy codeThe Code is as follows:
Try:
Import psyco
Psyco. full ()
Failed t 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.