Skip to content

Commit

Permalink
Swap for-loops to increase performance
Browse files Browse the repository at this point in the history
This method is faster if the first for-loop contains fewer items.
Since the function is called with, typically, `dist2` having less items, let's loop over `dist2` first. This makes the entire program 10% faster.
  • Loading branch information
spinerak committed Oct 14, 2024
1 parent ea8f827 commit a3351f2
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions worlds/yachtdice/Rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def dice_simulation_strings(categories, num_dice, num_rolls, fixed_mult, step_mu
# defaultdict is a dict where you don't need to check if an id is present, you can just use += (lot faster)
def add_distributions(dist1, dist2):
combined_dist = defaultdict(float)
for val1, prob1 in dist1.items():
for val2, prob2 in dist2.items():
for val2, prob2 in dist2.items():
for val1, prob1 in dist1.items():
combined_dist[val1 + val2] += prob1 * prob2
return dict(combined_dist)

Expand Down

0 comments on commit a3351f2

Please sign in to comment.