From 3da552d1d3637f6e187249311d3b46d0619158fb Mon Sep 17 00:00:00 2001 From: spinerak Date: Wed, 5 Jun 2024 23:01:02 +0200 Subject: [PATCH] Improve performance of add_distributions --- worlds/yachtdice/Rules.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/worlds/yachtdice/Rules.py b/worlds/yachtdice/Rules.py index 5a6c13e5a4..9eef89ba72 100644 --- a/worlds/yachtdice/Rules.py +++ b/worlds/yachtdice/Rules.py @@ -2,6 +2,7 @@ from ..generic.Rules import set_rule from BaseClasses import MultiWorld from .YachtWeights import yacht_weights import math +from collections import defaultdict category_mappings = { "Category Ones": "Ones", @@ -130,16 +131,14 @@ def diceSimulationStrings(categories, nbDice, nbRolls, fixed_mult, step_mult, di #function to add two discrete distribution. def add_distributions(dist1, dist2): - combined_dist = {} + combined_dist = defaultdict(float) for val1, prob1 in dist1.items(): for val2, prob2 in dist2.items(): - if int(val1 + val2) in combined_dist.keys(): - combined_dist[int(val1 + val2)] += prob1 * prob2 - else: - combined_dist[int(val1 + val2)] = prob1 * prob2 - return combined_dist + combined_dist[val1 + val2] += prob1 * prob2 + return dict(combined_dist) #function to take the maximum of 'times' i.i.d. dist1. + #I've tried using defaultdict but this made it slower. def max_dist(dist1, mults): new_dist = {0: 1} for mult in mults: