dbstein / fast_interp

numba accelerated interpolation on regular grids in 1, 2, and 3 dimensions
Apache License 2.0
62 stars 15 forks source link

Slower than scipy #7

Open inigozubeldia opened 1 year ago

inigozubeldia commented 1 year ago

Hello,

I've tried the following test code, which does 2d interpolation, and fast_interp is slower than scipy by three orders of magnitude. Is there something obvious I am doing wrong?

import fast_interp
import numpy as np
import time
import scipy.interpolate as interpolate

nx = 50
ny = 37
xv, xh = np.linspace(0, 1,       nx, endpoint=True,  retstep=True)
yv, yh = np.linspace(0, 2*np.pi, ny, endpoint=False, retstep=True)
x, y = np.meshgrid(xv, yv, indexing='ij')

test_function = lambda x, y: np.exp(x)*np.exp(np.sin(y))
f = test_function(x, y)

n = 128**2
test_x = np.random.rand(n)
test_y = np.random.rand(n)

t0 = time.time()

interpolater = fast_interp.interp2d([0,0], [1,2*np.pi], [xh,yh], f, k=1, p=[False,False], e=[0,0])
fe = interpolater(test_x, test_y)

t1 = time.time()

print(t1-t0)

cpdf = interpolate.RegularGridInterpolator((xv,yv),f,method="linear",fill_value=0.,bounds_error=False)((test_x,test_y))

t2 = time.time()

print(t2-t1)

I get t1-t0 = 2.1 s (fast_interp) and t2-t1 = 0.008 s (scipy).

ReadingClouds commented 1 year ago

One comment - fast_interp uses numba, so first time you call it the code gets 'compiled' (in the numba sense). This is a one-time overhead.

Prof. Peter Clark Department of Meteorology University of Reading Tel: +44(0) 118 378 7908<tel:+44%20118%20378%207908> email: @.**@.>

Please Note: as of October 2023 my working days are Thursday and Friday. I may not respond promptly to emails sent on other days (or even on Thursday or Friday).

Remember what the dormouse said...

From: inigozubeldia @.> Sent: Tuesday, October 24, 2023 6:46 PM To: dbstein/fast_interp @.> Cc: Subscribed @.***> Subject: [dbstein/fast_interp] Slower than scipy (Issue #7)

Hello,

I've tried the following test code, which does 2d interpolation, and fast_interp is slower than scipy by three orders of magnitude. Is there something obvious I am doing wrong?

import cnc import numpy as np import time import scipy.interpolate as interpolate

nx = 50 ny = 37 xv, xh = np.linspace(0, 1, nx, endpoint=True, retstep=True) yv, yh = np.linspace(0, 2*np.pi, ny, endpoint=False, retstep=True) x, y = np.meshgrid(xv, yv, indexing='ij')

test_function = lambda x, y: np.exp(x)*np.exp(np.sin(y)) f = test_function(x, y)

n = 128**2 test_x = np.random.rand(n) test_y = np.random.rand(n)

t0 = time.time()

interpolater = cnc.interp2d([0,0], [1,2*np.pi], [xh,yh], f, k=1, p=[False,False], e=[0,0]) fe = interpolater(test_x, test_y)

t1 = time.time()

print(t1-t0)

cpdf = interpolate.RegularGridInterpolator((xv,yv),f,method="linear",fill_value=0.,bounds_error=False)((test_x,test_y))

t2 = time.time()

print(t2-t1)

I get t1-t0 = 2.1 s (fast_interp) and t2-t1 = 0.008 s (scipy).

- Reply to this email directly, view it on GitHubhttps://github.com/dbstein/fast_interp/issues/7, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQXWEKAF64WQWOYNN3FPQTYA75FFAVCNFSM6AAAAAA6OCNA5CVHI2DSMVQWIX3LMV43ASLTON2WKOZRHE2TSNZWGYZDCMA. You are receiving this because you are subscribed to this thread.Message ID: @.**@.>>

shashi4u commented 1 month ago

For a large dataset of 4096x4096 regular grid fast_interp is around 2918.554 ms (~ 3 secs) whereas scipy's RectBivariateSpline is around 31405.037 (~31 secs). In this case fast_interp is 10x faster compared to scipy's RectBivariateSpline.

From my understanding fast_interp is not advisable if you want to interpolate small datasets only once.

When to use fast_interp:

When to avoid fast_interp: