简体   繁体   中英

Java as a cron script to interact with MySQL vs using PHP

I currently have a few Java programs that read and update the MySQL database using Cron.

I am considering porting the code to PHP. Before I do this, I did a simple benchmark test of SELECT ing all rows in a certain table and then storing the values inside a string.

I loop this 10,000 times for both the PHP and Java programs. PHP ran it in under 5 seconds. Java took around 1 minute.

I was astonished by the difference in performance. Is this about right? Is Java really this slow? Or am I doing something wrong?

I'm currently running the cron scripts in 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;
}

you can improve java's string performance in this case by using StringBuffer

private String getUserProfiles() 
{
    String query = "SELECT * FROM UserProfile;";
    StringBuffer output = new StringBuffer();
    try
    {
        for(int i =0; i < 10000; ++i)
        {
            ResultSet rs=statement.executeQuery(query);
            while(rs.next())
                output.append(rs.getString("name"));
        }
    } 
    catch (Exception e) 
    {
      e.printStackTrace();
    }
    return output.toString();
}

I'm sure it's going to depend largely on the code you wrote for each language (what does the Java code look like compared to PHP?). Such a large discrepancy hints that something is different. Perhaps the way you're establishing or maintaining database connections?

The difference is likely in how you handle the results. In the Java version, you use String, which means you will make a new copy each time in the loop. Try to use StringBuffer and append the result instead, only converting it to String when done.

Have a look at http://www.velocityreviews.com/forums/t146183-why-string-is-immutable.html for a discussion on why String behaves like that.

It looks like what you're really comparing is a method of compiling very large strings by concatenation; this is not something that a real database application is likely to do, plus you're not doing it in the most efficient way in Java (you are doing it in a terrible way in Java).

I feel sure that you should not base your choice of language on this artificial and badly implemented benchmark.

Consider which language has the easier code for you. I'd suggest you write it in PHP, as from the code, your grasp of Java is clearly weaker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM