Skip to content

Index lookup with Range grids slower than Vector grids for constant, linear and quadratic interpolations #85

@pmc4

Description

@pmc4

Hello. I've discovered this issue while playing with range grids. I wanted to see how much better was using a Range grid than a Vector one when my Y values are SMatrix. For my surprise, they are slower for constant, linear and quadratic interpolations.

Let me show this with a MWE:

using StaticArrays, FastInterpolations, BenchmarkTools

mutable struct Foo{T1, T2}
    xs::T1
    ys::T2
end

function Foo(xs::T) where T<:AbstractVector
    ys = [@SMatrix ones(ComplexF64,3,3) for i in eachindex(xs)] # Everything is a ones(3,3) for simplicity
    ysType = typeof(ys)

    return Foo{T, ysType}(xs, ys)
end

Here Foo is just a struct with a grid xs and the ys values to interpolate. My real use case is more complex than that, but it has to be inside a struct. For the MWE I'm using SMatrix, even though with SHermitianCompact the result is the same.

Now I create a rather large grid with N=1000 points, but using less than 100 points does not change problem. Let's see how much time they take when xs is a range vs when it is a vector.

N = 1000
xs1 = range(0.01, 30, N)
foo1 = Foo(xs1) # foo1 is for the range grid

xs2 = collect(range(0.01, 30, N))
foo2 = Foo(xs2) # foo2 is for the vector grid

p = 6.789 # random point
# Constant interp
@btime constant_interp($foo1.xs, $foo1.ys, $p); # 16.543 ns (0 allocations: 0 bytes)
@btime constant_interp($foo2.xs, $foo2.ys, $p); # 8.765 ns (0 allocations: 0 bytes)
# Linear interp
@btime linear_interp($foo1.xs, $foo1.ys, $p); # 15.736 ns (0 allocations: 0 bytes)
@btime linear_interp($foo2.xs, $foo2.ys, $p); # 6.903 ns (0 allocations: 0 bytes)
# Quadratic interp
@btime quadratic_interp($foo1.xs, $foo1.ys, $p); # 10.162 μs (0 allocations: 0 bytes)
@btime quadratic_interp($foo2.xs, $foo2.ys, $p); # 8.872 μs (0 allocations: 0 bytes)
# Cubic interp
@btime cubic_interp($foo1.xs, $foo1.ys, $p); # 7.870 μs (0 allocations: 0 bytes)
@btime cubic_interp($foo2.xs, $foo2.ys, $p); # 16.417 μs (0 allocations: 0 bytes)

As we can see, only cubic_interp has a faster lookup for a Range grid. These benchmarks are for my machine, let me know if you need more info about the specs or everything else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance-related issue or optimization opportunity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions