Skip to content

Comparison with LinearInterpolators #70

@JeffFessler

Description

@JeffFessler

For the comparison with LinearInterpolators.jl,
I haven't got cubic B-spline to work yet due to a Compat issue.

But here is the linear B-spline comparison:

using LinearInterpolators: SparseInterpolator, LinearSpline
# using InterpolationKernels: BSpline # todo later
using LinearAlgebra: mul!
using Plots
using Random: seed!
default(markerstrokecolor = :auto, label="")
seed!(0)

T = Float64
#T = Float32
N = 100
xs = range(T(0), T(10), N) # source grid
ys = sin.(xs) # initial y data

M = 1401
xf = range(T(-2), T(12), M) # fine grid, just for plotting
yf = sin.(xf)

Q = 1000
xq = sort(14 * rand(T, Q) .- 2) # query points for interpolation
yq = sin.(xq)

p1 = plot()
plot!(xf, yf, label="fine")
scatter!(xs, ys, label="data", marker=:x)
scatter!(xq, yq, label="query", marker=:o)

ker = LinearSpline(T)
# ker = BSpline{4,T} # cubic !
interp1 = SparseInterpolator(ker, xq, xs)

out1 = similar(yq)
mul!(out1, interp1, ys)

plot!(p1, xq, out1, label="interp1")


using FastInterpolations: linear_interp!
using FastInterpolations: ClampExtrap
using FastInterpolations: ExtendExtrap

out2 = similar(yq)

#extrap = ExtendExtrap()
extrap = ClampExtrap()
linear_interp!(out2, xs, ys, xq; extrap)

plot!(p1, xq, out2, label="interp2")
gui()

@assert out1  out2


using BenchmarkTools
@btime mul!($out1, $interp1, $ys); # 2.2 us - 0 alloc
@btime linear_interp!($out2, $xs, $ys, $xq; extrap); # 2.8 us, 1 alloc 64 byte

#=
with T = Float32, I get 
1.7 μs (0 allocations: 0 bytes) - mul!
2.9 μs (1 allocation: 48 bytes) - linear_interp!
=#
nothing

For my work Float32 suffices and curiously, FastInterpolations.jl was slighly slower for 32bit than 64bit, whereas the other package is faster. I had expected both to be faster - less memory to move around.

versioninfo()

Julia Version 1.12.5
Commit 5fe89b8ddc1 (2026-02-09 16:05 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (x86_64-apple-darwin24.0.0)
  CPU: 8 × Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, skylake)
  GC: Built with stock GC
Threads: 8 default, 1 interactive, 8 GC (on 8 virtual cores)
Environment:
  JULIA_NUM_THREADS = 8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions