Chris: based on the idea of a student, I extended his idea to the following utility function for zero order spline with constant step length in the abscissa:
function zero_spline(x,dx,y)
if x < 0
return y[1]
elseif x >= length(y)*dx
return y[end]
else
return y[trunc(Int,x/dx)+1]
end
end
I’ve run some simple benchmarks based on a vector pair xd
, yd
each of length(xd) = 10_001
:
julia> y = ZeroSpline(yd,xd)
julia> x = range(0,8000*60,length=10^4)
julia> @benchmark y.(x)
BenchmarkTools.Trial:
memory estimate: 78.27 KiB
allocs estimate: 3
--------------
minimum time: 202.679 ms (0.00% GC)
median time: 228.310 ms (0.00% GC)
mean time: 225.889 ms (0.00% GC)
maximum time: 245.988 ms (0.00% GC)
--------------
samples: 23
evals/sample: 1
If I instead use my utility function, I find:
julia> y1 = (x) -> zero_spline(x,60,xd)
julia> @benchmark y1.(x)
BenchmarkTools.Trial:
memory estimate: 539.33 KiB
allocs estimate: 29497
--------------
minimum time: 809.201 μs (0.00% GC)
median time: 1.401 ms (0.00% GC)
mean time: 1.498 ms (3.67% GC)
maximum time: 6.355 ms (38.53% GC)
--------------
samples: 3327
evals/sample: 1
OK… the ZeroSpline
instantiator uses ca. 7 times less memory, but “my” utility function is 150 times faster. If I remove the “safeguards” which allows for extrapolation, my code is a little bit faster, but not much.
Questions
- Is there a version of
ZeroSpline
for the case of constant abscissa step size? - Would a
ZeroSpline
instantiator with constant abscissa step size be as fast as “my” utility function? - Is there a way to reduce the memory usage of my utility function?
Of course, I realize that the DataInterpolations
package is much more fancy than my simple utility function.