App Speed Improvements

Hello! I’m so excited to participate in this community and get some help with what should be ( :crossed_fingers: ) a simple fix!

I’ve just deployed an application here using Heroku. Performance was excellent when I was just running in the terminal, but as soon as I started to convert into a web app, it tanked. The app will usually load well on the first try, then takes a very, very long time if I try to change any variables and get a new result. I’m almost certain someone with more CS/algorithms/data structures knowledge than I have will be able to crack the code on this one, but it could be something else, so I came here first!

Some relevant info:

  • Python v. 3.7.4
  • Streamlit v. 0.83.0
  • PipEnv for environment
  • Google Chrome v. 91.0.4472.101
  • Windows 10

Below are the two functions I believe could be slowing everything down.
A few notes:
costs is a list of floats
The app inputs correspond to add, multiply, and payout_goal, in that order

def charge(crystal_price, add, multiply):
    base = crystal_price + add
    cost = base * multiply
    decimals = str(round(cost, 2))[2:]
    if decimals == '.82':
        new_cost = cost + .06
    elif decimals == '.32':
        new_cost = cost + .56
    else:
        new_cost = cost
    while new_cost not in charges:
        new_cost += 1
    return round(new_cost, 2)


def wire_wraps(payout_goal, costs):
    total_cost = 0
    actual_payout = 0
    stones = []
    while actual_payout < payout_goal:
        stone = random.choice(costs)
        stones.append(stone)
        total_cost += stone / 2
        actual_payout += (charge(stone, add, multiply) / 2)
    return total_cost, actual_payout, stones

Thank you in advance!

I’m not an expert, but here is my shot at it.

First, It is not completely clear what charges is in the line while new_cost not in charges:.
Is it a global variable?

If your functions are taking to long it is one of two reason, probably a combination of both…

  1. Both functions include while loops. Which are susceptible to getting locked into infinite loops. Perhaps there is a need to check their logic.

  2. Your second function. wire_wraps() not only has a while loops. It also calls the function charge() which also has it’s own while loops. This is is probably the biggest resource overhead in your code.