简体   繁体   English

Java作为与MySQL交互的cron脚本与使用PHP

[英]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. 我目前有一些使用Cron读取和更新MySQL数据库的Java程序。

I am considering porting the code to PHP. 我正在考虑将代码移植到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. 在我这样做之前,我做了一个简单的基准测试, SELECT某个表中的所有行,然后将值存储在字符串中。

I loop this 10,000 times for both the PHP and Java programs. 我为PHP和Java程序循环了10,000次。 PHP ran it in under 5 seconds. PHP在5秒内运行它。 Java took around 1 minute. Java花了大约1分钟。

I was astonished by the difference in performance. 我对性能的差异感到惊讶。 Is this about right? 这是对的吗? Is Java really this slow? Java真的很慢吗? 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. 我目前正在使用JDK 6和PHP CLI 5.3在CentOS 5.5中运行cron脚本。

Here is the code in Java: 这是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: 然后在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 在这种情况下,您可以使用StringBuffer来提高java的字符串性能

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?). 我确信它在很大程度上取决于你为每种语言编写的代码(与PHP相比,Java代码看起来是什么样的?)。 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. 在Java版本中,您使用String,这意味着您每次都会在循环中创建一个新副本。 Try to use StringBuffer and append the result instead, only converting it to String when done. 尝试使用StringBuffer并附加结果,只在完成时将其转换为String。

Have a look at http://www.velocityreviews.com/forums/t146183-why-string-is-immutable.html for a discussion on why String behaves like that. 请查看http://www.velocityreviews.com/forums/t146183-why-string-is-immutable.html ,了解String为何如此行为。

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). 这不是真正的数据库应用程序可能会做的事情,而且你不是在Java中以最有效的方式做这件事(你在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. 我建议你用PHP编写它,因为从代码中,你对Java的掌握显然较弱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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