Skip to content

Comparing with Matplotlib's triangulation #34

@henry2004y

Description

@henry2004y

Hi! Thanks for the great package!

As a user, I want to report a performance comparison I found against Matplotlib:

using NaturalNeighbours
using StableRNGs
using PyPlot

function meshgrid(x, y)
   X = [x for _ in y, x in x]
   Y = [y for y in y, _ in x]

   X, Y
end

## The data 
rng = StableRNG(123)
f = (x, y) -> sin(x * y) - cos(x - y) * exp(-(x - y)^2)
x = vec([(i - 1) / 9 for i in (1, 3, 4, 5, 8, 9, 10), j in (1, 2, 3, 5, 6, 7, 9, 10)])
y = vec([(j - 1) / 9 for i in (1, 3, 4, 5, 8, 9, 10), j in (1, 2, 3, 5, 6, 7, 9, 10)])
z = f.(x, y)

## The interpolant and grid
xg = LinRange(0, 1, 100)
yg = LinRange(0, 1, 100)
_x = vec([x for x in xg, _ in yg])
_y = vec([y for _ in xg, y in yg])

## Evaluate some interpolants
println("NaturalNeighbours.jl:")
@time begin
   itp = interpolate(x, y, z; derivatives=true)
   triangle_vals = itp(_x, _y; method=Triangle())
   _x = vec([x for x in xg, _ in yg])
   _y = vec([y for _ in xg, y in yg])
   Wi1 = itp(_x, _y; method=Triangle());
end
@time begin
   itp = interpolate(x, y, z; derivatives=true)
   triangle_vals = itp(_x, _y; method=Triangle())
   _x = vec([x for x in xg, _ in yg])
   _y = vec([y for _ in xg, y in yg])
   Wi1 = itp(_x, _y; method=Triangle());
end

println("Matplotlib:")
@time begin
   triang = matplotlib.tri.Triangulation(x, y)
   # Perform linear interpolation on the triangle mesh
   interpolator = matplotlib.tri.LinearTriInterpolator(triang, z)
   Xi, Yi = meshgrid(xg, yg)
   Wi = interpolator(Xi, Yi)
end
@time begin
   triang = matplotlib.tri.Triangulation(x, y)
   # Perform linear interpolation on the triangle mesh
   interpolator = matplotlib.tri.LinearTriInterpolator(triang, z)
   Xi, Yi = meshgrid(xg, yg)
   Wi = interpolator(Xi, Yi)
end

println("")
NaturalNeighbours.jl:
  5.368652 seconds (8.50 M allocations: 560.056 MiB, 5.17% gc time, 99.57% compilation time)
  0.089407 seconds (150.61 k allocations: 7.037 MiB, 24.26% gc time, 74.26% compilation time)
Matplotlib:
  0.207081 seconds (288.81 k allocations: 19.936 MiB, 97.51% compilation time: 6% of which was recompilation)
  0.001353 seconds (80 allocations: 239.234 KiB)

This is running with 1 thread, but NaturalNeighbours.jl is ~ 16 times slower. I don't know if Matplotlib automatically uses multithreading, but this performance difference is noticeable. I don't trust the allocation statistics reported above for Matplotlib, but the measured time should be ok.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions