JuliaMath / JuliaMath/Interpolations.jl

Docs direct users to slow options

Open
#462 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
575
Forks
117
PR merge metrics
No merged PRs in 30d

Description

This may not need to be changed, but just FYI `LinearInterpolation` is *not* free of extrapolation, it merely defines extrapolation with `Throw()` behavior. And this does not come cheap:
```julia
julia> using Interpolations, BenchmarkTools

julia> xs = 1:0.2:5
1.0:0.2:5.0

julia> f(x) = log(x)
f (generic function with 1 method)

julia> A = [f(x) for x in xs];

julia> itp = LinearInterpolation(xs, A); # what the README directs people to use

julia> itp2 = scale(interpolate(A, BSpline(Linear())), xs); # a lower-level alternative

julia> function interpolate_many(itp, iv, n)
s = zero(itp(minimum(iv)))
xeval = minimum(iv) .+ (maximum(iv) - minimum(iv)).*rand(eltype(iv), n)
@inbounds @simd for i = 1:n
s += itp(xeval[i])
end
return s
end
interpolate_many (generic function with 1 method)

julia> @benchmark interpolate_many(itp, xs, 10000)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 51.328 μs … 865.010 μs ┊ GC (min … max): 0.00% … 93.11%
Time (median): 56.020 μs ┊ GC (median): 0.00%
Time (mean ± σ): 63.901 μs ± 36.250 μs ┊ GC (mean ± σ): 2.71% ± 4.93%

▂▇▇█▆▁▁▂▂▂ ▁▂▃▃▃▂▂▂ ▂
████████████▆▆▅▆▇▆█▇▆▅▃▄▃▄▆▅▅▄▂▄▃▅▄▄▅▇█████████▇▆▆▆▅▃▅▅▅▄▅▃▄ █
51.3 μs Histogram: log(frequency) by time 113 μs <

Memory estimate: 156.42 KiB, allocs estimate: 5.

julia> @benchmark interpolate_many(itp2, xs, 10000)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 33.864 μs … 910.252 μs ┊ GC (min … max): 0.00% … 93.33%
Time (median): 36.828 μs ┊ GC (median): 0.00%
Time (mean ± σ): 44.396 μs ± 38.660 μs ┊ GC (mean ± σ): 4.35% ± 5.26%

▃▇█▆▂▁ ▂▂▁ ▃▃▂▂▂▃▂▁ ▂
████████████▇▆▅▅▅▅▅▆▇█▇▅▅▅▃▃▃▁▅▅▃▃▁▄▃▅▃▄▄▅▁▄▄█████████▇▆▇▆▆▇ █
33.9 μs Histogram: log(frequency) by time 84.6 μs <

Memory estimate: 156.42 KiB, allocs estimate: 5.
```

Let's also try with integer-indexes:
```julia
julia> itpi = LinearInterpolation(1:length(A), A);

julia> itpi2 = interpolate(A, BSpline(Linear()));

julia> @benchmark interpolate_many(itpi, 1.0:length(A), 10000)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 51.644 μs … 934.393 μs ┊ GC (min … max): 0.00% … 92.43%
Time (median): 54.011 μs ┊ GC (median): 0.00%
Time (mean ± σ): 57.281 μs ± 25.354 μs ┊ GC (mean ± σ): 2.35% ± 5.08%

▃▅██▅▁ ▁
██████▇▇▇▇▅▆▅▆▅▄▅▅▅▄▅▅▄▄▄▄▄▃▄▄▃▂▂▃▄▃▄▄▄▄▃▂▅▅▅▅▅▅▆▆▇▇▆▆▇██▇▅▅ █
51.6 μs Histogram: log(frequency) by time 98.2 μs <

Memory estimate: 156.45 KiB, allocs estimate: 6.

julia> @benchmark interpolate_many(itpi2, 1.0:length(A), 10000)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 20.348 μs … 557.423 μs ┊ GC (min … max): 0.00% … 90.11%
Time (median): 24.013 μs ┊ GC (median): 0.00%
Time (mean ± σ): 26.495 μs ± 25.515 μs ┊ GC (mean ± σ): 5.28% ± 5.32%

▂▁ ▄██▅▂ ▂
██▇██████▇██▇▆▆▅▆▆▅▄▅▅▅▆▆▅▅▄▅▃▅▄▁▃▁▅▁▁▁▄▁▁▅▄▅▃▅▅▃▁▅▃▅▄▃▆██▇▇ █
20.3 μs Histogram: log(frequency) by time 63.9 μs <

Memory estimate: 156.45 KiB, allocs estimate: 6.
```

The difference is the branch, and SIMD-vectorization. My laptop is AVX2, an AVX512 machine would show even larger differences.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.