I currently have several Java programs that read and update a MySQL database using Cron.
I am considering porting code to PHP. Before I do this, I checked a simple SELECT test of all the rows in a specific table and then saved the values ββinside the row.
I repeat this 10,000 times for PHP and Java programs. PHP launched it in less than 5 seconds. Java took about 1 minute.
I was struck by the difference in performance. It's right? Is Java really this slow? Or am I doing something wrong?
I am currently running cron scripts on CentOS 5.5 with JDK 6 and PHP CLI 5.3.
Here is the code in Java:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class Test { private Connection connection = null; private Statement statement = null; public static void main(String args[]) { (new Test()).run(); } private void initDB() { try { String url="jdbc:mysql://localhost:3306/db"; Class.forName( "org.gjt.mm.mysql.Driver" ); connection = DriverManager.getConnection(url, "username", "password"); statement = connection.createStatement(); } catch (Exception e) { e.printStackTrace(); } } private String getUserProfiles() { String query = "SELECT * FROM UserProfile;"; String output = ""; try { for(int i = 0; i < 10000; ++i) { ResultSet rs=statement.executeQuery(query); while(rs.next()) output += rs.getString("name"); } } catch (Exception e) { e.printStackTrace(); } return output; }
/ more code continues /
And then in PHP:
try { $db = new PDO("mysql:host=localhost;dbname=db;charset=utf8", 'username', 'password'); $str = ""; for($i=0; $i < 10000; ++$i) { $qry = $db->prepare('SELECT * FROM UserProfile;'); $qry->execute(); $result = $qry->fetchAll(PDO::FETCH_OBJ); foreach($result as $profile) { $str .= $profile->name; } } } catch(PDOException $e) { echo $e->getMessage(); exit; }
source share