Java as a cron script for interacting with MySQL and using PHP

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; } 
+4
source share
4 answers

you can improve java string performance in this case 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(); } 
+1
source

I am sure that this will largely depend on the code you wrote for each language (what does Java code look like in comparison with PHP?). Such a large discrepancy suggests something else. Perhaps how do you establish or maintain database connections?

0
source

There is probably a difference in how you process the results. In the Java version, you use String, which means that you will create a new copy every time in a loop. Try using a StringBuffer and add the result instead only when it is converted to String.

See http://www.velocityreviews.com/forums/t146183-why-string-is-immutable.html for a discussion of why String behaves this way.

0
source

It looks like you are really comparing this method of compiling very large strings by concatenating; this is not what a real database application can do, and you are not doing it in the most efficient way in Java (you are doing it terribly in Java).

I am sure that you should not base your choice on this artificial and poorly implemented control.

Consider which language has simpler code for you. I would suggest you write it in PHP, as from the code your understanding of Java is clearly weaker.

0
source

Source: https://habr.com/ru/post/1346335/


All Articles