Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'diff': 0.05; 'matches': 0.07; 'subject:code': 0.07; 'tool,': 0.07; 'subject:help': 0.07; 'dict': 0.09; 'matched': 0.09; 'def': 0.10; 'date_time': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'row': 0.16; 'run.': 0.16; 'subject:making': 0.16; 'tool.': 0.16; 'tool:': 0.16; 'tup': 0.16; 'wrote:': 0.17; 'string,': 0.17; 'typical': 0.17; 'tuples': 0.22; 'elements': 0.23; 'matching': 0.23; 'sets': 0.23; 'specified': 0.23; 'this:': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; '(which': 0.26; 'select': 0.26; 'skip:m 30': 0.26; 'run': 0.28; 'record': 0.28; 'received:192.168.1.3': 0.29; 'time;': 0.29; "skip:' 10": 0.30; 'e.g.': 0.30; 'code': 0.31; 'could': 0.32; 'message.': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'themselves': 0.33; 'list': 0.35; 'false': 0.35; 'doing': 0.35; 'really': 0.36; 'tool': 0.36; 'but': 0.36; 'subject:with': 0.36; 'too': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'time,': 0.62; 'times': 0.63; 'email addr:gmail.com': 0.63; 'within': 0.64; 'here': 0.65; 'hours': 0.66; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=cccTb7WecioA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=IlQBlDHxBBYA:10 a=pGLkceISAAAA:8 a=1J-_NsaLA3LwLxBKd_wA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 21 Dec 2012 02:08:42 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: help with making my code more efficient References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356055729 news.xs4all.nl 6886 [2001:888:2000:d::a6]:60499 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35260 On 2012-12-21 00:19, Larry.Martell@gmail.com wrote: > I have a list of tuples that contains a tool_id, a time, and a message. I want to select from this list all the elements where the message matches some string, and all the other elements where the time is within some diff of any matching message for that tool. > > Here is how I am currently doing this: > > # record time for each message matching the specified message for each tool > messageTimes = {} > for row in cdata: # tool, time, message > if self.message in row[2]: > messageTimes[row[0], row[1]] = 1 > It looks like 'messageTimes' is really a set of tool/time pairs. You could make it a dict of sets of time; in other words, a set of times for each tool: messageTimes = defaultdict(set) for row in cdata: # tool, time, message if self.message in row[2]: messageTimes[row[0]].add(row[1]) > # now pull out each message that is within the time diff for each matched message > # as well as the matched messages themselves > > def determine(tup): > if self.message in tup[2]: return True # matched message > > for (tool, date_time) in messageTimes: > if tool == tup[0]: > if abs(date_time-tup[1]) <= tdiff: > return True > > return False > def determine(tup): if self.message in tup[2]: return True # matched message # Scan through the times for the tool given by tup[0]. for date_time in messageTimes[tup[0]]: if abs(date_time - tup[1]) <= tdiff: return True return False > cdata[:] = [tup for tup in cdata if determine(tup)] > > This code works, but it takes way too long to run - e.g. when cdata has 600,000 elements (which is typical for my app) it takes 2 hours for this to run. > > Can anyone give me some suggestions on speeding this up? >