优化两个简单的嵌套循环
我一直在尝试优化以下两个嵌套循环:
def startbars(query_name, commodity_name):
global h_list
nc, s, h_list = [], {}, {}
query = """ SELECT wbcode, Year, """+query_name+"""
FROM innovotable WHERE commodity='"""+commodity_name+"""' and
"""+query_name+""" != 'NULL' """
rows = cursor.execute(query)
for row in rows:
n = float(row[2])
s[str(row[0])+str(row[1])] = n
nc.append(n)
for iso in result:
try:
for an_year in xrange(1961, 2031, 1):
skey = iso+str(an_year)
h_list[skey] = 8.0 / max(nc) * s[skey]
except:
pass
有什么想法吗?谢谢。
I have been trying to optimize the two following nested loops:
def startbars(query_name, commodity_name):
global h_list
nc, s, h_list = [], {}, {}
query = """ SELECT wbcode, Year, """+query_name+"""
FROM innovotable WHERE commodity='"""+commodity_name+"""' and
"""+query_name+""" != 'NULL' """
rows = cursor.execute(query)
for row in rows:
n = float(row[2])
s[str(row[0])+str(row[1])] = n
nc.append(n)
for iso in result:
try:
for an_year in xrange(1961, 2031, 1):
skey = iso+str(an_year)
h_list[skey] = 8.0 / max(nc) * s[skey]
except:
pass
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码不完整,这使得很难提供好的建议,但是:
此外,您还需要知道当前代码有多慢,以及您需要它有多快,否则您的优化可能会错位。
你的数据结构全乱了。也许列出的内容会更快:
或者将所有检查移到第一个循环中:
Your code isn't complete which makes it hard to give good advice but:
Also you need to know how slow the current code is, and how fast you need it to be, otherwise your optimisations maybe misplaced.
Your datastructures are all messed up. Maybe something list this would be faster:
Or moving all checks into the first loop: