1 message in com.mysql.lists.javamysql strange performance - java stra...
FromSent OnAttachments
Przemysław Klein30 Jul 2004 02:14 
Subject:mysql strange performance - java strange performance
From:Przemysław Klein (p.kl@ziaja.com)
Date:07/30/2004 02:14:05 AM
List:com.mysql.lists.java

Hi.

The reason of low performance was that I used out-of-date JRE (j2sdk1.4.1_03).
Here come results for: j2sdk1.4.1_03 and j2sdk1.4.2_04. I felt deeply estonished
when I saw these results.

j2sdk1.4.1_03

[start] method2 (0.15s). method1 (10.409s). method2 (0.031s). method1 (9.398s). [end]

j2sdk1.4.2_04

[start] method2 (0.147s). method1 (0.119s). method2 (0.035s). method1 (0.066s). [end]

And the test source code:

-------------------------------------------------------------------------------------------------------------- import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

/* * Created on 2004-07-29 */

/* CREATE TABLE `test` ( `fInt` int(11) default NULL, `fDouble` double default NULL, `fText` text ) TYPE=MyISAM **/

/** * @author prep * * TODO JavaDoc */ public class test {

private static final int TEST_ROWS = 5000;

private static final String FN_INT = "fInt";

private static final String FN_DOUBLE = "fDouble";

private static final String FN_TEXT = "fText";

/** * Retriving data using ResultSet.getDouble(...). * * @param rs * ResultSet. * @throws NumberFormatException * @throws SQLException */ private static void method1(ResultSet rs) throws NumberFormatException,
SQLException { Runtime.getRuntime().gc(); long startTime = System.currentTimeMillis(); while (rs.next()) { rs.getInt(FN_INT); rs.getDouble(FN_DOUBLE); rs.getString(FN_TEXT); } long endTime = System.currentTimeMillis(); System.out.println("method1 (" + ((double) (endTime - startTime) / 1000)
+ "s)."); }

/** * Retriving data using Double.parseDouble(ResultSet.getString(...)). * * @param rs * ResultSet. * @throws NumberFormatException * @throws SQLException */ private static void method2(ResultSet rs) throws NumberFormatException,
SQLException { Runtime.getRuntime().gc(); long startTime = System.currentTimeMillis(); while (rs.next()) { rs.getInt(FN_INT); Double.parseDouble(rs.getString(FN_DOUBLE)); rs.getString(FN_TEXT); } long endTime = System.currentTimeMillis(); System.out.println("method2 (" + ((double) (endTime - startTime) / 1000)
+ "s)."); }

/** * Populating table with given number of rows. * * @param conn * Database connection. * @param rowsToPopulate * @throws SQLException */ private static void populate(Connection conn, int rowsToPopulate) throws
SQLException { long startPopulate = System.currentTimeMillis(); Statement stmt = conn.createStatement(); for (int i = 0; i < rowsToPopulate; i++) stmt.execute("insert into test values (1,0.1,
'abcdefghijklmnopqrstuwvxz')"); long endPopulate = System.currentTimeMillis(); System.out.println("populate " + rowsToPopulate + " rows (" + ((double)
(endPopulate - startPopulate) / 1000) + "s)."); }

/** * Deletes table contents (used when decreasing number of records in test). * * @param conn * Database connection. * @throws SQLException */ private static void delete(Connection conn) throws SQLException { Statement stmt = conn.createStatement(); stmt.execute("delete from test"); long endPopulate = System.currentTimeMillis(); }

public static void main(String[] args) throws NumberFormatException,
SQLException, ClassNotFoundException {

System.out.println("[start]"); // connecting Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager .getConnection("jdbc:mysql://localhost/test/?user=root&password=&useUnicode=true&characterEncoding=iso-8859-2");

// setting proper number of rows acording to TEST_ROWS (insert/delete if // needed) Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select count(fInt) from test");

rs.next(); int numRows = rs.getInt(1); if (numRows > TEST_ROWS) { delete(conn); numRows = 0; } if (numRows < TEST_ROWS) populate(conn, TEST_ROWS - numRows);

stmt = conn.createStatement();

// running tests rs = stmt.executeQuery("select * from test"); method2(rs); rs = stmt.executeQuery("select * from test"); method1(rs); rs = stmt.executeQuery("select * from test"); method2(rs); rs = stmt.executeQuery("select * from test"); method1(rs);

System.out.println("[end]"); } }

--------------------------------------------------------------------------------------------------------------

Thanks, Przemek