Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.tele2net.at!newsfeed.utanet.at!feeder1.cambriumusenet.nl!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'python,': 0.02; '(of': 0.07; 'compiler': 0.07; 'expressions': 0.07; 'integers': 0.09; 'used.': 0.09; 'cc:addr:python-list': 0.11; '2),': 0.16; 'bitwise': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer,': 0.16; 'shifts': 0.16; 'why,': 0.16; 'work.)': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'normally': 0.19; 'addition,': 0.20; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'obscure': 0.31; 'operators': 0.31; 'overhead': 0.31; 'steven': 0.31; 'probably': 0.32; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'possible': 0.36; 'should': 0.36; 'turn': 0.37; 'performance': 0.37; 'being': 0.38; 'pm,': 0.38; 'rather': 0.38; 'either': 0.39; 'even': 0.60; 'most': 0.60; "you're": 0.61; "you've": 0.63; 'real': 0.63; 'today': 0.64; 'become': 0.64; 'benefit': 0.68; 'mar': 0.68; '*really*': 0.84; 'expressive': 0.84; 'faster.': 0.84; 'hardly': 0.84; 'multiplying': 0.84; 'worthwhile.': 0.84; 'to:none': 0.92 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:cc :content-type; bh=lRLILDI1fQFDa4064DlnznXpJWpBBKD1i+xdzoIOl8M=; b=Bd5FMGIYNz43z4Hickad//QiGYMUeGt1mTIEDLSccrvi4Omd+Iw7pOVvhx80dWhkqv S885kC1Mae0dMCizwjXYmRhwrPUviCMJmJTuztjCOmkLC0HxPehoE0ezG4V5HFYlMk/v KKVvAwcOTGD9FnBKBCieSGJ8IXRGRa8tyrQYM/sw+q1AiCSBZWkUaJs1nTKFV2zIWpD0 8VfVSUDXulqi8xyjMA2s8JaBbFlZe79Nvlm5YUl+XwLlAQn7jj6XEN6Bd8uUFpTykBRZ ffL8hzw1O51bb+dM8OKF+NvciZqTcaxQii+d61TwFg7Ko97TplsCkVpuNp8/pOYaPDUl 6aAQ== MIME-Version: 1.0 X-Received: by 10.66.11.66 with SMTP id o2mr3840001pab.142.1393910643220; Mon, 03 Mar 2014 21:24:03 -0800 (PST) In-Reply-To: <53155c15$0$2923$c3e8da3$76491128@news.astraweb.com> References: <53144e8d$0$2149$426a74cc@news.free.fr> <1d1dfa1b-b715-4d8f-9c12-f0d3dc1a22c9@googlegroups.com> <85ppm3httu.fsf@benfinney.id.au> <20140303155112.46e34ff8@bigbox.christie.dr> <87siqy7whs.fsf@elektro.pacujo.net> <53155c15$0$2923$c3e8da3$76491128@news.astraweb.com> Date: Tue, 4 Mar 2014 16:24:03 +1100 Subject: Re: Reference From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393910647 news.xs4all.nl 2880 [2001:888:2000:d::a6]:45720 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67644 On Tue, Mar 4, 2014 at 3:52 PM, Steven D'Aprano wrote: > This is why, unless performance is *really* critical, one should normally > write x*2 when multiplying x by 2 rather than x >> 1. (And in Python, the > overhead means that there is no real performance benefit to using bit > shifts instead of multiplication or division.) In most C compilers today (C being where x << 1 would be used rather than x * 2), the expressions would be equivalent, so you can still express it as x * 2 and let the compiler turn that into a bit shift. (And it's possible that "x * 5" will become "x << 2 + x", too.) Definitely go for the expressive version unless you've actually tested that the obscure version is faster. (Of course, that doesn't mean the bit-shift operators should never be used. If you're trying to pack three tiny integers into a single larger integer, you probably want bit shifts and bitwise or, not multiplication and addition, even though either would work.) Code should look like its intent. Warping it around performance is hardly ever worthwhile. ChrisA