编写高效率的IDL程序

1、 尽量避免或少用循环

2、 多用whereHistogram

PRO TEST_TIME
;
a =  DIST(2000,2000)
sum = 0.
sum1 = 0.
start = SYSTIME(1)
FOR i=0L,N_ELEMENTS(a)-1L DO BEGIN
IF(a[i] GT 100.0) THEN  BEGIN
sum = sum +a[i]
ENDIF
ENDFOR
fortime = SYSTIME(1)-start
PRINT,'for time:',fortime

i=0L
start = SYSTIME(1)
WHILE i LT N_ELEMENTS(a)-1L DO BEGIN
IF(a[i] GT 100.0)THEN sum = sum +a[i]
i++
ENDWHILE
whiletime = SYSTIME(1)-start
PRINT,'while time:',whiletime

start = SYSTIME(1)
sum = TOTAL(a * (a GT 100.0))
funtime = SYSTIME(1)-start
PRINT,'function time:',funtime
;倍数
print,'result for VS function:', fortime/funtime
print,'result while VS function:',whiletime/funtime
END

IDL自带的一个图像为例，将数据值等于142的赋为0.

;≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌
;
;计算两个点的距离
;
function CalDistance, point1, point2
compile_opt idl2    ;
Return, SQRT((point1[0]-point2[0])^2+(point1[1]-point2[1])^2)
end
;≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌
;
;搜索当前坐标周围Distance内的下标，注意输入x和y方向的坐标范围xRange和yRange
;
function calIdxInDistance, curLoc, distance;,xRange,yRange
;初始化临时下标
suitLoc = [0,0]
;循环一次，计算矩形范围内的符合要求下标
for xLoc = curLoc[0]-distance, curLoc[0]+distance do begin
for yLoc = curLoc[1]-distance, curLoc[1]+distance  do begin
if calDistance(curLoc, [xLoc,yLoc]) LE distance then suitLoc = [[suitLoc],[xLoc,yLoc]]
endfor
end
;
return, suitLoc[*,1:(N_Elements(suitLoc)/2-1)]

end
;≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌
;
;测试即调用主函数
pro test_process

;原数据
file = FILEPATH('rbcells.jpg', \$
SUBDIRECTORY = ['examples', 'data'])
;
if size(data,/n_dimensions) ne 2 then return
dims = size(data,/dimension)
startTime  = systime(1)

;当值等于142时，半径5内的元素赋值为0
eqValue = 142
repValue = 0
;符合要求的坐标
suitIdx = calIdxInDistance([0,0],5)
;新数据
nData = data
idxs = where(data eq eqValue,count)
for curIdx =0,count-1 do begin

nSuit = suitIdx
;转换为二维坐标
suitLoc = ARRAY_INDICES(data, idxs[curIdx])
nSuit[0,*]= nSuit[0,*]+suitLoc[0]
nSuit[1,*]= nSuit[1,*]+suitLoc[1]
;下标要在数组自身范围内
nSuit[0,*] = dims[0] < nSuit[0,*] > 0
nSuit[1,*] = dims[1] < nSuit[1,*] > 0
;符合要求的位置赋值
nData[nSuit[0,*],nSuit[1,*]] = repValue
endfor
;输出花费时间
print,systime(1) - startTime
;
ns = dims[0]
nl = dims[1]
new = data
new1 = data
startTime = systime(1)
for i=0,ns-1 do begin
for j=0,nl-1 do begin
if new[i,j] eq eqValue then begin
for m=0,ns-1 do begin
for n=0,nl-1 do begin
if (m-i*1L)^2+(n-j*1L)^2 le 25 then new1[m,n]=0
endfor
endfor
endif
endfor

endfor
print,'common time:',systime(1) - startTime

window,1,xSize = dims[0]*2,ySize = dims[1]
tvscl,Data,0
tvscl,ndata,1
window,2,xSize = dims[0]*2,ySize = dims[1]
tvscl,new,0
tvscl,new1,1

end

http://wap.sciencenet.cn/blog-344887-487680.html

全部精选博文导读

GMT+8, 2021-11-30 21:20