How fast is Python?

I'm a Java programmer and if there's one thing that I dislike about it, it would be speed. Java seems really slow, but a lot of the Python scriptsprograms I have written so far seem really fast.

So I was just wondering if Python is faster than Java, or C# and how that compares to C/C++ (which I figure it'll be slower than)?


In terms of raw performance, Python is definitely slower than Java, C# and C/C++. However, there are other things that matter for the user/observer such as total memory usage, initial startup time, etc. For most things, Python is fast enough ;)

This site lets you compare different programming languages to each other. It uses simple bar graphs to show speed, memory usage, etc.

If you're interested, you can take a look at the much anticipated Unladen Swallow project that's striving to improve the performance of Python to five times that of CPython (!)

This totally depends on the usecase. For long running applications (like servers), Java has proven to be extremely fast - even faster than C. This is possible as the JVM might compile hot bytecode to machine code. While doing this, it may take fully advantage of each and every feature of the CPU. This typically isn't possible for C, at least as soon as you leave your laboratory environment: just assume distributing a dozen of optimized builds to your clients - that simply won't work.

But back to your question: it really depends. E.g. if startup time is an issue (which isn't an issue for a server application for instance) Java might not be the best choice. It may also depend on where your hot code areas are: If they are within native libraries with some Python code to simply glue them together, you will be able to get C like performance with Python as well.

Typically, scripting languages will tend to be slower though - at least most of the time.

If you want speed in Python, especially for complex algorithms, Psyco usually helps. From their webpage:

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.

Strangely, it hasn't been mentioned in the above links.

Here's another stackoverflow question that seems more complete: You might also look at the computer language shootout.

It is very hard to make a truly objective and general comparison of the runtime speed of two languages. In comparing any two languages X and Y, one often finds X is faster than Y in some respects while being slower in others. For me, this makes any benchmarks/comparisons available online largely useless. The best way is to test it yourself and see how fast each language is for the job that you are doing.

Having said that, there are certain things one should remember when testing languages like Java and Python. Code in these languages can often be speeded up significantly by using constructions more suited to the language (e.g. list comprehensions in Python, or using char[] and StringBuilder for certain String operations in Java). Moreover, for Python, 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.

I think Keyle's answer (among others) brings home a basic point: a huge amount depends on how you do things. That link gave two answers for C++, but I have a hard time believing that anybody would normally write C++ that's much like either one. My first attempt would look something like this:

#include <iostream>
#include <vector>
#include <time.h>

class person { 
    int count_;
    static int current_;
    person() : count_(++current_) {}
    int count() { return count_; }
int person::current_ = 0;
typedef std::vector<person> plist;
class chain {
    plist people_;
    void check_wrap(std::vector<person>::iterator &p) {
        if (p==people_.end())
            p = people_.begin();
    void advance(std::vector<person>::iterator &p, int places) {
        for (int i=0; i<places; i++)
    chain(int length) : people_(length) {}
    person *kill(int n) { 
        plist::iterator current = people_.begin();
        while (people_.size()>1) {
            advance(current, n);
            current = people_.erase(current);
        return &(*current);
int main() {
    const int ITER = 1000000;  
    clock_t start = clock();
    for(int i = 0 ; i <ITER; i++) {
        chain c(40);
    clock_t end = clock();
    std::cout << "Time per iterator: " << (((end - start) /(double)CLOCKS_PER_SEC/ITER)*1000000 << " microseconds.\n";
    return 0;

(For portability I've used clock() instead of gettimeofday, but anybody who wants can easily change that back).

There are a couple of points about this that strike me as interesting. First, the code has gotten a lot shorter -- in fact, competitive as the shortest code shown. Second, the code has gotten quite a bit faster -- probably faster than anything but the specially optimized version in C++.

Finally, at least to me it seems like the code has gotten quite a bit easier to read and understand. To me, his 'shout()' seemed quite confusing, as was making a 'Person' really a node in a linked list of Person objects, with Chain handling some of the linked-list management, but 'Person' also doing linked-list things along with 'Person' things.

That doesn't necessarily (or directly) tell us about the speed of Python, but I think it gives an idea about the quality of many benchmarks you can find on the web. Writing almost any benchmark that's meaningful and accurate is tremendously difficult -- and trying to compare across languages is one of the most difficult of those.

It's a question you can't answer properly, because it all depends when it has to be fast. Java is good for huge servers, it's bad when you have to re-compile and test a lot of times your code (compilation is sooooooo slow). Python doesn't even have to be compiled to test !

In production environment, it's totally silly to say Java is faster than C... it's like saying C is faster than assembly.

Anyway it's not possible to answer precisely : it all depends on what you want / need.

For the Python, velocity depends also for the interpreter implementations... I saw that pypy is generally faster than cpython.

Need Your Help

Qt interfaces or abstract classes and qobject_cast()

c++ qt interface qt4 qobject

I have a fairly complex set of C++ classes that are re-written from Java. So each class has a single inherited class, and then it also implements one or more abstract classes (or interfaces).

NodeJS plugin in IntelliJ Community Edition does not work

node.js intellij-idea

I have tried to use the NodeJS plugin in IntelliJ IDEA CE but can't get it to work. After the plugin have been installed and the IDE restarts, it says that it can't find Javascript plugin which is ...