fmdb icon indicating copy to clipboard operation
fmdb copied to clipboard

Slow compile time for executeQuery in Swift

Open CarlousF opened this issue 10 years ago • 1 comments

I've been having problems with slow compilation time in my Swift project for a few a days now, and was able to isolate the issue to a single function, which uses the updateQuery function from FMDB. I only recently familiarised myself with Swift and FMDB, so it may be some obvious error which I've overlooked.

Has anyone else encountered this issue? It's adding a good few minutes to compilation time, but I can't understand why. Here's a code snippet of the function:

    let resultSet: FMResultSet! = self.database!.executeQuery("SELECT * FROM Stock WHERE ISIN_Number = ?", withArgumentsInArray: [isinNumber])
    var stockInfo : StockInfo?
    if resultSet != nil {
        while resultSet.next(){
            let isin_Number = resultSet.stringForColumn("ISIN_Number")
            let portID = resultSet.intForColumn("Portfolio_ID")
            let profitLoss = Float(resultSet.doubleForColumn("ProfitLoss"))
            let avPricePerShare = Float(resultSet.doubleForColumn("AveragePricePerShare"))
            let currPricePerShare = Float(resultSet.doubleForColumn("CurrentPricePerShare"))
            let totSharesBought = Float(resultSet.doubleForColumn("TotalSharesBought"))
            let totSharesSold = Float(resultSet.doubleForColumn("TotalSharesSold"))
            let noSharesCurrentlyOwned = Float(resultSet.doubleForColumn("NoCurrentlyOwned"))
            let totalDividendsPaid = resultSet.stringForColumn("TotalDividendsPaid")
            let meanSellPrice = resultSet.stringForColumn("MeanSellPrice")
            let tickerSymbol = resultSet.stringForColumn("TickerSymbol")
            let companyName = resultSet.stringForColumn("CompanyName")
            let dayPL = Float(resultSet.doubleForColumn("DayProfitLoss"))
            let dayHigh = resultSet.stringForColumn("DayHigh")
            let dayLow = resultSet.stringForColumn("DayLow")
            let volume = resultSet.stringForColumn("Volume")
            let yearHigh = resultSet.stringForColumn("YearHigh")
            let yearLow = resultSet.stringForColumn("YearLow")
            let dayChange = resultSet.stringForColumn("DayChange")
            let divPerShare = resultSet.stringForColumn("DividendsPerShare")
            let marketCap = resultSet.stringForColumn("MarketCap")
            let eps = resultSet.stringForColumn("EPS")
            let priceEarningsRatio = resultSet.stringForColumn("PriceEarningsRatio")
            let dayChangePercent = resultSet.stringForColumn("DayChangePercent")

            stockInfo = StockInfo(isin_Number: isin_Number, portID: Int(portID), profitLoss: profitLoss, avPricePerShare: avPricePerShare, currPricePerShare: currPricePerShare, totSharesBought: totSharesBought, totSharesSold: totSharesSold, noSharesCurrentlyOwned: noSharesCurrentlyOwned, totalDividendsPaid: totalDividendsPaid, meanSellPrice: meanSellPrice, tickerSymbol: tickerSymbol, companyName: companyName, dayPL: dayPL, dayHigh: dayHigh, dayLow: dayLow, volume: volume, yearHigh: yearHigh, yearLow: yearLow, dayChange: dayChange, dividendsPerShare: divPerShare, marketCap: marketCap, eps: eps, priceEarningsRatio: priceEarningsRatio, dayChangePercent: dayChangePercent)
        }
    }

    //Close the database
    self.database!.close()

    return stockInfo!`

If anyone knows what I could do to improve the compile time of this code that'd be great!

CarlousF avatar Mar 16 '16 14:03 CarlousF

Fixed it. Seems to be a problem when relying on type inference to work out the types of the different parameters for selection from the table. Compile time dropped by ~80% by specifically telling the compiler what type each of these values should have:

    let resultSet: FMResultSet! = self.database!.executeQuery("SELECT * FROM Stock WHERE ISIN_Number = ?", withArgumentsInArray: [isinNumber])
    var stockInfo : StockInfo?
    if resultSet != nil {
        while resultSet.next(){
            let portID : Int = Int(resultSet.intForColumn("Portfolio_ID"))
            let isin_Number : String = resultSet.stringForColumn("ISIN_Number")
            let profitLoss : Float = Float(resultSet.doubleForColumn("ProfitLoss"))
            let avPricePerShare : Float = Float(resultSet.doubleForColumn("AveragePricePerShare"))
            let currPricePerShare : Float = Float(resultSet.doubleForColumn("CurrentPricePerShare"))
            let totSharesBought : Float = Float(resultSet.doubleForColumn("TotalSharesBought"))
            let totSharesSold : Float = Float(resultSet.doubleForColumn("TotalSharesSold"))
            let noSharesCurrentlyOwned : Float = Float(resultSet.doubleForColumn("NoCurrentlyOwned"))
            let totalDividendsPaid : String = resultSet.stringForColumn("TotalDividendsPaid")
            let meanSellPrice : String = resultSet.stringForColumn("MeanSellPrice")
            let tickerSymbol : String = resultSet.stringForColumn("TickerSymbol")
            let companyName : String = resultSet.stringForColumn("CompanyName")
            let dayPL : Float = Float(resultSet.doubleForColumn("DayProfitLoss"))
            let dayHigh : String = resultSet.stringForColumn("DayHigh")
            let dayLow : String = resultSet.stringForColumn("DayLow")
            let volume : String = resultSet.stringForColumn("Volume")
            let yearHigh : String = resultSet.stringForColumn("YearHigh")
            let yearLow : String = resultSet.stringForColumn("YearLow")
            let dayChange : String = resultSet.stringForColumn("DayChange")
            let divPerShare : String  = resultSet.stringForColumn("DividendsPerShare")
            let marketCap : String = resultSet.stringForColumn("MarketCap")
            let eps : String = resultSet.stringForColumn("EPS")
            let priceEarningsRatio : String = resultSet.stringForColumn("PriceEarningsRatio")
            let dayChangePercent : String = resultSet.stringForColumn("DayChangePercent")

            stockInfo = StockInfo(isin_Number: isin_Number, portID: Int(portID), profitLoss: profitLoss, avPricePerShare: avPricePerShare, currPricePerShare: currPricePerShare, totSharesBought: totSharesBought, totSharesSold: totSharesSold, noSharesCurrentlyOwned: noSharesCurrentlyOwned, totalDividendsPaid: totalDividendsPaid, meanSellPrice: meanSellPrice, tickerSymbol: tickerSymbol, companyName: companyName, dayPL: dayPL, dayHigh: dayHigh, dayLow: dayLow, volume: volume, yearHigh: yearHigh, yearLow: yearLow, dayChange: dayChange, dividendsPerShare: divPerShare, marketCap: marketCap, eps: eps, priceEarningsRatio: priceEarningsRatio, dayChangePercent: dayChangePercent)
        }
    }

    //Close the database
    self.database!.close()

    return stockInfo!`

CarlousF avatar Mar 16 '16 14:03 CarlousF