Could someone tell me why Java is faster than PHP

I don’t know anough about the low-level stuff to really argue for or against php vs. java, and I think it’s subordinate anyway. I’m quite sure that both can be scaled in regards to performance.
More importantly, it’s my belief that the software framework which the two platforms present to the programmer make a difference. PHP’s framework is easily accessible, but the consequences are often unmaintainable apps when they grow in size. I know that you can easily make spaghetti code in java, but it’s easier to avoid it.

I agree with you mostly but I want to gain a better understanding of some of your points.

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.
Can you prove this.

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.

I’ve done some reading on this, and from what I understand JIT would help Java perform better than PHP because PHP is interpreted (after being compiled to Zend opcode).

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.

read this and let me know what you think

> I know that you can easily make spaghetti code in java, but it’s easier to avoid it.

Well, I wouldn’t go as far as to say that, in regards that it’s easier to avoid in Java. At the heart of it, it’s basically down to the individual developer(s) how they develop, but looking deeper into what you say, maybe it’s the practice of separation that’s more foundamentally ingrained into the development community?

I have the adverse impression that it’s that foundamental practice of separation that is lacking some what with the PHP community…

> Web apps startup and tear down every request in the PHP model, so it doesn’t matter.

I would agree with that, and that Just In Time is pointless when you consider the environment of which PHP works under. As for Garbage Collection, that may well be a concern not so much based on the size of the application as such, but more so as the number of users using that application, increases.

I’m not sure myself how PHPs Garbage Collection works, or even if it works at all :eek:

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.

You can easily run a small command-line app with less than 4MB of RAM in java. Java doesn’t use a lot of memory…it does take awhile to start up. But if you have any evidence that PHP uses less memory than Java reference it.

JIT compilation does not make a web app faster. It can (theoretically) help improve the speed of a desktop app or server process.

What the hell are you even talking about? In java any code that becomes “hot” will be compiled to native code. Whether that code is running a web application or desktop app simply doesn’t matter.

Prove it.

google.com - Anybody that denies that Java as a language is faster than PHP is a mindless fan-boy of PHP. …hell try writing any NON-TRIVIAL algorithm in PHP and Java and see which which does better. PHP can solve some of its performance problems by outsourcing its work to C via an extension though. PHP is just not in the same class as Java, compare it to Perl, Python etc.

Dynamically typed languages have many advantages that statically typed ones do not. Data type indicates absolutely nothing about a variable.

Sure they have advantages…one of the advantages NOT being scalability. And…hmm…call me crazy but I’d imagine that a variables Data type would indicate something namely its data-type =).

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.

Adding typing information to variable names makes things really ugly. Furthermore if you have to add typing information to the variable names and/or document how does this imply less work? You do the same work the difference is languages with static typing can use this information at compile time. Furthmore good IDEs in Java do all the typing work for you. Have you actually worked on a proejct with more than 50 files to manage?

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.

Have you actually worked with java web applications AT ALL? Please tell me why you can’t “add more hardware” to java applications? EVEN if you wrote an application that is not distributable you can easily cluster tomcat (or any other servlet container) using sticky sessions. This is almost trivial to set up. Need more power? Add another tomcat instance. If your application was written to be distributable then its…a no-brainer…and ahem if you are working on the sort of app that can’t run on just one server…you’d do that. But more importantly…the part of scalability that is being talked about in this post is that of the code. PHP’s code is not scalable. Large web applications have thousands and thousands of source files being worked on by many people…there is no sensible way to manage this with PHP.

But what exactly non-trivial algorithms do you need in a web enviroment ?
99.99% is get data from here, apply some fairly trivial rules, and put it there.
We aren’t solving the travelling salesman problem, or anything close to being non-trivial imo.

Adding typing information to variable names makes things really ugly.
Furthermore if you have to add typing information to the variable names and/or document how does this imply less work?

This seems to be rant against Hungarian notation, and nothing todo with any language.

Have you actually worked with java web applications AT ALL? Please tell me why you can’t “add more hardware” to java applications? EVEN if you wrote an application that is not distributable you can easily cluster tomcat (or any other servlet container) using sticky sessions. This is almost trivial to set up. Need more power? Add another tomcat instance. If your application was written to be distributable then its…a no-brainer…and ahem if you are working on the sort of app that can’t run on just one server…you’d do that. But more importantly…the part of scalability that is being talked about in this post is that of the code. PHP’s code is not scalable.

I’ve been involved with companies that run high end sun fire servers, using off the shelf software (from BEA) and they had huge scalability problems. The GC basically was broke, and resulted in running out of memory. The solution was to double the amount of memory per server so they didnt have to power cycle as often.

Large web applications have thousands and thousands of source files being worked on by many people…there is no sensible way to manage this with PHP.

I don’t see how its any different from managing thousands of java files.

And vice versa…

Data type indicates absolutely nothing about a variable.
Nothing? Of course there are many intelligent people on both sides of the static vs. dynamic typing issue, but usually the argument made by the pro-dynamic typing people is that static typing does bring benefits, but it also brings costs and they feel the costs outweight the benefits. But I don’t usually see too many people who say that data types provide no information at all, a statement that seems a bit hyperbolic to me.

I’ll agree that in simple web apps it probably doesn’t make much of a difference whether you’re dealing with an integer 1, a string “1”, or a double 1.000. Primitive types can be fairly easily coverted to other primitive types without losing too much information. However, in object-oriented code I’ve found data type information to be highly useful. Consider two random classes that you use in your PHP code. It’s not very likely that an instance of one of your classes could be converted to an instance of another… the class of a particular variable is very important, and indicates what it is and what can be done with it (what methods can be called, etc.). It seems odd to say that this information doesn’t matter.

Use sensible variable names and document your code or it’ll be hard to use.
For me, one of the advantages (and yes, I see disadvantages too) of static typing in an OO language is documentation. It’s immensely useful to know without a doubt what type of objects are required as parameters on a method, and what type of object I’m going to be getting back. I’ve found in my and other’s dynamically typed OO code, there is sometimes a tendency to give object instances longer and more cumbersome names in an effort to make it clear what class the instance belongs to. This is, in a way, a type of Hungarian notation for types, and I think most people here would agree that this is a Bad Thing. Sure, you don’t have to do this; you can pick the same names as you would in a statically typed language, but it sometimes makes it a little trickier to know what you’re dealing with, especially if you have classes that are different but may contain a common subset of similar or the same method names.

Forcing something as archaic as static typing on web development slows development cycles painfully.
I think it depends on what style works for you. A counter argument to this is that it may slow initial development time, but it may improve maintenance time. You only write your app once, but you may be refactoring/maintaining it for a long time to come. The argument could be made that you should optimize for maintenance and not for the initial development time.

As for me, I see advantages and disadvantages to both. For simple tasks, static typing can be onerous. For more complex ones, I feel it improves the ability to reflect on what the code is doing and it may improve maintenance. So, speaking for me personally, I don’t necessarily prefer one over the other; they both very much have their uses.

PHP excels as a web app language and as a general-purpose scripting language.
You are being slightly inconsistent here. You say that PHP excels as a general-purpose scripting language. But earlier in your post you stated:

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.
I don’t see how a platform that has “pretty terrible” garbage collection can possibly excel as a general-purpose scripting language. “General-purpose” means that you may not be dealing with short-lived requests and environments that are being tossed out rapidly. I would argue that PHP does excel as a web app language but still has some improvements to be make before it can truly be “general-purpose”.

It can replace perl for everything
:eek: I beg to differ. Perl most definitely has its warts, but it’s still very useful for many tasks. PHP does not currently, and likely never will, have anything comparable to CPAN. You may find Perl the language ugly (although in many ways it is much more advanced than PHP: true block-level scoping, namespaces, closures and truly-anonymous code blocks, etc.), but there is an amazing selection of truly-useful reusable code available at CPAN for just about any task you can imagine. I’ve found that the same cannot be said for PHP. My personal opinion is that the lack of namespaces in PHP makes encapsulation at a library-level difficult and discourages the development and sharing of generic classes and class libraries.

Disclaimer: I’ve been pretty down on PHP in this post. Before anyone gets the idea that I’m a “hater” :wink: let me state that I do think PHP has a sweet spot in certain types of applications, and I use it heavily where it makes sense. It just feel that this attitude of “PHP has nothing to learn from other platforms” holds it back, and that’s a shame…

[OT]

EVERY language is “compiled”.
Actually, I’ve seen it stated that Ruby scripts are currently not compiled to bytecode/machine language, but are converted into an abstract syntax tree and interpreted. So, it could be argued that Ruby isn’t compiled.[/OT]

PHP uses a simple, naive reference counting algorithm for garbage collection. The link discusses the pros and cons of that.

But what exactly non-trivial algorithms do you need in a web enviroment ?

If you are using PHP for just a thin web client then it works great, as everything is as you say. But many web applications need to do non-trivial work, for example any sort of AI in the application (and yes many web apps use such) or even things like caching algorithms.

This seems to be rant against Hungarian notation, and nothing todo with any language.

The point of the comment is not a rant against Hungarian notation, rather to say that you have to do the same amount of work when you use hungarian notation vs using a OO language with static typing. That is to say the argument that languages with static typing take more “work” is silly.

I’ve been involved with companies that run high end sun fire servers, using off the shelf software (from BEA) and they had huge scalability problems. The GC basically was broke, and resulted in running out of memory. The solution was to double the amount of memory per server so they didnt have to power cycle as often.

So basically you threw hardware at the problem. But really if it was eating up memory like that, that is a good indication that there is a memory leak in the application…did see what was eating up all the memory? Were any efforts made to find memory leaks in the application? Usually companies don’t bother with this stuff…as memory is so cheap. But don’t blame java for a company that doesn’t want to find memory bugs in their application.

I don’t see how its any different from managing thousands of java files.

Then you aren’t thinking hard… THere are many reasons most notably that PHP lacks namespaces.

Even without Namespaces though, PHP still has credible functionality in regards to files, particularly with PHP5. Lets not forget that PHP has far greater grace than Java when strings and arrays are involved, particularly, this benifit comes from the core root where PHP comes from.

Both technologies have their advantages and disadvantages, so use the one which is best suited to the task… Why argue about what amounts to be, trivial technicalities?

Even without Namespaces though, PHP still has credible functionality in regards to files, particularly with PHP5.

I’m not sure what you are talking about here, explain?

Lets not forget that PHP has far greater grace than Java when strings and arrays are involved, particularly, this benifit comes from the core root where PHP comes from.

This is a mere aesthetic matter…it may be your view which is fine, but don’t pretend as if its universal. I certainly don’t agree with it.

PHP’s arrays are not universally loved. The way that PHP conflates arrays and hashes is frequently cited as a problem and can sometimes violate the principle of least surprise. Some may see it as an advantage, but it brings its problems as well. For example, when dealing with arrays that may come from unknown sources (say, a POST or GET request), the following code isn’t safe:


<?
if (count($someArray) == 1) {
  doSomethingWith($someArray[0]);
}
?>

You cannot know for sure that just because the array only has one element that you can refer to it with the 0 offset. It could be anything. Sometimes you WANT an array to be an array, and not a hash, but you don’t get to make that choice in PHP. Sure, you can sanitize the array’s upon input and rebuild them to reorder the keys, but then you have to worry about which you want to treat that way and which you don’t. Sometimes it would just be easier to be able to be explicit, I think.

Both technologies have their advantages and disadvantages, so use the one which is best suited to the task… Why argue about what amounts to be, trivial technicalities?
:tup: I agree with this.

For those who say Java uses a lot of memory … think about mobile applications.

JAVA beats the living crap out of PHP, when it comes to performance, for the following reasons:

  1. The Hotspot JVM knows about “adaptive optimizations” … something that even the .NET CLR doesn’t have yet

  2. PHP, being so crapy, is recompiled at every request, so that Zend can make a living

  3. The cost of a new process is higher than the cost of a new thread

  4. The JAVA language can be optimized very efficiently because it is a static language

  5. A LAMP setup doesn’t have stuff like connection-pooling by default

For those who say that PHP is more scalable that JAVA … PHP is scalable because of its mediocrity … and tell me … what infrastructure does PHP provide for distributed applications ?

Anyone that says otherwise is clearly wrong, ignorant or religios about it.

This thread is useless as it clearly leads to a flamewar … because of fan-boys that can’t accept the limitations of a scripting language … and thus they bash a technology they do not use or know.
If you want productivity, then PHP may be the answer for you, but if you want performance … with PHP it is like looking against the SUN :wink:

BTW, namespaces, strings, arrays, or all other syntax advantages or dissadvantages of PHP have nothing to do with its ability to scale.

In fact, PHP cannot scale too well precisely because of its shared-nothing architecture, and because the platform is crippled because of ZEND trying to make an honest buck.

Actually, have you worked with the collections framework in JAVA ?

Compared to the collections in JAVA, or with the Array in Ruby … PHP’s structures are hidious.

Something i hate more and more about PHP: Virtually every webhost is using a different php.ini and they all have different extensions in use… Some use mod_safe, most don’t. Some have enable short tags, most don’t… With Java you know what you can expect if you know which version your webcontainer supports…

Still 99.99% is trivial as I said originally. Complex things like data mining don’t get performed in a web enviroment.

The point of the comment is not a rant against Hungarian notation, rather to say that you have to do the same amount of work when you use hungarian notation vs using a OO language with static typing. That is to say the argument that languages with static typing take more “work” is silly.

So basically you threw hardware at the problem. But really if it was eating up memory like that, that is a good indication that there is a memory leak in the application…did see what was eating up all the memory? Were any efforts made to find memory leaks in the application? Usually companies don’t bother with this stuff…as memory is so cheap. But don’t blame java for a company that doesn’t want to find memory bugs in their application.

The software vendor was involved in the solving the issue. The issue was the Sun Java VM GC, not the software.

Then you aren’t thinking hard… THere are many reasons most notably that PHP lacks namespaces.

Namespaces are not a problem. PHP can have namespaces, as several patches have demonstrated.

BTW, namespaces, strings, arrays, or all other syntax advantages or dissadvantages of PHP have nothing to do with its ability to scale.

Why wouldn’t they? Part of what makes a application scalable is its ability to deal with large development teams, large code bases etc. Which language used clearly effects this… But often when people say “scaling” they are referring to performance issues etc in this context yeah.

  1. PHP, being so crapy, is recompiled at every request, so that Zend can make a living
  2. The cost of a new process is higher than the cost of a new thread
  3. A LAMP setup doesn’t have stuff like connection-pooling by default

These points are not very applicable… #2, there are many free opcode caches for PHP, they are trivial to install. #3 This is an apache issue…PHP doesn’t create threads or processes, that work is left to apache and can be done either way with apache 2.0+.
#5 Depends what you take the P to mean…connection pooling can easily be done with Perl.

Still 99.99% is trivial as I said originally. Complex things like data mining don’t get performed in a web enviroment.

Your point is just myopic. Please look at what it takes to implement a regular expression engine (and regular expressions are used ALLLL over the place in web apps). When you start writing non-trivial web applications you’ll find that there are no longer PHP extensions to do all the hard work for you.

Namespaces are not a problem. PHP can have namespaces, as several patches have demonstrated.

Namespaces are a language feature…PHP lacks them. Please explain which “patches” you are referring to.

I know what it takes to implement regular expressions. From simple Henry Spencer style recursive descent, to the more complex NFA/DFA systems for grep. Perhaps you should look at my post history, posts like implementing LALR(1) parsers in PHP.

Namespaces are a language feature…PHP lacks them. Please explain which “patches” you are referring to.

Few of them are at http://www.phpnamespaces.org/wiki/

I know what it takes to implement regular expressions. From simple Henry Spencer style recursive descent, to the more complex NFA/DFA systems for grep. Perhaps you should look at my post history, posts like implementing LALR(1) parsers in PHP.

Because I don’t care about your post history. So great you know what it takes…and you know they are used in web applications…but web applications only use non-trivial algorithms. RIGHT! Also writing a parser in PHP seems so silly…due to its performance issues…unless of course you needed such for a web application…but that couldn’t be so right?

Few of them are at http://www.phpnamespaces.org/wiki/

Well the site clearly states why php should have namespaces…but they don’t offer a solution they offer ideas on how a solution would look like. I mean they clearly state what they are doing is for demonstration. Namespaces are the sorts of things that need to be added to the language itself.

i really dont know much of programming stuff, just write simple PHP websites for my down to earth clients. Trying to learn Java so that I can get a job.
Now I read almost all replies. I cannot add much to this thread as I dont know!

But just one question … is Java and PHP comparable?
similerly is it fare to compare Zend Vs. SUN?

I am not sure this post can add any new thing to this increadebily informative(though with a sense of fire) thread. Moderator please delete this post if needed.