Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1099085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:40:45+00:00 2026-05-17T00:40:45+00:00

I have been trying to optimize the two following nested loops: def startbars(query_name, commodity_name):

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:40:46+00:00Added an answer on May 17, 2026 at 12:40 am

    Your code isn’t complete which makes it hard to give good advice but:

    1. Inner loop doesn’t depend on outer-loop, so pull it out of the outer loop.
    2. max(nc) is a constant after first loop, so pull it out of the loops.

    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:

    def startbars(query_name, commodity_name):
    
        assert query_name in INNOVOTABLE_FIELD_NAMES
    
        ## TODO: Replace with proper SQL query
        query = """ SELECT wbcode, Year, """+query_name+""" 
                 FROM innovotable WHERE commodity='"""+commodity_name+"""' and
    
                 """+query_name+""" != 'NULL' """
        rows = cursor.execute(query)
    
        mapYearToWbcodeToField = {}
        nc = []
        global h_list
        h_list = {}
    
        for row in rows:
            n = float(row[2])
            wbCodeToField = mapYearToWbcodeToField.setdefault(int(row[1]),{})
            wbCodeToField[str(row[0])] = n
            nc.append(n)
    
        constant = 8.0 / max(nc)
    
    
        for (an_year,wbCodeToField) in mapYearToWbcodeToField.iteritems():
            if an_year < 1961 or an_year > 2031:
                continue
    
            for (wbCode,value) in wbCodeToField.iteritems():
                if wbCode not in result:
                    continue
    
                skey = wbCode+str(an_year)
                h_list[skey] = constant * value
    

    Or moving all checks into the first loop:

    def startbars(query_name, commodity_name):
    
        assert query_name in INNOVOTABLE_FIELD_NAMES
    
        ## TODO: Replace with proper SQL query
        query = """ SELECT wbcode, Year, """+query_name+""" 
                 FROM innovotable WHERE commodity='"""+commodity_name+"""' and
    
                 """+query_name+""" != 'NULL' """
        rows = cursor.execute(query)
    
        data = []
        maxField = None
    
        for row in rows:
            an_year = int(row[1])
            if an_year < 1961 or an_year > 2031:
                continue
    
            wbCode = str(row[0])
            if wbCode not in result:
                continue
    
            n = float(row[2])
    
            data.append((wbCode+str(an_year),n))
            if maxField is None or n > maxField:
                maxField = n
    
        constant = 8.0 / maxField
    
        global h_list
        h_list = {}
    
        for (skey,n) in data:
            h_list[skey] = constant * n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to optimize this code: def spectra(mE, a): pdf = lambda
Recently I have been trying to optimize my tables, mainly because I've learned alot
I've been trying to optimize a numeric program of mine, and have run into
I have been working on this for 24 hours now, trying to optimize it.
I have been trying to optimize some code which handles raw pixel data. Currently
I have been trying to optimize an ASP.NET C# application for a few days
I have been working in trying to optimize a webservice that is required to
I'm trying to optimize query performance and have had to resort to using optimizer
I have been trying to organise my views to optimize load times by having
Have been trying to encrypt an xml file to a string so that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.