我目前需要使用 Python 2.7 比较包含 MAC 地址的字符串(例如“11:22:33:AA:BB:CC”)。目前,我有一个包含 MAC 地址和我的脚本遍历该集合,将每个新 MAC 地址与列表中的地址进行比较。这很好用,但随着集合的增长,脚本会大大减慢速度。只有 100 个左右,您会注意到巨大的差异。
有人对加快这个过程有什么建议吗?将它们存储在一个集合中是进行比较的最佳方式,还是将它们存储在 CSV/DB 中更好?
代码示例...
def Detect(p):
stamgmtstypes = (0,2,4)
if p.haslayer(Dot11):
if p.type == 0 and p.subtype in stamgmtstypes:
if p.addr2 not in observedclients:
# This is the set with location_mutex:
detection = p.addr2 + "\t" + str(datetime.now())
print type(p.addr2)
print detection, last_location
observedclients.append(p.addr2)
请您参考如下方法:
首先,您需要profile your code了解瓶颈到底在哪里......
此外,作为一般性建议,请考虑使用 psyco,尽管 there are a few times when psyco doesn't help
一旦发现瓶颈,cython可能有用,但您需要确保在 cython 源代码中声明所有变量。
