Deutsch English Français Italiano |
<mailman.37.1711845737.3468.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB <python@mrabarnett.plus.com> Newsgroups: comp.lang.python Subject: Re: Can you help me with this memoization simple example? Date: Sun, 31 Mar 2024 00:39:09 +0000 Lines: 77 Message-ID: <mailman.37.1711845737.3468.python-list@python.org> References: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com> <01e1e4ee-e770-4f69-8dfd-ba8b7bb0a6ae@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de lxITpg1CPsO/y9xJPfT27QrVHZHHUP+ICbppnBuOlIcg== Cancel-Lock: sha1:PQSEB7j+YHbSprrxM4EmFDQ1kac= sha256:+yQ6sllL+e9ffmwtvXpvXj9ivpd/o1olVAbr5fXqWlw= Return-Path: <python@mrabarnett.plus.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=plus.com header.i=@plus.com header.b=MtbB4WWd; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'e.g.': 0.07; 'elif': 0.09; 'else:': 0.09; 'from:addr:python': 0.09; 'ok,': 0.09; 'received:192.168.1.64': 0.09; 'threshold': 0.09; 'import': 0.15; 'args[0]': 0.16; 'below).': 0.16; 'dict': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'iterate': 0.16; 'key,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:plus.net': 0.16; 'subject:simple': 0.16; 'tuple': 0.16; 'wrote:': 0.16; 'values': 0.17; "can't": 0.17; 'to:addr :python-list': 0.20; 'code': 0.23; 'list,': 0.24; 'cannot': 0.25; 'creating': 0.27; 'function': 0.27; 'header:User-Agent:1': 0.30; 'anybody': 0.32; 'elements': 0.32; 'python-list': 0.32; 'retrieve': 0.32; 'subject:Can': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'using': 0.37; 'received:192.168': 0.37; 'list': 0.39; 'use': 0.39; 'want': 0.40; 'tell': 0.60; 'received:212': 0.62; 'here': 0.62; 'below:': 0.63; 'great': 0.63; 'key': 0.64; 'let': 0.66; '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=plus.com; s=042019; t=1711845550; bh=ceQGO1xNW3bDkIeCC9bu1cXhDZUoVkvRWWK2glQ725I=; h=Date:Subject:To:References:From:In-Reply-To; b=MtbB4WWd6yJctq5ZRSiiNKPMadP2fX5LqXxQylaYwa3B4QT7TKr/x0Q2KF1gPpuOl LtPOTLGxnCobsp3EOXu9APm6NkmKEK1Gcug2nuhZtBL60aFzAYSGGR+yX9/b+R8qHI L93O394U+o7KsZrfqMsiZuJGse3qq5pQoyEcRwdXypoD14Oa6oB/ADtreOZx7ai8pj /o4afY+FT1GNJ7irBMOW/V2+Tu47qjY1XRsMrIum5RJ3WgyhFqdPp2gznElh+I2/Ku dGmtJLDIdmh1nzGEX/WkHZ+xh0dykrIJ094Ks6TcWPGzpftKyNIwSJUOHHVDPusuit wBZ1a5O83J0Vw== X-Clacks-Overhead: "GNU Terry Pratchett" X-CM-Score: 0.00 X-CNFS-Analysis: v=2.4 cv=TuZFhyXh c=1 sm=1 tr=0 ts=6608b0ae a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=OC_A1QDR1020s07x:21 a=IkcTkHD0fZMA:10 a=86f44TcmWWceCtWXfKsA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla Thunderbird Content-Language: en-GB In-Reply-To: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com> X-CMAE-Envelope: MS4xfJU6TTRKbdOWobYWalTw+6Yyp4WnHVBUzo1wXlMH6yg/z0pVALAc4bzN5ZnyeKXkeZ5PuOf5jNr/3rGYv0AVD0E/DmfXRGZEWRQMDyLz3m2vsFmkCeMP o2Y0zSjnxkSyResZpAy1Eh47iDBYRT13GLMQdFdt5QOpMauOXeVxtXEns9px7OOz8oi1YY3/Dn8sm+ls4TuD1YbNxbgEFgr1cNE= 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: <01e1e4ee-e770-4f69-8dfd-ba8b7bb0a6ae@mrabarnett.plus.com> X-Mailman-Original-References: <CAGJtH9S0o_S_bJvYWRSYBvymzF3p1Y-hcG_gMc1UjCSEaiB2TQ@mail.gmail.com> Bytes: 6699 On 2024-03-31 00:09, marc nicole via Python-list wrote: > 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])) 'list' will iterate over args[0] to make a list, and 'sum' will iterate over that list. It would be simpler to just let 'sum' iterate over 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 You can't use a list as a key, but you can use a tuple as a key, provided that the elements of the tuple are also immutable. > 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)