Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using wrong loop order #136

Open
dcelisgarza opened this issue Apr 22, 2022 · 0 comments
Open

Using wrong loop order #136

dcelisgarza opened this issue Apr 22, 2022 · 0 comments

Comments

@dcelisgarza
Copy link

dcelisgarza commented Apr 22, 2022

Hi, I'm using the package primarily for its moving averages. I found that pretty much every single case uses the wrong loop order for best performance. The fix is pretty easy, ie just swap the inner and outer loops. For example, this function in movingaverages.jl.

function sma(ta::TimeArray, n::Integer)
  tstamps = timestamp(ta)[n:end]

  vals = zeros(size(values(ta),1) - (n-1), size(values(ta),2))
  for i in 1:size(values(ta),1) - (n-1)
    for j in 1:size(values(ta),2)
      vals[i,j] = nanmean(values(ta)[i:i+(n-1),j])[1]
    end
  end

  cname = Symbol[]
  cols  = colnames(ta)
  for c in 1:length(cols)
    push!(cname, Symbol(string(cols[c], "_sma_", n)))
  end

  TimeArray(tstamps, vals, cname, meta(ta))
end

would simply turn into

function sma(ta::TimeArray, n::Integer)
  tstamps = timestamp(ta)[n:end]

  vals = zeros(size(values(ta),1) - (n-1), size(values(ta),2))
  for j in 1:size(values(ta),2)
    for i in 1:size(values(ta),1) - (n-1)
      vals[i,j] = nanmean(values(ta)[i:i+(n-1),j])[1]
    end
  end

  cname = Symbol[]
  cols  = colnames(ta)
  for c in 1:length(cols)
    push!(cname, Symbol(string(cols[c], "_sma_", n)))
  end

  TimeArray(tstamps, vals, cname, meta(ta))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant