优化代码以提高速度

时间:2016-12-02 16:50:40

标签: java mysql optimization

我已经编写了一个看起来非常慢的程序,它需要花费很多分钟而不是几秒钟来从数据库中读取和写入数据。它似乎在我的 runComparisons 方法中放慢了很多,但我不确定我应该采取哪些步骤来纠正这个问题。

full program

 public static void runComparisons(String industryTitle, List<String> tickerName, TradingInterval tradingInterval) throws SQLException {

        // Get primary performance values ticker return
        for (String ticker : tickerName) {
            String startDate = tradingInterval.beginDate;
            //System.out.println("start date " + startDate);
            String endDate = tradingInterval.endDate;
            //System.out.println("end date " + endDate);
            PreparedStatement statement = conn.prepareStatement
                    ("select P.TransDate, P.openPrice, P.closePrice" +
                            " from PriceVolume P" +
                            " where Ticker = ? and TransDate>= ? and TransDate <= ?" +
                            "order by TransDate DESC");

            statement.setString(1, ticker);
            statement.setString(2, startDate);
            statement.setString(3, endDate);
            ResultSet result = statement.executeQuery();

            String date;
            double nextOpenPrice = 0.0;
            double currentClosePrice;
            //int numberDays = 1;
            //int splitCounter = 0;
            double divideAmount = 1;
            String tickerSymbol = "";

            // create list to contain dates for use in investment strategy
            ArrayList<holdSplitVals> ascendingList = new ArrayList<>();


            // process result set
            while (result.next()) {
                //tickerSymbol = result.getString("Ticker");
                // reset split amount for next iteration
                date = result.getString("TransDate");
                double openPrice = result.getDouble("OpenPrice");
                double closePrice = result.getDouble("ClosePrice");


                currentClosePrice = closePrice;
                // get closing/opening ratio

                double splitAmount = getRatios(date, nextOpenPrice, currentClosePrice);


                if (splitAmount > 0) {
                    divideAmount = divideAmount * splitAmount;
                    //splitCounter++;
                }

                // hold data in helper class
                holdSplitVals data = new holdSplitVals();
                data.date = date;
                data.openPrice = openPrice / divideAmount;
                data.closePrice = closePrice / divideAmount;

                ascendingList.add(0, data);

                nextOpenPrice = openPrice;

                if (ascendingList.size() >= 1) {

                    outputTable(closePrice, openPrice, industryTitle, tickerSymbol, startDate, endDate);

                }
            }
        }
    }

    private static void outputTable(double closePrice, double openPrice, String industryTitle, String tickerSymbol, String startDate, String endDate) throws SQLException {
        double tickerNumber = (closePrice / openPrice) - 1;
        DecimalFormat df = new DecimalFormat("#.####");
        df.setRoundingMode(RoundingMode.CEILING);
        String tickerNum = df.format(tickerNumber);

        PreparedStatement writeStatement = writerconn.prepareStatement(
                "insert into Performance(Industry, Ticker, StartDate, EndDate, TickerReturn)"
                        + "values(?, ?, ?, ?, ?)");
        writeStatement.setString(1, industryTitle);
        writeStatement.setString(2, tickerSymbol);
        writeStatement.setString(3, startDate);
        writeStatement.setString(4, endDate);
        writeStatement.setString(5, tickerNum);

        writeStatement.executeUpdate();
    }

    private static double getRatios(String date, double nextOpenPrice, double currentClosePrice) {
        double coRatio = currentClosePrice / nextOpenPrice;
        // calculate splits
        // for 2:1 split
        if (Math.abs(coRatio - 2.0) < 0.20) {
            //splitCounter++;
            //splitDate = ("2:1 split on " + date);
            //System.out.println("2:1 split on " + date + " " + currentClosePrice + "--> " + nextOpenPrice);
            return 2.0;
            // for 3:1 split
        } else if (Math.abs(coRatio - 3.0) < 0.20) {
            //splitCounter++;
            //splitDate = ("3:1 split on "+ date);
            //System.out.println("3:1 split on " + date + " " + currentClosePrice + "--> " + nextOpenPrice);
            return 3.0;
            // for 3:2 split
        } else if (Math.abs(coRatio - 1.5) < 0.15) {
            //splitCounter++;
            //splitDate = ("3:2 split on " + date);
            //System.out.println("3:2 split on " + date + " " + currentClosePrice + "--> " + nextOpenPrice);
            return 1.5;
        }
        return 0;
    }
}

0 个答案:

没有答案