X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



4206 Rate this article:
No rating

Optimizing derivative approximations in IDL


Computing standard derivative approximations that are not included as part of the standard library (The discrete 2D Laplacian for example) can be very time consuming. This Help Article demonstrates how IDL's SHIFT function can be used, in place of a FOR loop, to improve performance when approximating derivatives.

Use the SHIFT function to avoid FOR loops.

Since FOR loops are handled at the interpreter level, they execute slowly in comparison to an equivalent operation that uses an IDL intrinsic function. In the latter case IDL simply packages the information and sends it to an optimized C routine, so only a small amount of overhead is incurred in the process.

Below is an example of a 2d laplacian code using the SHIFT function. The code includes a timing test to compare with the standard FOR-loop formulation. At the chosen resolution, the SHIFT formulation ran up to 100 times faster on certain platforms.
pro ApproximateLaplacian
;Main level program to create
;some random data and compute
;its 2d laplacian, using both
;the SHIFT and LOOP formulations
;and producing timing results

nx=512 & ny=512
f = FLTARR(nx+2,ny+2)
g1 = FLTARR(nx+2,ny+2)
g2 = FLTARR(nx+2,ny+2)

f(1:nx,1:ny) = RANDOMU(seed,nx,ny)

t0 = SYSTIME(1)

;Compute LAPLACIAN
FOR i = 1, nx DO BEGIN
FOR j = 1,ny DO BEGIN
g1(i,j) = f(i+1,j) + f(i-1,j) + $
f(i,j+1) + f(i,j-1) - $
4*f(i,j)
ENDFOR
ENDFOR

t1 = SYSTIME(1)

;Compute LAPLACIAN using SHIFT
g2 = SHIFT(f,-1,0) + SHIFT(f,1,0) + $
SHIFT(f,0,1) + SHIFT(f,0,-1) - $
4*f

t2=SYSTIME(1)

time1 = t1-t0
time2 = t2-t1

;Compare times
PRINT, time1, time2

END
Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »