views:

285

answers:

6

Which language is faster for web, Java or PHP?

+7  A: 

Speed doesn't matter

in most cases.

Processing is cheap. Code in what you're comfortable with. Writing proper code goes much further for speed then choosing a language. Solid coding conventions and design plan will also help more.

Josh K
Furthermore, it'd be easy to write fast or slow applications in either language by choosing good or poor datastructures and algorithms.
sarnold
@sarnold: Correct. Another thing to consider (though technically outside the scope) is development time. If you're not a Java developer you aren't going to like the J2EE development cycle of repackage, redeploy, test.
Josh K
Can you generate actual web pages with java? Because otherwise applet/VM initialisation time kills any site for me.
Graphain
To me, answers like this are as vague and unhelpful as questions like these. In some situations, speed really does matter. Inside loops, batch processing, etc. We have no idea what the original poster wanted, so why not trust that it really was something for which speed mattered? The reality of the situation is that we have no idea what the context of this question is: what this person is trying to achieve - it's far too vague - and it's for that reason I nominated closing it.
thomasrutter
+1  A: 

Best answer I could find

"stuff to consider:

  1. Java web applications are compiled to bytecode. Even JSPs, which are compiled at runtime. This is an advantage over most uses of PHP, where the Zend Optimizer is not in use.

  2. Data can be cached in a live servlet instance - no direct/easy way of doing this in PHP to my knowledge (there is only ever a single instance of a servlet/JSP in memory)

    • If anybody knows how to cache data in PHP without resorting to ugly hacks, please enlighten me!
  3. Java applications tend to be n-tiered, which generally results in a more maintainable application at a slight performance penalty. This probably sounds trollish, but honestly: even within Java itself direct use of JDBC will always be faster than going through three layers of objects to the database.

But is an n-tiered Java application able to hit the database sooner than an uncompiled, hacked-up monolithic PHP script? I don't think there's an answer to that question.

All that said, I'm working on an n-tiered MVC framework for php 5 (it's called Pure (http://www.sf.net/projects/php-pure)), so my PHP applications are generally n-tiered too. I'll worry about speed when and if it becomes an issue. For now, it's definitely not an issue."

courtesy of krumms

Ryan
+1  A: 

Can't answer this question with one or the other unless you define what you want to measure the speed of.

Some things are much faster in PHP (in a native function for example), other things are much faster in Java.

thomasrutter
A: 

The intent of each language is substantially different from the other, so if you're debating over which to use for a particular task, you should generally based the decision on that task (and how well suited each language is to it) rather than performance.

For raw performance of code written in the language (as opposed to simply calling code in the standard library), Java will probably run faster than PHP as an extremely general rule. If that matters, chances are that PHP just isn't very well suited to the task at hand.

Jerry Coffin
A: 

Asides speed, I believe the performance of Java is bettern than PHP, but developing a PHP project is faster

Truong Ha
If you're a shop full of Java developers, it's faster to develop a project in Java. How fast it is to develop in just depends on what your specialty, I'd say.
thomasrutter
and what if you don't java neither php?
ultrajohn
A: 

Difficult one to answer as in theory Java should be faster: its precompiled, any trivial algorithim will run faster in java than php and there has been a vast amount of work done to optimise java from improving the code is standard libraries, to JIT compilers etc.etc.

PHP loaded and interpreted every time if your not using the Zend optimiser, objects intialised on every execution, even the most trivial string varaible is actually a complex object with many methods to support.

The problem is that in practice PHP sites seem to run faster using fewer resources.

I think this is because php developers take a more straightforward approach to design and dont get lost trying to implement exotic design patterns and implementing endless pointless abstractions.

James Anderson