Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Slow #11

Open
liveresume opened this issue Jan 26, 2017 · 1 comment
Open

Slow #11

liveresume opened this issue Jan 26, 2017 · 1 comment

Comments

@liveresume
Copy link

liveresume commented Jan 26, 2017

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants