Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java vs php benchmark [closed]

I'm a php developer, but recently had to write the same application twice, once in php and once in java, for a class I'm taking at school. For curiosity I did a benchmark on the two and found that the java version was 2 to 20 times slower than the php version if the database is accessed, and 1 to 10 times slower without DB access. I see two immediate possibilites:

  1. I suck at java.
  2. I can finally tell people to quit whining about php.

I posted my servlet code here. I don't want any nit-picky whining or minor improvements, but can someone see a horrible glaring performance issue in there? Or can anybody explain why Java feels like it has to suck?

I've always heard people say that java is faster and more scalable than php, especially my teacher, he is convinced of it, but the more requests that are made, the slower java gets. php doesn't seem to be affected by increased loads but remains constant.

like image 585
regality Avatar asked Feb 12 '11 04:02

regality


People also ask

Is PHP faster than Java?

Java is supposed to be faster than any other programming language based on its design and architecture, but PHP runs the quick development race when executed. PHP is faster than Java in web development and offers mainstream and advanced server-side content.

Why do big companies use Java and not PHP?

Reason #1: Cross-Platform Flexibility The fact that Java can run on any server and operating system is one of the reasons why any type of business should consider using it for their projects. Regardless of the type of company and the devices it has, Java can always be an option that fits any technology budget.

Is Java slower than PHP?

Design comparison From a design and architecture perspective, Java is a compiled language and is faster than PHP.


2 Answers

In a mature Java web application the Servlet would make use of an existing JDBC connection pool. Establishing a new connection will be by far the greatest cost you pay in time.

Calling Class.forName for every attempt to get the connection will also cause an unnecessary slow down.

JVM tuning could also be a factor. In an enterprise environment the JVM memory and possibly GC configurations would be adjusted and tuned to achieve a desirable balance between responsiveness and resource utilization.

As Stephen C points out, the JVM also has a concept of a sort of "warm up".

All that said, I have no idea how PHP compares to Java and I feel both languages offer great solutions to separate non-disjoint sets of needs.

like image 142
Tim Bender Avatar answered Sep 20 '22 00:09

Tim Bender


Based on not much info (where the best decisions are made), my guess is the Class.forName("com.mysql.jdbc.Driver"); in getConnection() is the big timesink.

Creating a new String in importFile when the char[] can be passed to out.println is me nitpicking.

like image 38
typo.pl Avatar answered Sep 22 '22 00:09

typo.pl