【问题标题】:how to minimize the execution time for a java program? [closed]如何最小化java程序的执行时间? [关闭]
【发布时间】:2017-09-05 06:26:56
【问题描述】:

我在 java 中创建了一个程序,用于将 1000 个随机数输入两个不同的数据库,一个用于奇数,一个用于偶数。代码执行良好,但执行需要将近一分钟。如何最大限度地减少执行时间?

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;

public class Test1 extends Thread {

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    long start = System.currentTimeMillis();

    int evencount = 0;
    int oddcount = 0;
    int breakcon = 0;
    int breakcon1 = 0;

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1" + "?useSSL=false", "root",
            "1234");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2" + "?useSSL=false", "root",
            "1234");

    try {

        Class.forName("com.mysql.jdbc.Driver");

        for (int n = 1; n <= 1000; n++) {
            Random r = new Random();
            int val = r.nextInt(100000);

            if (val % 2 == 0) {
                PreparedStatement ps = con.prepareStatement(" insert into try values (?)");
                ps.setInt(1, val);
                ps.executeUpdate();
                ps.addBatch();
                breakcon = breakcon + 1;
                if (breakcon % 500 == 0 || breakcon == val)
                    ps.executeBatch();
                evencount++;

            } else {
                try {

                    Class.forName("com.mysql.jdbc.Driver");
                    PreparedStatement ps3 = conn.prepareStatement(" insert into try1 values (?)");
                    ps3.setInt(1, val);
                    ps3.executeUpdate();
                    ps3.addBatch();
                    breakcon1 = breakcon1 + 1;
                    if (breakcon1 % 500 == 0 || breakcon1 == val)
                        ps3.executeBatch();

                    oddcount++;

                }

                catch (Exception e2) {
                    System.out.println(e2);
                }
            }
        }

    }

    catch (Exception e) {
        System.out.println(e);
    }

    long end = System.currentTimeMillis();
    NumberFormat formatter = new DecimalFormat("#0.00000");
    System.out.println("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
    System.out.println(oddcount + evencount);

}
}

【问题讨论】:

  • 我会看看 SQL 批处理。
  • @JoeC 我该怎么做?请解释
  • 这类问题最好在Code Review 提问。因为它是关于改进运行代码
  • @Jens 宁愿完全删除 Class.forName()...这是过去的真正爆炸。

标签: java execution-time


【解决方案1】:

不要在每次setInt 之后调用ps.executeUpdate();(和ps3.executeUpdate();) - 在for循环之后只调用一次。这就是使用addBatch 的全部意义(聚合插入/更新并同时执行它们)。

正如鲍里斯在下面的评论中提到的,如果你也打开rewriteBatchedStatements,它会加快执行速度。请参阅here 如何实现。

【讨论】:

  • 我猜打开rewriteBatchedStatements 也会有帮助?
  • @BoristheSpider 好点,谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多