Image Source: pixabay.com
How to Make Python Fast as Psyco
Hey Guys, welcome to my next blog of Python. But today I won’t be talking about just Python fast. So before I proceed, I would assume that you already know bits and *bytes about python. If you are a beginner, then you can search for my other blogs, learn about python, then get back to us. now we are discussing the topic Python fast as Psyco.
If you are an experienced programmer in another field, and you think that you know the basics and now you can read this blog, then just ask yourself one question, do you know what is Psyco? If at this point you are thinking that this “Psyco” means crazy, then again, this stuff is not for you.
You still have avoided a lot of basic stuff or you are just kidding yourself by telling that you are an experienced programmer. Trust me, I will try to make this blog as easy as possible and will try not to offend anyone. But if you don’t have the basics right, it will just be harder for you to understand this.
Leave the hard part aside, you make even start to think that python fast stuff is much harder than any other programming which is absolutely wrong. So now we know what’s what, let’s get on to it, shall we?
What is Psyco?
So, what is Psyco? Psyco makes python fast. Confused? Yes, I too was confused when I heard that for the first time. Python is already fast, then why use psyco? That’s why I said. This blog is not for noobs and beginners. If you have coded in python run for a good amount of time, creating a large number of codes and everything, then you will know that python fast needs compact and strict coding. With the proper type of coding, you can make it fast as well as compact.
But it needs time and stuff. You cannot create compact and bug-free codes out of the blue. And that’s where Psyco comes into existence. Psyco is just a python fast extension module which speeds up the execution of a python run code. So, you may be confused right now, module making a Python code run faster? Yeah, exactly that’s what it is. Let’s get to the bigger picture.
4.8 (2,630 ratings)
Python and its True Nature
If you have coded in Java or C before, you might think that python run is extremely fast that the others. But that’s where you are wrong. In terms of raw performance, python is definitely slower than C, Java or C#. But python is not known for having speed in raw performance.
Only beginners or noobs do raw coding. Professional people do proper coding along with proper indentations*. When you compare a properly compiled code and measure total memory usage, initial startup time, load time, then at that point in time, python run is extremely fast.
Besides, if you are trying to write codes for creating a server or stuff, Java is extremely fast, even faster than C. This is possible as the Java Virtual Machine might compile hot byte-code to machine code. While doing this, it may take full advantage of each and every feature of the CPU.
This typically isn’t the case with C, at least till you leave your laboratory environment. Now just assume, distributing a dozen of optimized builds to your clients – that simply won’t work.
So, now let’s get back to our main point if startup time is an issue (which isn’t an issue for a server application for instance) Java might not be the best alternative. It may also depend on where your hot code areas are, for example: If they are within the native libraries with some Python fast code to simply glue them together, you will be able to get C like performance with Python fast as well. But still, scripting languages will tend to be slower though – at least most of the time.
Psyco – Is Exactly how it sounds
Yeah, you read that right. Now, let’s get back to our main topic. Now, this is what the psyco developers define in their web page:-
“Think of Psyco as a kind of just-in-time (JIT) compiler, a little bit like what exists for other languages, that emit machine code on the fly instead of interpreting your Python program step by step. The difference with the traditional approach to JIT compilers is that Psyco writes several version of the same blocks (a block is a bit of a function), which are optimized by being specialized to some kinds of variables (a “kind” can mean a type, but it is more general). The result is that your unmodified Python programs run faster.
2x to 100x speed-ups, typically 4x, with an unmodified Python interpreter and unmodified source code, just a dynamically loadable C extension module.”
In short, to rephrase this in a simple statement, Psyco will give you more Java-like performance. You will get slower start times and higher memory usage in exchange for faster algorithms.
Having said that, there are certain things one should remember when testing languages like Java and Python. The code in these languages can often be speeded up significantly by using constructions more suited to the language (e.g. list comprehensions in Python fast, or using char and String Builder for certain String operations in Java).
Moreover, for Python fast, using Psyco can greatly boost the speed of the program. And then there is the whole issue of using appropriate data structures and keeping an eye on the runtime complexity of your code.
To understand python psyco, one needs to have a good understanding of python’s eval_frame() function. What python psyco does is converts python’s eval_frame function to a compound evaluation unit, and psyco uses lots of memory when doing this. Using psyco is far easier than to explain it.
To simply use psyco, download the psyco module from sourceforge.net and then keep this code at the start of your very first code:
import psyco ; psyco.jit()
from psyco.classes import *
And then, you can use the following command to target psyco’s behavior more precisely:
psyco.bind(somefunc) # or method, class
newname = psyco.proxy(func)
If this stuff do not work with you at first, then you need to be patient. It requires trial and error. You cannot directly expect your program to run like Usain Bolt. Spare me the sarcastic rude behavior, but that’s how it works.
In reality, python psyco does not change or modify your code to make it smarter. It hardly profiles your code to be more specific. It does minimal optimization to make it as near to machine code as possible.
Although python psyco makes your programs as fast as C, there are some limitations and cons to them. Following are some, which I have listed below:
- Psyco is now deprecated, unmaintained and dead. Replacement is PyPy
- Psyco has a lot of issues with even python 2.7. But it works far better and faster in v2.5
- Psyco does not work on 64-bit machines. But one can install a virtual machine with i386 architecture and make it work though.
- Mac OSX by default comes with python 64-bit. One will need to recompile python x86 from source to make Psyco work.
Even though having so many issues, people still use Psyco instead of PyPy. But again, if you ask me, PyPy is an awesome alternative to Psyco. Psyco works like a Just In Time compiler, but PyPy has a Just in time compiler of its own. Where Psyco uses lots of memory, PyPy uses much less memory than Psyco. PyPy is even more compatible with frameworks like Django and Twisted.
So after all this no matter what I say, it’s to our own use. People with a background of Psyco will always suggest you to use Psyco, whereas people wanting speed would need PyPy. But let me quote something.
“Even though you feel good enough with motorcycles like R1 or Hayabusa, sometimes riding a Harley Davidson has its own pleasure”
It works in the same way with Psyco and PyPy. So, in the end, without much ado, what I would suggest is to use both in the start, and then continue with whichever you feel much comfortable.
Here are some articles that will help you to get more detail about the Python Fast And python psyco so just go through the link.