Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Kill Ugly Processor Architectures -- Karl Lehenbauer


devel / comp.lang.python / Can you help me with this memoization simple example?

SubjectAuthor
o Can you help me with this memoization simple example?marc nicole

1
Can you help me with this memoization simple example?

<mailman.36.1711843576.3468.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25342&group=comp.lang.python#25342

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: mk1853...@gmail.com (marc nicole)
Newsgroups: comp.lang.python
Subject: Can you help me with this memoization simple example?
Date: Sun, 31 Mar 2024 01:09:43 +0100
Lines: 64
Message-ID: <mailman.36.1711843576.3468.python-list@python.org>
References: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de I0Xi/G6nsazfsA9WyAyd2gpbcEmYDcC7Opo4/uEzwInw==
Cancel-Lock: sha1:5uGvZ6/BAIYsMLRiaEZm0g39T5Q= sha256:Rj9gwLhkHbwSLSVlVIpc+XrzGmY8dYsJru0gxNfb39A=
Return-Path: <mk1853387@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=L5Jj9Lju;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.023
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'def': 0.04; 'e.g.': 0.07;
'elif': 0.09; 'else:': 0.09; 'ok,': 0.09; 'threshold': 0.09;
'import': 0.15; 'below).': 0.16; 'dict': 0.16; 'key,': 0.16;
'subject:simple': 0.16; 'values': 0.17; 'to:addr:python-list':
0.20; 'to:no real name:2**1': 0.22; 'code': 0.23; 'cannot': 0.25;
'creating': 0.27; 'function': 0.27; 'sfxlen:2': 0.31; 'anybody':
0.32; 'elements': 0.32; 'retrieve': 0.32; 'subject:Can': 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35; 'using':
0.37; 'use': 0.39; 'want': 0.40; 'tell': 0.60; 'here': 0.62;
'below:': 0.63; 'great': 0.63; 'key': 0.64; 'subject:this': 0.67;
'attached': 0.68; 'adds': 0.69; 'addition,': 0.70; 'subject:you':
0.84; '500000': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1711843573; x=1712448373; darn=python.org;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=nZNj43G5JQoNmIAeogpiecQYCHqX+gUfoUyRZzvdZZI=;
b=L5Jj9LjuAZJUNfX3QWKXUn6xSwZZXN/Xb698LLl9Wiu4tL5+4zp+uG/0Xk2CzKiTx9
BHcd8LyZHtIGRUoD9YHIqpoB6tqQ2PRWnGofTO3drJBjx+8tpt9wSBDwyfYzRYekADYi
ccocjN7yMVAyNjdGukHOwsS5VADIu8czvBpn6ftJmYkquFRmx4lL2SPB/89gIjba3kmq
V64lTiWvPdKYAszOYzfbPwFyxXc20XhJLU5efPsgfhB4fRkfHSkV/M4pr33CTVG3u1XA
d1BH18m6HBbX36l2crQ1KctxcF6hGTf4gc1a5ysg41deOrz7Xb3EsmspkY0w7Ig2J2Kx
jcKw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1711843573; x=1712448373;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=nZNj43G5JQoNmIAeogpiecQYCHqX+gUfoUyRZzvdZZI=;
b=WuTdIhS0+mv0rLXidOydizymALxI+d9xP/VnqT1a+1Wf3NZuTGYEkuhEGdU7BqKnCW
ewRONDHTeeBNb+fLkp/rORk/WD+/dR5Rc1ybxbM742Ie7Lo6SDxhK9S5TDCkkX0qp8zN
mGKKcI8pA1vKeN5jxOI+Y9650FWNl760DoEF1dCok6TCWPLfKdESHijQaYK9WZOmCFUk
A77sqCGbD6sR6wxfEewMJZLXh/Vg73ll5ncunRqJzqMTpF50NjXqSpkKQ9z0DEwaYn1U
wlRp05e64v8xZ9l0y/P2NW1TzBa5N06ZuONG07mpgEJ2YHwLsFUovxtTD0wM11Cm3Yor
OUjw==
X-Forwarded-Encrypted: i=1;
AJvYcCXj97fAo+FSDlrSC8TSfYPWwucaHGvGUREL1Ak0GxzghxOknZQnt3MYSYNaPEtHG7Fs9ksfRBCdlDolKtxO+g1qoeukeHeC
X-Gm-Message-State: AOJu0YymI4Q/FEVJ0Yp5/Eje6JS06mW2OZKOAkjLx0ZSVpGpirEVZZTM
1qEYghjj9Io2gBtRe4LmwY5mkYmzgYCaWJERZUhC/NwxY5/5P4v+IvH8evFiqlG9a0Xl2agKp5P
f7/7JQeINOuT+3uYamoFgsimeouxpKxTBY8A=
X-Google-Smtp-Source: AGHT+IEsw0vGAu3raWRI/SCIESjnl/CK6aZUwaVXEmhxSff+XKF4hsW98eQ/LfwphbmM/yOcyXEwDKVv9HtmJPkDlII=
X-Received: by 2002:a17:90a:4216:b0:2a0:33c2:997e with SMTP id
o22-20020a17090a421600b002a033c2997emr5120629pjg.41.1711843573127; Sat, 30
Mar 2024 17:06:13 -0700 (PDT)
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com>
 by: marc nicole - Sun, 31 Mar 2024 00:09 UTC

I am creating a memoization example with a function that adds up / averages
the elements of an array and compares it with the cached ones to retrieve
them in case they are already stored.

In addition, I want to store only if the result of the function differs
considerably (passes a threshold e.g. 500000 below).

I created an example using a decorator to do so, the results using the
decorator is slightly faster than without the memoization which is OK, but
is the logic of the decorator correct ? anybody can tell me ?

My code is attached below:

import time

def memoize(f):
cache = {}

def g(*args):
if args[1] == "avg":
sum_key_arr = sum(list(args[0])) / len(list(args[0]))
elif args[1] == "sum":
sum_key_arr = sum(list(args[0]))
if sum_key_arr not in cache:
for (
key,
value,
) in (
cache.items()
): # key in dict cannot be an array so I use the sum of the
array as the key
if (
abs(sum_key_arr - key) <= 500000
): # threshold is great here so that all values are
approximated!
# print('approximated')
return cache[key]
else:
# print('not approximated')
cache[sum_key_arr] = f(args[0], args[1])
return cache[sum_key_arr]

return g

@memoize
def aggregate(dict_list_arr, operation):
if operation == "avg":
return sum(list(dict_list_arr)) / len(list(dict_list_arr))
if operation == "sum":
return sum(list(dict_list_arr))
return None

t = time.time()
for i in range(200, 15000):
res = aggregate(list(range(i)), "avg")

elapsed = time.time() - t
print(res)
print(elapsed)

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor