程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Optimization of multi tier for loop in Python

編輯:Python
The phenomenon and background of the problem

Here is a three story for loop , The running speed needs to be optimized

for i in range(len(points_xy)): weight_t=1 x_pos = int((points_xy[i][0] - x_min2) / abs(x_res2)) y_pos = int((y_max2 - points_xy[i][1]) / abs(x_res2)) # First search the square area centered on the point , Then search the circle inside  y_len_min = (y_pos - radius_pixel_width - 1) if (y_pos - radius_pixel_width - 1) > 0 else 0 y_len_max = (y_pos + radius_pixel_width + 1) if (y_pos + radius_pixel_width + 1) < row else row x_len_min = (x_pos - radius_pixel_width - 1) if (x_pos - radius_pixel_width - 1) > 0 else 0 x_len_max = (x_pos + radius_pixel_width + 1) if (x_pos + radius_pixel_width + 1) < col else col for y in range(y_len_min, y_len_max): for x in range(x_len_min, x_len_max): distance = float(((x - x_pos) ** 2 + (y - y_pos) ** 2) ** 0.5) dis=distance*30 if (distance < radius_pixel_width and distance > 0): value = raster_data[y_pos][x_pos] D_value=((radius_pixel_width - distance + 1)/(radius_pixel_width**2))*weight_t if (result_data[y][x] != -999.0): result_data[y][x] += D_value else: result_data[y][x] = D_value elif(distance==0 and result_data[y][x] != -999.0): result_data[y][x]+=(1*weight_t) elif(distance==0 and result_data[y][x] == -999.0): result_data[y][x] = 1*weight_t
Operation results and error reporting contents
My solution ideas and tried methods

Add at the beginning jit, But the result told me that point_xy yes list It doesn't seem to work , I have put the jit Put it in for Internal loop , The result is that the program keeps running . Ask how to optimize

for i in range(len(points_xy)): x_pos = int((points_xy[i][0] - x_min) / abs(x_res)) y_pos = int((y_max - points_xy[i][1]) / abs(x_res)) # First search the square area centered on the point , Then search the circle inside  y_len_min = (y_pos - radius_pixel_width - 1) if (y_pos - radius_pixel_width - 1) > 0 else 0 y_len_max = (y_pos + radius_pixel_width + 1) if (y_pos + radius_pixel_width + 1) < rows else rows x_len_min = (x_pos - radius_pixel_width - 1) if (x_pos - radius_pixel_width - 1) > 0 else 0 x_len_max = (x_pos + radius_pixel_width + 1) if (x_pos + radius_pixel_width + 1) < cols else cols @jit def keral_map(y_len_min, y_len_max, x_len_min, x_len_max, x_pos, y_pos, radius_pixel_width, result_data): weight_t = 1 for y in range(y_len_min, y_len_max): for x in range(x_len_min, x_len_max): distance = float(((x - x_pos) ** 2 + (y - y_pos) ** 2) ** 0.5) # Judge within the radius  if (distance < radius_pixel_width and distance > 0): D_value = ((radius_pixel_width - distance + 1) / (radius_pixel_width ** 2)) * weight_t if (result_data[y][x] != -999.0): result_data[y][x] += D_value else: result_data[y][x] = D_value elif (distance == 0 and result_data[y][x] != -999.0): result_data[y][x] += (1 * weight_t) elif (distance == 0 and result_data[y][x] == -999.0): result_data[y][x] = 1 * weight_t return result_data keral_map(y_len_min, y_len_max, x_len_min, x_len_max, x_pos, y_pos, radius_pixel_width, result_data)
What I want to achieve

The current speed is probably 25 About seconds (6 10000 points ), I wonder if I could hurry up


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved