Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!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.033 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'binary': 0.05; 'float': 0.05; 'python': 0.09; 'integers': 0.09; 'modulo': 0.09; 'cc:addr :python-list': 0.10; "'/'": 0.16; 'opcode': 0.16; 'operators.': 0.16; 'wrote:': 0.17; '(or': 0.18; 'bit': 0.21; '3.x': 0.22; 'fraction': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'loop,': 0.29; 'yes.': 0.29; 'maybe': 0.29; 'expect': 0.31; 'problem.': 0.32; 'instruction': 0.32; 'point,': 0.33; 'problem': 0.33; 'operations': 0.33; 'done': 0.34; 'faster': 0.35; 'doing': 0.35; 'pm,': 0.35; 'there': 0.35; 'but': 0.36; 'subject:" ': 0.36; 'bad': 0.37; 'one,': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'performance': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'subject:-': 0.40; 'more': 0.63; 'other.': 0.64; 'frequently': 0.65; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'power': 0.74; 'expresses': 0.84; 'measure.': 0.84; 'subject:Floor': 0.84; 'results,': 0.91 Date: Thu, 06 Sep 2012 21:36:54 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: jimbo1qaz Subject: Re: Bitshifts and "And" vs Floor-division and Modular References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:6BWNJTQNLxWv0DlMFUzUi9Y/FFj0eywCWoQDhx4Mv7j Hoc+BC8dVup+mWThRThlsOM4wFi0kxuxhHBh74tQ41iMHpvY+N mA6kyvIr9DpbHq+UHjSOTiMfehsFwSBpRwjeif+y+FFRjA9YzF BW3HhoM/bFN7kx+gFM1gKCP7DlnGnt2h1SD72nKgnJKNV6bthx syuH+yR8ZdySa/vn2r4US/VKTyZOfFufkbmLhpmeB3hPIpEflx BUIHgfRkzNQIelI6K6UBgzv7fZnqoY5IZU2lWMZMTPA/qfrTc4 vHTzNoTX3m8VfvbtKBdutIQ01NLGTJBLIySfmUZpyzTEUtryg= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346981842 news.xs4all.nl 6934 [2001:888:2000:d::a6]:40598 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28661 On 09/06/2012 08:01 PM, jimbo1qaz wrote: > Is it faster to use bitshifts or floor division? Yes, and yes. Without doing any measurement, I'd expect that in CPython, it makes negligible performance difference for ordinary ints (under 2**31, more or less). Ordinary ints can be done with single instructions, and any such instruction would be a tiny fraction of the opcode overhead. One place were there might be a difference would be for longs. The implementation of those would have to be a loop, and eventually one might be faster than the other. At that point, maybe you'd want to measure. > And which is better, & or %? > All divisors and mods are power of 2, so are binary operations faster? And are they considered bad style? The better way is not the faster one, but rather is the one that more clearly expresses the original problem. If the problem is a modulo one, use % (or frequently divmod). If the problem is a bit shift/masking one, then use such operators. BTW, '/' on integers is redefined for Python 3.x to give float results, and not to truncate. -- DaveA