我正在学习python并遇到了this example模拟我以前见过的模型。其中一个功能看起来不必要很长,所以我认为尝试提高效率是一种好习惯。我的尝试,虽然需要更少的代码,大约快1/60。是的,我做了60倍。
我的问题是,我哪里出错了?我已经尝试计算函数的各个部分,但没有看到瓶颈在哪里。
这是原始功能。这是一个人们生活在网格上的模型,他们的幸福取决于他们是否与大多数邻居一样。 (这是Schelling的segregation model。)所以我们给一个人一个x,y坐标,并通过检查他们每个邻居的种族来确定他们的幸福。
def is_unhappy(self, x, y):
race = self.agents[(x,y)]
count_similar = 0
count_different = 0
if x > 0 and y > 0 and (x-1, y-1) not in self.empty_houses:
if self.agents[(x-1, y-1)] == race:
count_similar += 1
else:
count_different += 1
if y > 0 and (x,y-1) not in self.empty_houses:
if self.agents[(x,y-1)] == race:
count_similar += 1
else:
count_different += 1
if x < (self.width-1) and y > 0 and (x+1,y-1) not in self.empty_houses:
if self.agents[(x+1,y-1)] == race:
count_similar += 1
else:
count_different += 1
if x > 0 and (x-1,y) not in self.empty_houses:
if self.agents[(x-1,y)] == race:
count_similar += 1
else:
count_different += 1
if x < (self.width-1) and (x+1,y) not in self.empty_houses:
if self.agents[(x+1,y)] == race:
count_similar += 1
else:
count_different += 1
if x > 0 and y < (self.height-1) and (x-1,y+1) not in self.empty_houses:
if self.agents[(x-1,y+1)] == race:
count_similar += 1
else:
count_different += 1
if x > 0 and y < (self.height-1) and (x,y+1) not in self.empty_houses:
if self.agents[(x,y+1)] == race:
count_similar += 1
else:
count_different += 1
if x < (self.width-1) and y < (self.height-1) and (x+1,y+1) not in self.empty_houses:
if self.agents[(x+1,y+1)] == race:
count_similar += 1
else:
count_different += 1
if (count_similar+count_different) == 0:
return False
else:
return float(count_similar)/(count_similar+count_different) < self.similarity_threshold
这是我的代码,正如我所说,它的速度慢得多。我想通过创建一个“偏移”列表来添加到每个人的坐标以确定可能的邻居的位置,检查这是否是有效位置,然后检查邻居的竞赛,以避免上面的所有if语句。
def is_unhappy2(self, x, y):
thisRace = self.agents[(x,y)]
count_same = 0
count_other = 0
for xo, yo in list(itertools.product([-1,0,1],[-1,0,1])):
if xo==0 and yo==0:
# do nothing for case of no offset
next
else:
# check if there's a neighbor at the offset of (xo, yo)
neighbor = tuple(np.add( (x,y), (xo,yo) ))
if neighbor in self.agents.keys():
if self.agents[neighbor] == thisRace:
count_same += 1
else:
count_other += 1
if count_same+count_other == 0:
return False
else:
return float(count_same) / (count_same + count_other) < self.similarity threshold
(创建该类的其余代码是on the site,其中的示例来自。)
以下是时间安排结果:
%timeit s.is_unhappy2(49,42)
100 loops, best of 3: 5.99 ms per loop
%timeit s.is_unhappy(49,42)
10000 loops, best of 3: 103 µs per loop
我希望有python知识的人可以立即看到我做错了什么而不必深入了解其余代码的细节。你能明白为什么我的代码比原来的差得多吗?
答案 0 :(得分:8)
不要使用Name : ConsoleHost
Version : 4.0
InstanceId : 1a9413a0-cdbb-46a3-bbe3-b3dbc72fad38
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
,只需使用np.add
即可。这应该会更快(在我的小测试中快10倍)。
你也可以......
neighbor = (x+xo, y+yo)
if neighbor in self.agents:
.keys()
list
并且没有空的if-block 结果:
xo or yo
你可以添加
for xo, yo in itertools.product([-1,0,1],[-1,0,1]):
if xo or yo:
neighbor = self.agents.get((x+xo, y+yo))
if neighbor is not None:
if neighbor == thisRace:
count_same += 1
else:
count_other += 1
到类初始值设定项,然后你的函数可以使用那些预先计算的增量:
self.neighbor_deltas = tuple(set(itertools.product([-1,0,1],[-1,0,1])) - {(0, 0)})
恭喜您决定改进该作者可笑的重复代码,顺便说一句。
答案 1 :(得分:3)
罪魁祸首看起来就是这句话:
neighbor = tuple(np.add( (x,y), (xo,yo) ))
将其更改为显示加速:
neighbor = (x + xo, y + yo)