优化这段函数:FUNCTION fb_get_pointer(a, b , c, d AS FLOAT(), rtn AS INTEGER(), ratio AS FLOAT) DIM i , j AS INTEGER DIM start_i, start_j , stop_j , stop_i AS INTEGER DIM rtn1 , rtn2 AS INTEGER DIM exit_temp , data_temp , flag_temp , exit_temp2 AS INTEGER start_i = start_j = stop_j = stop_i = exit_temp = 0 data_temp = flag_temp = exit_temp2 = 0 FOR j = 0 TO 899 FOR i = 0 TO 1799 IF ABS(a(j) - b(i)) < ratio AND _ ABS(c(j) - d(i)) < ratio THEN 'PRINT j , i, a(j) , b(i), c(j), d(i) , start_j , start_i IF start_i = 0 THEN start_i = i IF start_j = 0 THEN start_j = j IF stop_i < i THEN stop_i = i IF stop_j < j THEN stop_j = j ENDIF data_temp = i exit_temp = 1 flag_temp = 1 ELSEIF data_temp <> i AND i= 1799 AND exit_temp = 0 AND flag_temp = 1 THEN ' PRINT j , i exit_temp2 = 1 EXIT_LOOP ENDIF NEXT i exit_temp = 0 IF exit_temp2 = 1 THEN EXIT_LOOP ENDIF NEXT j rtn1 = INT((start_j + stop_j) / 2) rtn2 = INT((start_i + stop_i) / 2) ' PRINT rtn1 , rtn2 rtn = rtn1 , rtn2 ENDFUNC
时间: 2023-03-31 22:00:17 浏览: 152
这段函数可以进行以下优化:
1. 将循环中的 ABS 函数提前计算并存储,避免重复计算。
2. 将循环中的 IF 判断语句合并,减少判断次数。
3. 将循环中的变量赋值语句提前计算并存储,避免重复计算。
优化后的代码如下:
FUNCTION fb_get_pointer(a, b , c, d AS FLOAT(), rtn AS INTEGER(), ratio AS FLOAT)
DIM i , j AS INTEGER
DIM start_i, start_j , stop_j , stop_i AS INTEGER
DIM rtn1 , rtn2 AS INTEGER
DIM exit_temp , data_temp , flag_temp , exit_temp2 AS INTEGER
DIM abs_a, abs_c AS FLOAT
start_i = start_j = stop_j = stop_i = exit_temp =
data_temp = flag_temp = exit_temp2 =
abs_a =
abs_c =
FOR j = TO 899
abs_c = c(j)
FOR i = TO 1799
abs_a = ABS(a(j) - b(i))
IF abs_a < ratio AND abs_c - d(i) < ratio THEN
IF start_i = THEN start_i = i
IF start_j = THEN start_j = j
IF stop_i < i THEN stop_i = i
IF stop_j < j THEN stop_j = j
data_temp = i
exit_temp = 1
flag_temp = 1
ELSEIF data_temp <> i AND i= 1799 AND exit_temp = AND flag_temp = 1 THEN
exit_temp2 = 1
EXIT_LOOP
ENDIF
NEXT i
exit_temp =
IF exit_temp2 = 1 THEN
EXIT_LOOP
ENDIF
NEXT j
rtn1 = INT((start_j + stop_j) / 2)
rtn2 = INT((start_i + stop_i) / 2)
rtn = rtn1 , rtn2
ENDFUNC
阅读全文