Could someone tell me why Java is faster than PHP

I’ve been doing a lot of reading because of Harry Fuecks Pro PHP Rant
In my reading, I tripped up on something.
George Schnossnagle says here that he believes Java is faster than PHP.

Notice how PHP being naturally slower than Java (something I believe to be true) comes in last?

I always believed so too because (don’t laugh)

  1. Java is used in Enterprise systems so it must be fast
  2. I thought because Java is compiled … it would be faster.

Well after doing a little research over at wikipedia
I found this.

This is achieved by most Java compilers by compiling the Java language code “halfway” to [url=“http://en.wikipedia.org/wiki/Bytecode”]bytecode (specifically [url=“http://en.wikipedia.org/wiki/Java_bytecode”]Java bytecode) —simplified machine instructions specific to the Java platform. The code is then run on a [url=“http://en.wikipedia.org/wiki/Virtual_machine”]virtual machine (VM), a program written in native code on the host hardware that [url=“http://en.wikipedia.org/wiki/Interpreter_%28computing%29”]interprets and executes generic Java bytecode.

Apparently compiled Java bytecode is interpreted too (just like PHP) so what makes Java faster than PHP?

Is it the compiler or JVM runtime incorporated into J2EE (am I throwing around words I do not understand?)

I really would like to know the answer to this.

Two things. JavaVM’s are developed by sun, who have brainpower (and shiny goldpieces) enough to make blindingly fast interpreters. And they have spend a long time doing it.
Second, PHP compiles to bytecode on each request. This takes time. Further, since java bytecode is compiled in advance, the compiler can afford to optimize the bytecode.

Additionally, PHP lives on a pper-request basis, where java servlets are kept in memory between requests.

Prove that Java is “faster” than PHP in the general sense. “I think PHP is faster because of x” or “I think java is faster because of Y” doesn’t work. Make two applications that run similiar logic and test them by sending several hundred simultaneous requests at each. Come back and report the results.

In my experience, Java is slower than PHP for the stuff that you should be using PHP for as long as you’re using an opcode cache. Even without the opcode cache, PHP frequently beat out most of the Java stuff anyway.

And, no, simple “create x thousand variables and echo the output” type of scripts don’t work. Why? Because that blatently ignores real world usage scenerios, namely the fact that a whole hell of a lot of the code that you use in PHP is actually mapped directly to C function calls, which is almost guaranteed to be faster than the java equivilents. Try doing something useful and then come back and say that Java is “faster” than PHP.

There are also scalability issues to consider. It’s a hell of a lot easier to build scalable PHP apps than it is scalable java apps.

Uh-oh. Something smells like burning.

?

Java is probably faster but you also have to look at the amount of resource it uses to be faster, ie: RAM.

In my experience, Java runs faster but uses more memory. However it provides stateful execution through servlets. Use the right tool for the right job.

The Java VM is more advanced than the PHP VM. Read about [URL=http://www-128.ibm.com/developerworks/java/library/j-jtp09275.html?ca=dgr-lnxw01JavaUrbanLegends]fast garbage collection and [URL=http://www.artima.com/designtechniques/hotspot.html]Just In Time Compilation.

One thing, though. All that JIT compilation, zillions of classes to load and shared state caches to warm up show up when you are loading a java desktop app – Slow. This doesn’t matter so much on a server, where you are running the same code over and over all the time and you never shut down the JVM.

Apparently compiled Java bytecode is interpreted too (just like PHP) so what makes Java faster than PHP?

Java isn’t “interpreted” in the same sense that php is, java’s VM is much more sophisticated than what PHP offers. Java hasn’t been an “interpreted language” for a long time (I forgot which version changed its status though). Anyhow java as things like a Hot spot compiler, which will compile “hot stops” of the program to machine code.
Theoretically its possible that this can make much better optimizations than when you say compile C to native code, as it has much more information about the system its running on.

In my experience, Java runs faster but uses more memory. However it provides stateful execution through servlets. Use the right tool for the right job.

Whats your basis for this claim? In a web environment java based applications work
MUCH differently than php apps do. That is to say with servlets you can save things between requests…if you do this of course you’ll use more memory. But you are using more meory because you are doing something different. Where as with php all memory allocated during a request is taken back. PHP’s interpreter does not use less memory that a VM for java.

PHP frequently beat out most of the Java stuff anyway

If PHP is performing better than your java stuff then you dont’ know how to write java.
Java as a language runs…much faster than php does. Furthemore in a web environment you can make optimizations that you cannot make in php, where as any optimization you do with php is either a standard feature with servlets/jsp or can easily be done.

It’s a hell of a lot easier to build scalable PHP apps than it is scalable java apps.

Maybe in an alternate universe? But certainly not this one.

I didn’t know there was any compilation going on with PHP at all. I checked that out and found this
That is really good to know.

I read this article and a couple of others … here’s a quote.

Faster is a non-starter, because Java is not so much faster than PHP that it makes much of a difference.
What it seems like to me, is that Java isn’t that much faster than PHP (if it is indeed faster).

PHP’s interpreter does not use less memory that a VM for java.
I can’t speak on this, because all my knowledge about this is theory (I programmed in Java a long time ago). However, it seems that Java loads a lot of stuff into main memory to run as quickly as it does and PHP doesn’t use as much memory. Whether this is significant enough is probably dependent on your server right?

It’s a hell of a lot easier to build scalable PHP apps than it is scalable java apps.

Maybe in an alternate universe? But certainly not this one
I’m reading “Moving to ASP.NET Web Development with VB.NET” by Harris and Mac Donald. I don’t code in VB.NET, but there was a big chapter on scaling Web applications that caught my eye. What they say in the book is borne out by George’s ideas in my first post. The design and architecture of the web app affect scalability a WHOLE lot more than the individual language. Yes? No? maybe?

Yes. An opcode-cache can boost performance quite a bit. I’ll recommend you look at PECL::APC.

I think it depends a lot on the type of application, you’re writing. If you are rendering 3d graphics, java probably beats the *** out of PHP. Webapps on the other hand, aren’t cpu-heavy - it’s a lot of i/o, and switching between i/o and cpu bursts. Theese things are more dependent on the OS than the interpreter, so the result are probably much the same for both.

I’m guessing here, but I would suppose that the same VM can stay alive for ever, like a deamon. It’s comparable to apache having to start up before PHP can be served.

That’s my view too. And java comes with a thorugh and very clever framework fo classes, which makes it much easier to write large scale apps. Java’s framework may be a killer for beginners, but it’s really valuable when you need to write bigger stuff. PHP on the other hand, is easily accesible, but doesn’t help you much after a certain point.

I think it depends a lot on the type of application, you’re writing. If you are rendering 3d graphics, java probably beats the *** out of PHP. Webapps on the other hand, aren’t cpu-heavy - it’s a lot of i/o, and switching between i/o and cpu bursts. Theese things are more dependent on the OS than the interpreter, so the result are probably much the same for both.
He He … I wonder who would actually write a 3D graphics app in PHP? :stuck_out_tongue:
I get your point though.

That’s my view too. And java comes with a thorugh and very clever framework fo classes, which makes it much easier to write large scale apps. Java’s framework may be a killer for beginners, but it’s really valuable when you need to write bigger stuff. PHP on the other hand, is easily accesible, but doesn’t help you much after a certain point.
Okay, so I’m not crazy and all this reading is making me a little bit knowledgeable. I agree with you about PHP though. I wrote this website from scratch in PHP. Don’t be deceived by the 2 or 3 pages it has, the CMS behind it is almost 3 or 4 times as big. It started out fine, but now it has become really tedious to use PHP to code it, and I don’t even think using OOP would really help. I’m actually thinking about rewriting the whole thing in .NET.

Semantic question here but …
Is it really called the PHP Virtual Machine or is it the PHP runtime?
Would those be one and the same thing?

However, it seems that Java loads a lot of stuff into main memory to run as quickly as it does and PHP doesn’t use as much memory. Whether this is significant enough is probably dependent on your server right?

It may be the case that java VMs use more “base” memory than the PHP interpreter I haven’t really bothered to look as in either case its not very big. I was referring to the amount of memory your application will use. But this is a little hard to compare…but I have tested roughly how much memory is used by PHP objects vs Java objects and java does much better. Also even the PHP people note that Java is better at object allocation/collection and warn against pretending (namely PHP5) as if it is java.

Webapps on the other hand, aren’t cpu-heavy - it’s a lot of i/o, and switching between i/o and cpu bursts. Theese things are more dependent on the OS than the interpreter, so the result are probably much the same for both.

With java web technologies you can greatly reduce this load, by using in memory caches. You can’t really do the same in PHP, although you can use things like memcached to do some of them. So this is only true when you app can’t take advantage of any sort of caching logic.

The design and architecture of the web app affect scalability a WHOLE lot more than the individual language. Yes? No? maybe?

Sure, but the language and the technologies built on it effect how you design the application. With java (and .NET) there are standard web frameworks that have proven to be scalable, there is no such thing in PHP. Although I think Zend is working on some framework…I’d be really suprised if it did well. Even the lack of good IDEs for PHP effect its scalability. To me at least, PHP is just a templating language trying in the last couple of years to be something else. Its very good to use when you are writing a thin client over some database…but to write any real “business logic” in it seems silly.

This doesn’t surprise me. OOP was an after thought in PHP 4 and Zend still haven’t gotten it completely right in PHP 5.

With java web technologies you can greatly reduce this load, by using in memory caches. You can’t really do the same in PHP, although you can use things like memcached to do some of them. So this is only true when you app can’t take advantage of any sort of caching logic.
Lets not make this about J2EE vs PHP. Every one knows … or should know (at least) that J2EE provides a bevy of tools and functionality for enterprise level web application development that PHP can only dream of at this point. (I’m agreeing with you.)

Sure, but the language and the technologies built on it effect how you design the application. With java (and .NET) there are standard web frameworks that have proven to be scalable, there is no such thing in PHP. Although I think Zend is working on some framework…I’d be really suprised if it did well.
Ehhhh … kinda sorta. This one goes back to the fact that PHP wasn’t designed to be an application language. I agree with you about the Framework and how it helps make ASP.NET and J2EE easier to scale. However Java (as a language) isn’t more scalable than PHP. Java already has a framework that helps it scale. I’d like to see what PHP does with its framework in PHP6 before I make a judgement either way. Make no mistake, PHP can scale, but I think I can concede without controversy that Java and .NET frameworks make it easier to do.

Even the lack of good IDEs for PHP effect its scalability. To me at least, PHP is just a templating language trying in the last couple of years to be something else. Its very good to use when you are writing a thin client over some database…but to write any real “business logic” in it seems silly.
I agree up until the bolded part. Frameworks are available in PHP that make it very possible to write business logic in PHP … WACT for example. And every man’s definition of “silly” varies. Also (I hope someone else jumps in on this to offer an alternative viewpoint) but its my belief that PHP IDEs are just as plentiful, if not more common than Java IDEs.

Also (I hope someone else jumps in on this to offer an alternative viewpoint) but its my belief that PHP IDEs are just as plentiful, if not more common than Java IDEs.

They are just as plentiful, but there are no where near in quality. I have used the ones that are suppose to be the best (Zend Studio, eclipse with php plugin etc) and can say I’d almost go “old school” and use emacs. I think PHP being “dymanically typed” has something to do with it…but in many cases more can be done. Where as products for java like Intellij are a pleasure to use. Although I will say I found Zend Studio’s debugger to be decent

However Java (as a language) isn’t more scalable than PHP

Actually, I think this is wrong. The two main issues that come to mind are that PHP lacks namespaces and that PHP is dymanically typed. The first I think most would agree with. With the second you can add comments to variables, methods etc to give hints about what they are suppose to be…but this is a bit hacky and usually developers get lazy with documentation. Also I’m aware of PHP5’s “type hinting” in methods, but this only solves a few of the problems.

Debatable … there’s Komodo, PHPEdit, PHPDesigner (My new favorite), TSW Webcoder 2006 and tons more. I don’t know much about Java IDEs so I can’t speak to that. I do like Netbeans though.

Actually, I think this is wrong. The two main issues that come to mind are that PHP lacks namespaces and that PHP is dymanically typed. The first I think most would agree with. With the second you can add comments to variables, methods etc to give hints about what they are suppose to be…but this is a bit hacky and usually developers get lazy with documentation. Also I’m aware of PHP5’s “type hinting” in methods, but this only solves a few of the problems.
I saw a post that made pointed out similar problems with PHP scalability
Pretty brutal, but good points nonetheless. I really can’t argue on this since my experience with scaling is pretty limited, so I have to concede on those points as well.

Faster when doing what?

If doing number crunching Java’s JIT VM is surely going to give it the advantage.
Grabbing data from external resources, and presenting a web page, I’d doubt there is much difference in similarly architectured applications.

Flickr makes an interesting case study of PHP scaling:

Java’s startup cost means that you’ll usually leave the process around and serve requests through that single process (the typical “application server” model). PHP’s startup cost is extremely low, and the standard setup is to simply toss more instances of apache at the problem. As traffic increases, add more servers. It’s pretty much infinitely scalable.

Fast garbage collection is meaningless when you simply toss away the entire app context at the end of the request (the way that you build PHP apps). PHP’s garbage collection is actually pretty terrible, but it doesn’t matter because you just toss it all out at the end of the request.

JIT compilation does not make a web app faster. It can (theoretically) help improve the speed of a desktop app or server process. In practice, it usually performs worse than statically compiled apps (e.g. C). Web apps startup and tear down every request in the PHP model, so it doesn’t matter.

PHP’s interpreter does not use less memory that a VM for java.
In most instances it does. A typical command-line PHP app uses about 4 MB of RAM. An apache thread running the PHP interpreter will usually eat 6-12MB.

Java as a language runs…much faster than php does.
Prove it.

Maybe in an alternate universe? But certainly not this one.

How to scale PHP:

Install app on more servers.

I work with PHP code every day that deals with hundreds of requests per second. The scalability problems are always on the database end, not on the application end. If you want the app to handle a heavier load, you simply add more hardware 99% of the time. You can’t just throw more hardware at a java app to solve the scalability problem. No, PHP isn’t made out of magic and butterflies, but this is one area that scripting languages really shine.

The idea that PHP’s type hinting in some way makes apps harder or that it’s a problem is just laughable. Dynamically typed languages have many advantages that statically typed ones do not. Data type indicates absolutely nothing about a variable. Use sensible variable names and document your code or it’ll be hard to use. Forcing something as archaic as static typing on web development slows development cycles painfully.

No, PHP is never going to be good as a daemon process. That’s just cold hard truth. PHP excels as a web app language and as a general-purpose scripting language. It can replace perl for everything and everything else for web apps, but it’ll never replace C when it comes to high performance daemon processes. Nobody’s ever going to write a web server in PHP.

EVERY language is “compiled”. The difference is with regards to what it’s compiled to.

Java is compiled to java byte code which the JRE maps to machine instructions to perform tasks.

PHP is compiled to PHP byte code which the PHP runtime maps to machine instructions to perform tasks.

C code is compiled to machine instructions to perform tasks.

C# code is compiled to CLI bytecode which the .net (or mono, or whatever) runtime (CLR) maps to machine instructions to perform tasks.

What affects speed is when that compilation occurs. In PHP, compilation is done at run time (unless you’re using an opcode cache). In other languages, it’s done at build time.

You can’t really do the same in PHP, although you can use things like memcached to do some of them.

Incidentally … http://nanoweb.si.kz/