views:

641

answers:

11

This is something I've always wondered: Why is PHP slower than Java or C#, if all 3 of these languages get compiled down to bytecode and then executed from there? I know that normally PHP recompiles each file with each request, but even when you bring APC (a bytecode cache) into the picture, the performance is nowhere near that of Java or C# (although APC greatly improves it).

Edit: I'm not even talking about these languages on the web level. I am talking about the comparison of them when they're number crunching. Not even including startup time or anything like that.

Also, I am not making some kind of decision based on the replies here. PHP is my language of choice; I was simply curious about its design.

A: 

A wild guess might be that JAVA depends on some kind of "application" server, while PHP doesn't -- which means a new environnement has to be created each time a PHP page is called.

(This was especially true when PHP was/is used as a CGI, and not as an Apache module or via FastCGI)


Another idea might be that C# and JAVA compilers can do some heavy optimisations at compile time -- on the other side, as PHP scripts are compiled (at least, if you don't "cheat" with an opcode cache) each time a page is called, the compilation phase has to be real quick ; which means it's not possible to spend much time optimizing.


Still : Each version of PHP generally comes with some amelioration of the performances ; for instance, you can gain between 15% and 25% of CPU, when switching from PHP 5.2 to 5.3.

For instance, take a look at those benchmarks :


One important thing, also, is that PHP is quite easy to scale : just add a couple of web servers, and voila !

The problem you often meet when going from 1 to several servers is with sessions -- store those in DB or memcached (very easy), and problem solved !


As a sidenote : I would not recommend choosing a technology because there is a couple of percent difference of speed on some benchmark : there are far more important factors, like how well your team know each technology -- or, even, the algorithms you are going to use !

Pascal MARTIN
+6  A: 

I'm guessing you are a little bit into the comparing of apples and oranges here - assuming that you are using all these languages to create web applications there is quite a bit more to it than just the language. (And lots of the time it is the database that is slowing you down ;-)

I would never suggest choosing one of these languages over the other on the basis of a speed argument.

Simon Groenewolt
+1 Speed is most of the time about how well written the code is, not about the very relative differences in speed measured in silly benchmarks. Good developers can make any of these platforms fly.
ChristopheD
Exactly!
Gary Willoughby
I'm not choosing a language. PHP is my language of choice (for web, at least). I was simply curious.
ryeguy
It's not really the languages that make the speed difference, but the way their (compiled) code is executed. As others have mentioned, the java and .NET virtual machines have been tuned quite well, it /could/ be that the way php is executed is less optimized. If this is a theoretical/language limit or just a matter of work to do for the php guys is probably up for debate. (Seeing the recent increase in javascript performance I think it is quite possible to speed up php execution)
Simon Groenewolt
A: 

According to wikipedia, PHP uses The Zend Engine, which does not have a JIT.

Ken
+2  A: 

The biggest single reason is that Java's HotSpot JVM and C#'s CLR both use Just-In-Time (JIT) compilation. JIT compilation compiles the bytecodes down to native code that runs directly on the processor.

Also I think Java bytecode and CIL are lower-level than PHP's internal bytecode which might make alot of JIT optimizations easier and more effective.

mcjabberz
A: 

Depends on what you want to do. In some cases, PHP is definitely faster. PHP is (pretty) good at file manipulation and other basic stuff (also XML stuff). Java or C# might be slower in those cases (though I didn't benchmark).

Also, the PHP output (HTML or whatever) needs to be downloaded to the browser, which also consumes time.

Also, the speed of Java / C# is very much depending on the machine it runs on (which could be multiple). Java / C# could be slow on your computer, while PHP just runs on one server from which it is available and is always as fast as the server is (except for download times, etc.).

I don't think they are comparable in a general manner. I think you need to take a task, which you could be accomplished with those three programming languages, and then compare that. That is basically always what you should do when choosing a programming language; find the one that fits the task. Don't shape the task until it fits the programming language.

MiRAGe
Your comparison doesn't make much sense. The OP was asking about simple, raw number-crunching performance, and why a method in PHP would be slower than the same method in Java/C#.
musicfreak
Well, it didn't make much sense because comparisons are hard to make. Another, probably unsatisfying, comparison is to ask 'Why are pears sweater then apples?'. They are both fruit (like PHP/Java are both programming languages) but there is no real answer to the question simply because they differ to much.
MiRAGe
I disagree. You can clearly explain why pears are sweeter than apples (if you know your fruits well enough). The languages may have different uses, but that shouldn't stop you from being able to compare them side-by-side.
musicfreak
+3  A: 

Both Java and C# have JIT compilers, which take the bytecode and compile into true machine code. The act of compiling it can take time, hence C# and Java can suffer from slower startup times, but once the code is JIT compiled, its performance is in the same ballpark as any "truly compiled" language like C++.

RichieHindle
+1 Your answer is basically the same as mine. I don't see why it should have been down-voted
mcjabberz
A: 

As many people have already stated it before me: parsing and interpreting strings, that PHP does, will never be as fast as executing machine code.

kahoon
A: 

The only thing that makes PHP slower is poor developers writing poor code. But I guess that can be said about any development language.

Josh
That can be said about Java and C# as well.
musicfreak
+15  A: 

One reason is the lack of a JIT compiler in PHP, as others have mentioned.

Another big reason is PHP's dynamic typing. A dynamically typed language is always going to be slower than a statically typed language, because variable types are checked at run-time instead of compile-time. As a result, statically typed languages like C# and Java are going to be faster at run-time, but take much longer to compile than a dynamically typed language like PHP. A JIT compiler makes this less of an issue for dynamically typed languages, but alas, PHP does not have one.

musicfreak
+1 for mentioning dynamic typing. Good point!
mcjabberz
I'm surprised this wasn't mentioned earlier! IMO this is a bigger performance issue than not having a JIT compiler, although I guess that depends on many factors.
musicfreak
Well dynamic typing definitely makes JIT compiling and general runtime optimizations more interesting!
mcjabberz
PHP bytecode caches help so much since they're essentially caching the source -> bytecode translation, but there is not a cached or optimized translation from bytecode -> machine code. It just gets interpreted at that more basic level.
Justin
A bytecode cache still doesn't help with the dynamic typing issue. And the bytecode still has to be interpreted, like you said. So it still can't compete with a statically-typed, compiled, JITed language.
musicfreak
A: 

There is no way an interpreted language can be faster than a compiled language or even a JIT language under trivial conditions.

Unless your test program consists of printing out "Hello Worlds" if you are concerned about speed, stick with C# or Java.

TC Wu
A: 

Stumbled upon real-world benchmarks comparing PHP to Java and C#:

http://g-wan.com/

And PHP is actually much faster than Java and C#...

Sepp
No, PHP is much slower than C# or Java. That worthless page you linked to is comparing servers, not languages.
ryeguy