invenia / RingArrays.jl

A sliding window over a huge array.
Other
3 stars 3 forks source link

Slow #11

Open liveresume opened 7 years ago

liveresume commented 7 years ago

Why is this running so slowly?

using RingArrays
function sma(arr, P)
  tlen = length(arr)
  tarr = zeros(tlen)
  ring = RingArray{Int, 1}(max_blocks=P, block_size=(1,), data_length=tlen)
  for i in 1:tlen-P+1
    load_block(ring, [arr[i]])
    tarr[i+P-1] = mean(ring[ring.range])
  end
  tarr
end

@time sma([1:20], 5)
 0.158607 seconds (3.37 k allocations: 765.797 KB, 99.45% gc time)
samuel-massinon commented 7 years ago

Can you run @time sma([1:20], 5) again and see if the speed increases? The first time you run it, most of the time is due to compilation.

For example:

julia> using RingArrays

julia> function sma(arr, P)
         tlen = length(arr)
         tarr = zeros(tlen)
         ring = RingArray{Int, 1}(max_blocks=P, block_size=(1,), data_length=tlen)
         for i in 1:tlen-P+1
           load_block(ring, [arr[i]])
           tarr[i+P-1] = mean(ring[ring.range])
         end
         tarr
       end
sma (generic function with 1 method)

julia> @time sma([1:20], 5)
  0.086269 seconds (99.22 k allocations: 4.309 MB)
1-element Array{Float64,1}:
 0.0

julia> @time sma([1:20], 5)
  0.000014 seconds (12 allocations: 864 bytes)
1-element Array{Float64,1}:
 0.0