The only thing I can spot is indeed the findnext function. Higher order
functions are slow in Julia. It's probably easiest if you just +/- dump
the code of findnext into your code:
julia> @edit findnext(x->x, [5.],1)
# returns the index of the next element for which the function returns true
function findnext(testf::Function, A, start::Integer)
for i = start:length(A)
if testf(A[i])
return i
end
end
return 0
end
On Tue, 2015-05-26 at 10:54, Andrea Cimatoribus <[email protected]>
wrote:
> Hi all, I am very new to Julia, and I am trying it with a dive-in approach,
> translating of a python script of mine. The results are already
> encouraging, since I get almost the same performance as the highly
> optimized numpy code with trivial Julia code (finally I can write for
> cycles!), but I think (because I profiled) I would gain a lot from
> optimizing the following linearly interpolating function:
>
> function interp1d(x::Array{Float64,1},x0::Array{Float64,1},y0::Array{Float64
> ,1})
> y = zeros(x)*NaN;
> jj = 1
> for ii in 1:length(y)
> if x0[1]<x[ii]<x0[end]
> jj = findnext(val->val>x[ii],x0,jj)-1;
> y[ii] = y0[jj] + (y0[jj+1]-y0[jj])/(x0[jj+1]-x0[jj])*(x[ii]-x0[
> jj]);
> end
> end
> return y;
> end
>
>
> I guess that findnext may not be the best choice, and possibly
> metaprogramming could help, but I am mostly new to it and I would
> appreciate some sort of introduction. Could someone point me to resources
> to make progress, or do you have any suggestion?
> Thanks,
> Andrea