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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'exception': 0.03; 'present,': 0.07; 'try:': 0.07; 'python': 0.09; '"if': 0.09; 'dec': 0.15; '(assuming': 0.16; 'dictionary,': 0.16; 'do!': 0.16; 'expensive,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'missing,': 0.16; 'slow,': 0.16; 'subject:compare': 0.16; 'try/except': 0.16; 'wrote:': 0.17; 'cheap': 0.17; 'thu,': 0.17; 'fairly': 0.21; 'constant': 0.22; 'keyerror:': 0.22; 'keys': 0.22; 'nearly': 0.23; 'so.': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'setting': 0.26; 'message-id:@mail.gmail.com': 0.27; 'catching': 0.29; "d'aprano": 0.29; 'dictionary': 0.29; 'hash': 0.29; 'steven': 0.29; 'expect': 0.31; 'could': 0.32; 'to:addr:python-list': 0.33; '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.220': 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; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; '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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=hpBoUvy6d0+LUk6duAbBYSi1VnE9P3MNgwmJfy9p1sU=; b=GdLyQ4XLlHpEINoRzUgGYbSzWqGF9Jpn2hwmqH5Bg3gLO2JHbtgpRf9FlYonpnTCIf jJ+Fbx+aX4wLET2FZ8R8uKjnJJCKpNj0i/T1ioxbkRLhLCIiZYvkS935+E2ncsb7/E/i HjgMxb+PIcG8VMZrlt6qG6m+glSwe200fzO+jgitHovncEoiY0LmTjL9hC+RfJl5xoE/ 5PGR6GGr2+Q4MFdIuGH2jcbkTjPVAEghh1uW+4f9DakyqFEBQQOO+I209FIJ9cxVtluC IB0610ZkZTX8/Yir+T9GdfgI8Bpfn9kJAKinrfRjjXjiweIF48aUDau943rcTayJ//dH Ot7g== MIME-Version: 1.0 In-Reply-To: <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 6 Dec 2012 23:14:17 +1100 Subject: Re: Confused compare function :) From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354796060 news.xs4all.nl 6974 [2001:888:2000:d::a6]:58176 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34379 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