Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have many times seen various benchmarks that show how a bunch of languages perform on a given task.

Always these benchmarks reveal that Python is slower then Java and faster than PHP. And I wonder why is that the case.

  • Java, Python, and PHP run inside a virtual machine
  • All three languages convert their programs into their custom byte codes that run on top of OS -- so none is running natively
  • Both Java and Python can be "complied" (.pyc for Python) but the __main__ module for Python is not compiled

Python and PHP are dynamically typed and Java statically -- is this the reason Java is faster, and if so, please explain how that affects speed.

And, even if the dynamic-vs-static argument is correct, this does not explain why PHP is slower than Python -- because both are dynamic languages.

You can see some benchmarks here and here, and here

share|improve this question

closed as not constructive by ZJR, Jarrod Roberson, gnat, Yannis Rizos Nov 16 '12 at 8:47

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Regarding Python vs. PHP: it's just a quality of implementation issue, most likely. –  Charles Salvia May 3 '12 at 13:57
8  
@good_computer Most benchmarks are very badly done. There was another recent one (I don't think you've linked) that most people who reviewed it complained that the language it claimed to be "fastest" simply had the best optimized code. This is usually done unconsciously by someone not as familiar with the languages that end up deemed "slow", so they don't realize they're writing better code in the ones they've found to be "fast". –  Izkata May 3 '12 at 14:57
    
@good_computer It seems to me that you are claiming something, as your question includes the text "Always these benchmarks reveal that Python is slower then Java and faster than PHP" and "PHP is slower than Python". Removing those quotes and rephrasing the question to be language agnostic could get it reopened. –  briddums May 3 '12 at 15:08
    
This question is really biased: (1) referring to non-authoritative benchmarks conducted on very naively non-optimized code written by novice programmers in languages they don't master (as dissected in the respective comment threads) and (2) built upon misconceptions about interpreted/bytecode languages (php/python are interpreted, java's bytecoded, python cache files are abstract syntax trees, not bytecode) and the state of the three languages (there are compiled versions of both python and php - python's are more mature, compiled php, though, runs facebook) –  ZJR Nov 16 '12 at 4:22
add comment

5 Answers

up vote 17 down vote accepted

JVM code can be JIT-compiled efficiently, using a trivial (and fast) ad hoc compiler. But the same would be exceptionally hard for PHP and Python, because of their dynamically typed nature. JVM translates to a fairly low level and straightforward native code, quite similar to what would a C++ compiler produce, but for the dynamic languages you'd have to generate dynamic dispatch for literally all the basic operations and for all the method calls. This dynamic dispatch is the primary bottleneck for all the languages of this kind.

In some cases it is possible to eliminate the dynamic dispatch (as well as the virtual calls in Java) using a much more complicated tracing JIT compiler. This approach is still in its infancy, not doing too much of an abstract interpretation, and such a compiler is likely to choke on eval calls (which are very typical for the dynamic languages).

As for the difference between Python and PHP, the latter is just of a much lower quality. It could run faster in theory, but it never will.

share|improve this answer
1  
Why is JIT "exceptionally" hard for dynamic languages? Look at v8 or TraceMonkey in JavaScript world -- there JIT works just fine. –  good_computer May 3 '12 at 13:58
5  
@good_computer, tracing JITs are notably more complex than normal, ad hoc JITs, and they're still performing much slower than JITs for the statically typed languages. A proper tracing JIT would involve a full-blown abstract interpretation, and it would choke on each and every eval call. –  SK-logic May 3 '12 at 14:06
1  
There's probably a hundred or so engineers inside Oracle's HotSpot compiler team that would disagree about the "trivial" part :-) –  Jörg W Mittag May 3 '12 at 14:38
1  
@JörgWMittag, of course, HotSpot is not that straightforward, it does a bit of static analysis, it is using the runtime profiling results, but still it is much simpler than a proper tracing JIT. And, I'd say, HotSpot is overcomplicated and its implementation is, to put it politely, a little bit too verbose. –  SK-logic May 3 '12 at 14:41
1  
@Frank Shearar, an ad hoc JIT for a dynamic language is as trivial as for a statically typed one (see LuaJIT for example). OTOH, an efficient JIT is a totally different thing. –  SK-logic May 5 '12 at 12:31
show 5 more comments

There is a general problem with this question in that it is too absolute. It does not really make sense to say "language X is faster than language Y". A computer language itself isn't "fast" or "slow" because it is merely way of expressing an algorithm. The actual question should be something on the order of "why is the implementation X1 of language X faster than implementation Y1 of language Y for this particular problem domain?"

Some speed differences are certainly going to fall out of the language itself as certain languages are easier to implement certain domains than others. But much of what makes an implementation fast isn't the language. For instance, you can't really say "Python is slower than Java" without considering whether you are talking about CPython, IronPython or PyPy. This is particularly true for languages that use a VM as the speed is going to be directly impacted by the quality of the VM.

As an aside, I work with a system that for various reasons can't use JIT on our device with a very popular JavaScript VM that normally supports it. This means that our JavaScript runs far, far slower than it would on a PC with a similar processor. That one change, which is not directly related to the language itself, takes JavaScript from being "a few times slower than C++" to being "orders of magnitude slower than C++" for the tasks we care about.

Also consider is that languages differ in performance characteristics in ways that are not directly comparable. Too many benchmarks just translate a program from language A to language B and don't take into account that languages differ in what features are fast. (You can see this in any reasonable benchmark comparison such as those you link to as they often have notes like "thanks to so-and-so for showing me how to implement it in language Foo.)

For instance, Take this Java code:

for(int i=0;i<10;i++) {
    Object o = new Object;
    doSomething(o);
}

It would be tempting to "rewrite" this in C++ and compare run times:

for(int i=0;i<10;i++) {
    Object *o = new Object;
    doSomething(o);
    delete(o);
}

The thing is, any competent C++ programmer will immediately see that in C++, this is not the fastest way to do something. You can easily speed things up by changing it to be more appropriate to C++:

for(int i=0;i<10;i++) {
    Object o;
    doSomething(&o);
}

The point is not that C++ can be fast but rather than writing benchmarks to compare languages is really, really hard. To do it appropriately, you have to be an expert in both languages, and write from scratch in both languages. Even then, you can easily run into to areas where one language excels at a particular task. For example, I can write a version of Towers of Hanoi in C++ that will run faster than Java on any reasonable compiler. I can do that by essentially cheating, using C++ templates, evaluated at compile time (http://forums.devshed.com/c-programming-42/c-towers-of-hanoi-using-templates-424148.html)

The point there is not that I could say that "C++ is faster than Java" because my program returned instantly while the Java version ran for minutes (and hoping nobody noticed my program took a half hour to build.) The point is that for this vary narrow case, C++ is faster. For other narrow cases it might be the other way around. So it isn't "C++ is faster", it is "C++ is faster in instances where you can evaluate the expression at build time using templates." Less satisfying, but true.

Speed differences in languages are mostly about the implementation. Compiled languages are going to be faster than interpreted languages. Compiling to native code is going to be faster than compiling to byte code. This will have much more effect than questions like whether than language is statically typed or not. And of course, good implementations are going to be faster than bad ones.

And don't forget that good programmers are going to produce faster code than bad programmers, often to an extent that quite outweighs language differences.

share|improve this answer
5  
+1 for last sentence. –  good_computer May 5 '12 at 3:20
    
+1 for correcting the perspective –  Jimmy Hoffa Nov 16 '12 at 4:30
add comment

It has to do with the quality of the compiler, java's compiler has been continually optimized for much longer, and optimization is more important because all code is compiled for Java. I'm not sure the exact reason for python to be faster than PHP, but I would bet its because of Google's influence with Python.

share|improve this answer
6  
Why was this downvoted? This is exactly the answer: performance is purely a matter of research and engineering effort, and thus ultimately money. The companies which produce Java implementations just happen to be richer than the ones producing Python or PHP implementations. That's all. –  Jörg W Mittag May 3 '12 at 14:40
    
Furthermore, I'm pretty sure CPython optimizations are not accepted if they make the code too hard to read and only enhance performance by very little. –  cgt May 4 '12 at 7:42
2  
+Jörg W Mittag: I disaggree. Some language features can be very difficult to implement performantly, therefore they make the creation of an efficient implementation very difficult or next to impossible. On the other hand, it's trivially easy to create an "efficient" implementation of the "Assembler" language. –  user281377 May 4 '12 at 7:42
    
@ammoQ I suspect that a lot of that comes down to type systems and in particular to the ability to know exactly what type you've got and what the exact semantics of the permissible operations are. Dynamic languages gain flexibility from their nature, but make it harder to do the type proofs (and so to compile to safe hyper-fast code). –  Donal Fellows May 4 '12 at 12:49
1  
@DonalFellows Exactly my thought. The less is known at compile time, the more has to be figured out during runtime. –  user281377 May 5 '12 at 20:41
add comment

Why Java is the fastest:

Statically typed + JIT compile + --server flag to aggressively recompile running code.

Why Python is faster than PHP:

Python might be a dynamic language, but it is still strongly typed. This means that the structures you code are capable of runtime optimization.

Why PHP sucks:

It's basically javascript on the server (no multithreading support, totally dynamic, loosely typed).

In essence, the more the compiler knows about your code, the more it can optimize. Java is fully optimizable before it is run, and while it is running. Python is optimizable while it is running, and PHP is, well, terrible. Facebook actually transpiles their PHP to C before it gets on the server.
https://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/

share|improve this answer
    
Actually javascript on the server is Node.JS and from what I understand (though I won't substantiate this) the V8 engine outperforms PHP in general (though probably not by a ton). Further you should mention Python can be compiled to native (How does it perform compared to Java then?) –  Jimmy Hoffa Nov 16 '12 at 4:27
    
I haven't used python extensively enough to help you there, but I can say that nodejs running V8 has support for native C extensions (though crossing the JS/C boundary is supposedly slow), plus it can take advantage of Google's JIT compiler... I wouldn't be surprised if node is faster than both python and php. Here's a benchmark (flawed as most are) blog.famzah.net/2010/07/01/… Note that the java looked slower than JS until a commenter pointed our flaws in the benchmark... So, take it with a grain of salt. :) –  Ajax Nov 18 '12 at 22:03
    
That said, node and php are also both singlethreaded, and unless you like setting up cluster proxies (like haproxy), I wouldn't touch either of them in a serious production environment. –  Ajax Nov 18 '12 at 22:04
add comment

The benchmarks are rather skewed in favour of heavy mathematical programming.

It not surprising that Python is pretty good at complex math if you consider where and why it was first written.

PHP on the other hand was written to serve web pages, it can do other things but web pages is what its best at and its on a par or better than Java at this task.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.