Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34518

Re: Confused compare function :)

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 <maniandram01@gmail.com>
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 <mailman.555.1354796060.29569.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=122.167.90.161; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD
References <CAKhY55PXaMkdnW7qMb-5ConHMcz05Gnkqw4wvyoprBFBd5UFFw@mail.gmail.com> <mailman.538.1354753193.29569.python-list@python.org> <k9p32p$nek$1@dont-email.me> <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <mailman.549.1354783761.29569.python-list@python.org> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <mailman.555.1354796060.29569.python-list@python.org>
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 <maniandram01@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.644.1355027681.29569.python-list@python.org> (permalink)
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

Show key headers only | View raw


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
> 
> <steve+comp.lang.python@pearwood.info> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 01:19 +0100
  Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 00:42 +0000
    Re: Confused compare function :) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-06 13:41 -0500
    Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:55 +0100
  Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 03:22 +0000
    Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 04:32 +0000
      Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 09:49 +0100
        Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 11:47 +0000
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 08:55 -0300
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 14:32 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:47 +1100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 23:14 +1100
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 22:16 +0000
              Re: Confused compare function :) Terry Reedy <tjreedy@udel.edu> - 2012-12-08 02:01 -0500
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-08 18:17 +1100
              Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-08 17:50 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-09 14:22 +1100
                Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-09 07:39 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
          Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-06 13:51 +0000
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 02:55 +0000
              Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-07 16:40 +0000
        Re: Confused compare function :) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-12-06 14:33 +0100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:58 +1100
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 15:21 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 01:28 +1100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 15:22 +0100
          Re: Confused compare function :) Dave Angel <d@davea.name> - 2012-12-06 09:40 -0500
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 11:46 -0300
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 17:16 +0100
          Re: Confused compare function :) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-06 16:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:08 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:10 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:31 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:25 +0100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-07 14:36 +0100
        Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:24 +0000
      Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 20:34 +1100
      Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:25 +0000

csiph-web