views:

677

answers:

9

I'm a Java programmer and if theres one thing 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 then Java, or C# and how that compares to C/C++ (which I figure it'll be slower than)?

+3  A: 

This might help

keyle
No - half-a-dozen programs written by one programmer, for only one (really quite peculiar) task, really doesn't help.
igouy
yeah I understand that. There is always stuff that a language is going to be better at than another. On the other hand the sample program is quite a day-to-day piece which 'might help' Bob.
keyle
'might help' Bob - or might mislead Bob. The Java program runs for only 0.16 seconds but that's hard to notice because all the times have been munged. The C++ time is given as "approx 0" - really how helpful is that?
igouy
+14  A: 

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.

sfussenegger
+2  A: 

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

Richard Nienaber
igouy
Woops. Thanks for the alternative link.
Richard Nienaber
+16  A: 

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 (!)

lemonad
I like your term "fast enough". This has certainly been my experience with Python as well.
iWerner
+1 for "fast enough" - people tend to spend way too much time talking about performance anyway
sfussenegger
igouy
"fast enough" for everything that doesn't need to be fast :-)
igouy
+2  A: 

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.

ang mo
They forgot to say hat sometimes a slowdown is achieved xD
fortran
Unfortunately Psycho hasn't been developed for several years and will never work on 64 bit platforms.
thrope
@thrope: That's because psyco is deprecated in favour of pypy, which has recently exceeded the performance of psyco when the x86 JIT backend finally implemented ints and floats. pypy is a JIT generator.
cjrh
Psyco will give you more Java-like performance. You will get slower start times and higher memory usage in exchange for faster algorithms.
Jason Baker
A: 

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.

Olivier Pons
I can't agree with your claim that Java code is hard to test. Incremental compilation and hot code replacement make testing Java really fast - I normally don't even note it. Furthermore it's not silly to say that Java can't be faster than C. The JVM will try to compile hot code areas to machine code. This machine code will be heavily optimized for the machine it's running on. For C you *could* do this at compile time - but you can't always predict the machine the code is going to run on, so it might be compiled for i386 to satisfy most clients.
sfussenegger
I do agree with you about the Java stuff. But for C it's always about who compiles it. If you make a good makefile, and you develop properly, it's not possible at all that the same compiled Java code will be faster. Not at all. The only possible cases it when you have the same developper who develops in Java and code a bad code with a bad makefile and compares the speed. It's all about the developper's skills. The only thing you can say is "Java is sometimes faster than C if the developper doesn't know C"!. But C is **always** to the worst as fast as Java.
Olivier Pons
A: 

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.

MAK
igouy
+1  A: 

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_;
public:
    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++)
      check_wrap(++p);
    }
public:
    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);
      check_wrap(current);
     }
     return &(*current);
    }
};
int main() {
    const int ITER = 1000000;  
    clock_t start = clock();
    for(int i = 0 ; i <ITER; i++) {
        chain c(40);
        c.kill(3);  
    }
    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.

Jerry Coffin
Indeed, although your algorithm is likely broken. As far as the comparison between Java and C++ goes, it probably only shows that small object allocation/deallocation is not a strength of C++. A straightforward and some 3-4 times shorter approach with a vector (even if allocated anew for each iteration) and erase showed that the C++ code could be trivially made some 5-6 times faster, without anything fancy. I suspect that benchmark is similarly suboptimal for other languages, possible for Java as well.
UncleBens
It's probably sub-optimal for Java, but with Java it probably doesn't make nearly as much difference. One part of this code is broken (I forgot that vector fills with a copy of a single object instead of creating each object independently, so they all get a `count_` of 1. Other than that, I believe it works correctly.
Jerry Coffin
A: 

(Substitute {LanguageImplementation} with, e.g. Python if you like.)

The {LanguageImplementation} approach to speed:

  • Write your code in {LanguageImplementation}; if {LanguageImplementation} has an interpreter, develop your functions in the interpreter; as functions test successfully, copy them over to your source file. Working with an interpreter can produce very fast development.

  • Run your application, and profile to find which bit is slow

  • For all I/O bound situations, it is likely that there will be only a handful of small bits that use most of the system resources; if necessary rewrite only those bits in optimized C or asm.

This can make {LanguageImplementation} very fast.

cjrh