Daniel Jones wrote:

> 
> Here's a version that's 7x faster (and almost 8x if you disable bounds
> checking). The main thing is to avoid indexing into arrays with ranges (as
> in u[i:j]) if you can, since that will allocate and copy a new array each
> time. And also, like Patrick said, globals should usually be declared with
> 'const'.
> 
> On Thursday, July 31, 2014 8:38:33 AM UTC-7, Neal Becker wrote:
>>
>> Attached is my 1st attempt at julia, it is a simple FIR filter, which I
>> translated from my c++ version.
>>
>> It is benchmarking about 10x slower than python wrapped c++ version.
>>
>> Any suggestions?

I was concerned about that indexing arrays causing copying, and to get closer 
to 
the c++ version (which does not copy), I tried out ArrayViews.  I'll attach 
that 
version, but I did not see any speedup.
module firmod

import ArrayViews
using ArrayViews.view

type FIR{in_t,coef_t}
    in::Vector{in_t}
    coef::Vector{coef_t}
    function FIR (coef::Vector{coef_t})
        new (zeros (in_t, size (coef)), copy (coef))
    end
end

  ## template<typename i_t>
  ## void shift (i_t i) {
  ##   std::copy_backward (in.begin(), in.end()-boost::size(i), in.end());
  ##   std::reverse_copy (i.begin(), i.end(), in.begin());
  ## }
function shift!{in_t,coef_t} (f::FIR{in_t,coef_t}, u::DenseArray{in_t,1})
    si = length(f.in)
    su = length(u)
    for i =si-su:-1:1
        f.in[i+su] = f.in[i]
    end
    f.in[1:su] = u[end:-1:1]
end

function compute1{in_t, coef_t} (f::FIR{in_t,coef_t})
    s = zero (promote_type (in_t, coef_t))
    size = length(f.coef)
    for i = 1:size
        s += f.in[i] * f.coef[i]
    end
    return s
end

function shift_compute1{in_t,coef_t} (f::FIR{in_t,coef_t}, 
u::DenseArray{in_t,1})
    shift! (f, u)
    return compute1 (f)
end


function compute{in_t,coef_t} (f::FIR{in_t,coef_t}, u::DenseArray{in_t,1})
    out = similar (u, promote_type (in_t,coef_t))
    size = length (u)
    for i = 1:size
        out[i] = shift_compute1 (f, view (u,i:i))
    end
    return out
end

c = float ([1:4])
g = FIR{Float64,Float64} (c)
h = FIR{Complex{Float64},Float64} (c)
j = float ([1:10])
k = compute (g, j)

function timeit()
    n = 10
    l = 1000000
    u = zeros(Float64, l)

    for i=1:n
        v = compute (g, u)
    end
end

end

Reply via email to