A little test Java v PHP

I was curious as to the relative speeds on Java versus PHP. So I created a couple of simple applications (see below).

PHP took approximately 2.5 seconds to execute the core part of executing a loop a million times.

Java took about 0.045 seconds to do the same thing, over 50 times faster.

I was curious as to the relative speeds on Java versus PHP. So I created a couple of simple applications (see below).

PHP took approximately 2.5 seconds to execute the core part of executing a loop a million times.

Java took about 0.045 seconds to do the same thing, over 50 times faster.

I know its a very crude measure, but it did suprise me how much better java is at executing than php. Of course the overhead of getting the results to and from tomcat sitting behind apache, compared to running php within the apache process might be pretty significant, but it does encourage me not to worry so much about whether I did the right thing building my application in Java.

For PHP


<html>
    <head>
        <title>PHP speed test</title>
   </head>
   <body>
        <p> This is a test to see how good php is in executing a loop a million times</p>
        <p>Times are: 
<?php  

/* Copyright (c) 2005 Alan Chandler, licenced under the GPL */

define (LOOPS,1000);
$x=3.0;
$start=microtime();
for ($i=0;$i&lt;LOOPS;$i++) {	
    for ($j=0;$j&lt;LOOPS;$j++) {
        $y=$x*3.0;
        $x=$y/3.0;
    }
}
$end=microtime();

list($su,$ss) = explode(" ",$start);

list($eu,$es) = explode(" ",$end);

$start= date('H:i:s',(float)$ss).":".(int)((float)$su*1000);
$end =  date('H:i:s',(float)$es).":".(int)((float)$eu*1000);

echo $start." ".$end;

?>     </p>

    </body>
</html>

For Java, the tapestry page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Java Speed Test</title>
</head>
<body>
<!-- Copyright (c) 2005 Alan Chandler, licenced under the GPL -->
<p> This is a test to see how good java is in executing a loop a million times</p>
<p>Times are: <span jwcid="@Insert" value="ognl:times">hh:mm:ss:mmm to hh:mm:ss:mmm</span></p>
</body>
</html>

and the Java

package uk.org.chandlerfamily.tapestry.speed;

/* Copyright (c) 2005 Alan Chandler, licenced under the GPL */

import org.apache.tapestry.html.BasePage;

import java.util.Date;import java.text.SimpleDateFormat;

public abstract class Home extends BasePage {	
    private static final int LOOPS=1000;	
    public String getTimes() {
        int i;	
        int j;
        float x=3;
        float y=7;
    
        Date start = new Date();
        for (i=0; i&lt;LOOPS;i++) {
            for (j=0;j&lt;LOOPS;j++) {
                y=x*3;
                x=y/3; // Waste time
            }
        }

        Date end = new Date();
        SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss:SSS");
        return fmt.format(start) +" to " + fmt.format(end);	
    }

}

Author: Alan

I am Alan Chandler.

Leave a Reply

Your email address will not be published. Required fields are marked *