Skip to content

Instantly share code, notes, and snippets.

@moble
Created March 18, 2023 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moble/8c3c3369324f0597045db09a6575ad35 to your computer and use it in GitHub Desktop.
Save moble/8c3c3369324f0597045db09a6575ad35 to your computer and use it in GitHub Desktop.
# The vector method is roughly 2.5x than the existing code with a
# sorted t′, and about 15% slower with a worst-case-disordered t′.
using BenchmarkTools
using DataInterpolations
shuffle(v) = collect(Iterators.flatten(zip(v[begin:length(v)÷2], v[length(v)÷2+1:end])))
function inplace(u, interp, t)
@assert length(u) == length(t)
@inbounds for i in eachindex(t)
u[i] = interp(t[i])
end
u
end
N = 1_000_000
t = LinRange(-1_000.0, 10_000.0, N);
s = CubicSpline(cos.(t), t);
l = LinearInterpolation(cos.(t), t);
t_mid = (t[begin:end-1] + t[begin+1:end]) / 2;
t_shuff = shuffle(t_mid);
u′ = s(t_mid);
u′′ = s(t_shuff);
v′ = l(t_mid);
v′′ = l(t_shuff);
println("#"^80)
println("CubicSpline with typical arrangement of time points")
println("New DataInterpolations method:")
display(@benchmark $s($u′, $t_mid))
println("\n\nOld looped approach:")
display(@benchmark inplace($u′, $s, $t_mid))
println("\n\n" * "#"^80)
println("LinearInterpolation with typical arrangement of time points")
println("New DataInterpolations method:")
display(@benchmark $l($v′, $t_mid))
println("\n\nOld looped approach:")
display(@benchmark inplace($v′, $l, $t_mid))
println("\n\n" * "#"^80)
println("CubicSpline with worst-case arrangement of time points")
println("New DataInterpolations method:")
display(@benchmark $s($u′′, $t_shuff))
println("\n\nOld looped approach:")
display(@benchmark inplace($u′′, $s, $t_shuff))
println("\n\n" * "#"^80)
println("LinearInterpolation with worst-case arrangement of time points")
println("New DataInterpolations method:")
display(@benchmark $l($v′′, $t_shuff))
println("\n\nOld looped approach:")
display(@benchmark inplace($v′′, $l, $t_shuff))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment