# Speed improvement possible?

**URL:** <https://discourse.julialang.org/t/speed-improvement-possible/44065>\
**Category:** Performance\
**Created:** [July 31, 2020, 11:10pm UTC](https://discourse.julialang.org/t/speed-improvement-possible/44065 "2020-07-31T23:10:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruan\_Stander](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruan_stander/32/11304_2.png) [@Ruan\_Stander](https://discourse.julialang.org/u/Ruan_Stander)\
**Post date:** [July 31, 2020, 11:10pm UTC](https://discourse.julialang.org/t/speed-improvement-possible/44065/1 "2020-07-31T23:10:39Z")

</div>

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

```julia
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
```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [August 1, 2020, 6:46am UTC](https://discourse.julialang.org/t/speed-improvement-possible/44065/2 "2020-08-01T06:46:14Z")

</div>

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

```julia
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](https://docs.julialang.org/en/v1/manual/performance-tips/index.html).
