Path: csiph.com!usenet.pasdenom.info!news.albasani.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; 'exception': 0.03; 'present,': 0.07; 'try:': 0.07; 'python': 0.09; '"if': 0.09; 'to:addr:comp.lang.python': 0.09; 'bug': 0.10; 'cc:addr:python- list': 0.10; 'dec': 0.15; '(assuming': 0.16; 'dictionary,': 0.16; 'do!': 0.16; 'expensive,': 0.16; 'hashes': 0.16; 'missing,': 0.16; 'slow,': 0.16; 'subject:compare': 0.16; 'try/except': 0.16; 'useless.': 0.16; 'wrote:': 0.17; 'cheap': 0.17; 'thu,': 0.17; 'saying': 0.18; 'fairly': 0.21; 'constant': 0.22; 'keyerror:': 0.22; 'keys': 0.22; 'cc:2**0': 0.23; 'nearly': 0.23; 'cc:no real name:2**0': 0.24; 'so.': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'chris': 0.28; 'catching': 0.29; "d'aprano": 0.29; 'dictionary': 0.29; 'hash': 0.29; 'steven': 0.29; 'becomes': 0.30; 'thursday,': 0.30; 'expect': 0.31; 'december': 0.32; 'could': 0.32; 'membership': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'or,': 0.34; 'faster': 0.35; 'path': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; 'wanted': 0.36; 'depends': 0.36; 'execute': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'received:209.85.214': 0.39; 'takes': 0.39; 'first': 0.61; 'safe': 0.63; 'subject': 0.66; 'collision': 0.84; 'cost,': 0.84; 'fast,': 0.84; 'occasion': 0.84; 'ridiculously': 0.84; 'faster.': 0.91 Newsgroups: comp.lang.python Date: Sat, 8 Dec 2012 19:07:50 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.167.90.161; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 122.167.90.161 MIME-Version: 1.0 Subject: Re: Confused compare function :) From: Ramchandra Apte To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355027681 news.xs4all.nl 6902 [2001:888:2000:d::a6]:54445 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34518 On Thursday, 6 December 2012 17:44:17 UTC+5:30, Chris Angelico wrote: > On Thu, Dec 6, 2012 at 10:47 PM, Steven D'Aprano > > wrote: > > > Not so. Which one is faster will depend on how often you expect to fail. > > > If the keys are nearly always present, then: > > > > > > try: > > > do_stuff(mydict[k]) > > > except KeyError: > > > pass > > > > > > will be faster. Setting up a try block is very fast, about as fast as > > > "pass", and faster than "if k in mydict". > > > > > > But if the key is often missing, then catching the exception will be > > > slow, and the "if k in mydict" version may be faster. It depends on how > > > often the key is missing. > > > > > > > Setting up the try/except is a constant time cost, while the > > duplicated search for k inside the dictionary might depend on various > > other factors. In the specific case of a Python dictionary, the > > membership check is fairly cheap (assuming you're not the subject of a > > hash collision attack - Py3.3 makes that a safe assumption), but if > > you were about to execute a program and wanted to first find out if it > > existed, that extra check could be ridiculously expensive, eg if the > > path takes you on a network drive - or, worse, on multiple network > > drives, which I have had occasion to do! > > > > ChrisA Not really. I remember a bug saying that only 256 hashes were required of known texts and then the randomization becomes useless.