Image Source: pixabay.com
How to Make Python Fast as Psyco?
Hey Guys, welcome to my next blog on 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 Psyco is? If at this point you think 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? It makes python fast. Confused? Yes, I, too, was confused when I heard that for the first time. It 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 it runs for a good amount of time, creating many codes and everything, 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. It is just a python fast extension module that speeds up the execution of a python run code. So, you may be confused right now; is the module making Python code run faster? Yeah, exactly that’s what it is. Let’s get to the bigger picture.
Python and its True Nature
If you have coded in Java or C before, you might think that python run is extremely fast than the others. But that’s where you are wrong. In terms of raw performance, it is definitely slower than C, Java or C#. But it 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, it runs 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 – This 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 versions 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, it 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 this it 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 your code’s runtime complexity.
To understand python psyco, one needs to understand python’s eval_frame() function. Python psyco converts python’s eval_frame function to a compound evaluation unit, and it uses lots of memory when doing this. Using psyco is far easier than explaining it.
To simply use it, 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 behaviour more precisely:
psyco.bind(somefunc) # or method, class
newname = psyco.proxy(func)
If this stuff does 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 behaviour, but that’s how it works.
In reality, it 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 it makes your programs as fast as C, there are some limitations and cons to them. Following are some, which I have listed below:
- It is now deprecated, unmaintained and dead. Replacement is PyPy
- It has a lot of issues with even python 2.7. But it works far better and faster in v2.5
- It 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 the 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. It works like a Just In Time compiler, but PyPy has a Just in time compiler of its own. Where it 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 in 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 with.
This has been a guide to Python fast. Here we discussed what psyco and its true nature are and How to Make Python Fast as Psyco. You can also go through our other suggested articles to learn more –