Speed improvement possible?

Would appreciate tips to improve speed of code below. Transforms a dataset into an input matrix and corresponding output whilst skipping missing output values

function AddMatrixToInput!(row,day,instrument,Imatrix,pcMatrix,rankedcorrelationlist,lookback,relatedInstruments)
    for i in 1:lookback
         for j=1:relatedInstruments
            Imatrix[i,j,row]=min(max(pcMatrix[day-i,rankedcorrelationlist[instrument,j]],-0.2),0.25)
        end
    end
end
#Convert a percent change matrix into an input vs output set. Ignore if output is missing
function generateInputOutput(pcMatrix,rankedcorrelationlist;lookback=5,relatedInstruments=10)
    r,c = size(pcMatrix)
    Output=Array{UInt8}(undef,r*c)
    Imatrix=Array{Float32,3}(undef,lookback,relatedInstruments,r*c)
    row=1
    for instrument in 1:c
        for day in lookback+1:r
            if pcMatrix[day,instrument]!=0.0    #test if output is missing
                AddMatrixToInput!(row,day,instrument,Imatrix,pcMatrix,rankedcorrelationlist,lookback,relatedInstruments)
                Output[row]=pcMatrix[day,instrument]>0 ? 1 : 0
                row+=1
            end
        end
        println(instrument/c)
    end
    resize!(Output,row-1) #resize for missing outputs
    IMatrix=[Imatrix[i,j,k] for i=1:size(Imatrix,1),j=1:size(Imatrix,2),k=1:row-1]
    return IMatrix,Output
end
1 Like

It would help with an example to actually run the code.
That said, making the i loop the inner loop will help:

function AddMatrixToInput!(row,day,instrument,Imatrix,pcMatrix,rankedcorrelationlist,lookback,relatedInstruments)
    for j=1:relatedInstruments, i in 1:lookback
        Imatrix[i,j,row]=min(max(pcMatrix[day-i,rankedcorrelationlist[instrument,j]],-0.2),0.25)
    end
end

You could also use annotations like @inbounds. Iā€™d recommend reading the performance tips.

3 Likes