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.
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: