views:

147

answers:

2

I want to know how Java (JSP) on Tomcat compares to PHP on Apache in terms of performance.

Two servers with the same hardware configurations, one running Tomcat/Java (JSP) the other Apache/PHP, both servers maxed out with how many connections they can handle at once. Would they be somewhat close or would one pull away from the other one by a large margin? I basically just want to know if Tomcat/Java (JSP) is going to be a big performance hit if I switch to it vs PHP. If anyone can give a detailed answer on why one is faster than the other that would be amazing. Links are great too, I was unable to find anything online surprisingly.

Please no Java vs PHP wars, this is about performance only, nothing to do with the languages themselves.

Note: If there is any other concerns I should have for switching to Java from PHP please let me know. I REALLY hate asking this question because I'm usually the first person to say "program in what you like" but in my situation I need whats also good for the projects I work for. I know that there are large sites written in JSP, but it doesn't mean that they're better.

Thanks

+5  A: 

What's good for the projects you're working on is to spend as little time as possible to write them as developer time is way more expensive than any perceived differences in performance. So stick with what you're familiar with.

The answer to your question is: they are both fast enough.

Any such comparison is hard because you end up doing things differently in different languages. Java bytecode is probably faster to interpret but then again any decent PHP install uses as opcode cache largely negating any such advantage in real terms.

Java also has a more complicated development model because Web processes are persistent. This can have a performance advantage but also can create problems like memory and other resource leakage, which PHP doesn't tend to have because everything is created and destroyed on each request (barring session information, memcache and so on).

Also PHP extensions can be created for any parts that you want to speed up.

$10,000 can buy an awful lot of hardware. It can buy the hardware to run SO. It doesn't buy much developer time.

I've got experience doing both Java and PHP development. I will generally choose PHP for Web development because of:

  • quicker to test changes in development (ie no build/deploy steps and Java hot-deploy has serious limitations). Words cannot express how freeing it is to test changes by saving the file you're working on and clicking reload on a browser vs running an Ant/Maven build process;
  • far fewer issues of memory/resource leakage;
  • extensive library of functions to do pretty much anything you want;
  • cheaper to host (at the low end).

I will use Java for some things, like anything that involves a lot of background processing and threading, which aren't PHP's strong points.

You'll note that performance (or the lack thereof) doesn't even rate as a reason for or again.

Sorry if that doesn't answer your question, but such concerns over performance are a pointless distraction.

cletus
I 100% agree with you. I've been programming in Java for along time now though. It's not that I need to learn a new language. Java is pure OO, it's not loosely typed, the Java community has high standards, etc. I just feel that I work so hard making PHP into something that Java already is if that makes sense.
William
It looks like you edited your post now that I commented. You have a lot of good points. I also use Java for background processing and threading. Thats the main reason I started writing Java and I started liking it so much I wanted to use it all the time. I really wish PHP wasn't loosely typed and I wish it was a pure OO language. It looks like I might be sticking with PHP after all.
William
@William: I actually **like** the fact that PHP is loosely typed. It's part of the appeal. You just have to write so much boilerplate in Java (like ob.setXXX(otehr.getXXX()) translating objects between layers). As for PHP and OO IMHO you shouldn't try and make PHP something it's not. It's not a pure OO language (far from it) but that's not actually a bad thing.
cletus
I know it's not, and that is why I was hoping to move to something like Java. I feel weird using so much OOP in a language that wasn't originally built for it, but it's not something I plan on changing. I agree there are times (ESPECIALLY in C) where strict types is annoying. I guess I wouldn't mind it so bad if you could set in a function that type is REQUIRED (I know it works for arrays and objects). Thats my biggest issue.
William
If it's that important, the functions themselves can check the types.
cletus
The fact I have to actually run a check in my function for X methods when I can go to another langauge that does it for me is annoying. My ENTIRE point in this whole thing is I have to do extra things in PHP that Java would do for me. THAT is why I wanted to switch, because I don't want to try to make PHP something it's not. I like a more "strict" language, Java offers that. But you made OTHER good points in why I'll just have to deal with PHP.
William
@William: my point is that you **don't** really have to do these things. They're not as necessary as you think they are. Don't try and turn PHP into Java. If you're not comfortable with <insert "not-Java" here> then perhaps you should be using Java. Contrary to popular belief, the world doesn't end with loose typing.
cletus
+1  A: 

The best way to answer performance questions is with a benchmark. Implement some simple page in both PHP and Java and then benchmark them using ab (Apache Benchmark).

Having said that, I suspect Java will outperform PHP because of the nature of the 2 platforms. Java is compiled to optimized bytecode (once) and then interpreted by a virtual machine. When Tomcat runs, the JVM loads the classes required for any given page and keeps them in memory so they're ready to go when an HTTP request hits the web server. Contrast that with PHP which reloads and re-interprets the code from scratch with each invocation by Apache. This is helped to a large degree by op-code caching, but still not to the level of what happens in the JVM.

Asaph
+1 Thanks for answering the question. cletus did beat you to it and also mentioned a few things that were helpful in making my decision.
William
@William: Thanks. @cletus did provide a great answer as he always does. BTW: Your comment says "+1" but my answer isn't upvoted :( I'm just sayin'...
Asaph
Fixed, I don't get why it wasn't up voted. Sorry about that. :)
William
@William: Thanks :)
Asaph