From b75d7fa34865bf16b826d9a747b346ac944529ee Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 14 Dec 2010 12:22:17 +0000 Subject: [PATCH] minor cache based optimization --- lib/core/common.py | 17 ++++++++++++----- lib/core/option.py | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index ddc413f80..413f0b8c7 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -1290,13 +1290,20 @@ def stdev(values): if not values or len(values) < 2: return None - summa = 0.0 - avg = average(values) + key = (values[0], values[-1], len(values)) - for value in values: - summa += pow(value - avg, 2) + if key in kb.cache.stdev: + return kb.cache.stdev[key] + else: + summa = 0.0 + avg = average(values) - return sqrt(summa/(len(values) - 1)) + for value in values: + summa += pow(value - avg, 2) + + retVal = sqrt(summa/(len(values) - 1)) + kb.cache.stdev[key] = retVal + return retVal def average(values): """ diff --git a/lib/core/option.py b/lib/core/option.py index dc7bddd64..1861311f3 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -1125,6 +1125,7 @@ def __setKnowledgeBaseAttributes(): kb.cache = advancedDict() kb.cache.content = {} kb.cache.regex = {} + kb.cache.stdev = {} kb.commonOutputs = None