【发布时间】:2021-10-11 02:30:59
【问题描述】:
我目前正在尝试通过解决一些示例问题来提高我的 Python 技能。其中之一是关于检查网格点是否位于障碍物后面。输出是不在障碍后面的所有网格点的数量。 给定的Hint是计算方向到点的角度,并与障碍物左右两侧的角度进行比较。 我的代码可以工作并给出正确的结果,但是对于大型网格 (1000x1000),它非常慢,所以我想知道是否有办法让代码更快。
花费时间最长的部分是检查一个点是否在障碍物后面,所以我在这里只包含这部分代码。如果您还需要其余代码,我很乐意将其包含在内:)
import math
import numpy as np
def CheckIfObstacle(point, Obstacle):
for obs in Obstacle:
# condition for a point being behind a barrier that is on positive side of Grid
if obs[0]>0 and point[0] >= obs[0] and (obs[1] >= point[1] >= obs[2]):
return True
# condition for a point being behind a barrier that is on negative side of Grid
elif obs[0]<0 and point[0] <=obs[0] and (obs[1]>= point[1]>= obs[2]):
return True
return False
Obstacle = [] #[(y1,angle_big1,angle_small1),(y2,angle_big2,angle_small2),...]
for i in range(2,nObs+2):
some code that puts data in Obstacle
Grid = calcGrid(S) # [(y1,angle1),(y2,angle2),...]
count = 0
p=0
for point in Grid:
if p%10000==0:
print(round(p/len(Grid)*100,3),'%')
p+=1
if CheckIfObstacle(point, Obstacle) ==False:
count +=1
print(count)
这是我所有版本的禁食版本。我认为 1000x1000 网格大约需要 15 分钟,但现在我有一个更大的网格,它运行了一个小时并且在 5% 左右。如果有人对如何改进代码有任何想法,我会很高兴收到一些反馈。
提前致谢!
【问题讨论】:
标签: python for-loop optimization