Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'algorithm': 0.03; 'binary': 0.05; 'raises': 0.07; 'works.': 0.07; 'augmented': 0.09; 'compiler.': 0.09; 'longs': 0.09; 'meaningful': 0.09; 'cc:addr :python-list': 0.10; 'assignment.': 0.16; 'digits.': 0.16; 'wrote:': 0.17; 'code.': 0.20; 'bit': 0.21; 'assuming': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'setting': 0.26; 'bugs': 0.27; 'run': 0.28; 'accidentally': 0.29; 'question:': 0.29; 'maybe': 0.29; 'thursday,': 0.30; 'message.': 0.33; 'operations': 0.33; 'faster': 0.35; 'doing': 0.35; 'pm,': 0.35; 'similar': 0.35; 'but': 0.36; 'subject:" ': 0.36; 'should': 0.36; 'bad': 0.37; 'ok,': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'subject:-': 0.40; 'think': 0.40; "you'll": 0.62; 'time,': 0.62; 'more': 0.63; 'decided': 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; 'clearer': 0.84; 'probable': 0.84; 'subject:Floor': 0.84; 'hundred': 0.95 Date: Thu, 06 Sep 2012 21:46:12 -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:zVC9J+GSIwFUMBQnZzQca04coptu7ptSVfSMr4Ssi6J NANJIpi+E4676hCr4DR9oRWPzhYkprlsndbv8D0GsE3BTWl1NT 1J+ZpPzx5Hy/drHYL/hOfJZIPw/Fy0+W5P6ZvLcsX0htF1RSt2 2Uce4c8xCxzXPtzhBs7WsiGDWhcU0J7UTu8BvpcaMtk9AUjMvA BqjiqmK1E90yJqIB2K4UqARhUudyPJvAxPN/wJfBDbq9FzNh6M CIazdLGwh0uw2u873QMEtu+74SJM4UK3o6v7m9zpDxL24YioNg NviaTYtV2XuuVvF/nF9Db1uY0yI1JBy77EQOLBrig+HpJUsDg= = 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346982399 news.xs4all.nl 6901 [2001:888:2000:d::a6]:47438 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28663 On 09/06/2012 09:30 PM, jimbo1qaz wrote: > On Thursday, September 6, 2012 5:01:12 PM UTC-7, jimbo1qaz wrote: >> Is it faster to use bitshifts or floor division? And which is better, & or %? >> >> All divisors and mods are power of 2, so are binary operations faster? And are they considered bad style? > OK, I decided to change my code. Which raises a similar question: Which one is better for setting a bit of a byte: |= or +=, assuming each will only be run once? Intuitively, I think |=, but some timeits are inconclusive, mainly because I don't know how it works. Maybe i should have been clearer in my message. i don't think you'll find a meaningful difference unless you're doing longs of a few hundred digits. So if the algorithm is to OR on a bit, please use a |= augmented assignment. not only will it ward off probable bugs when you accidentally try to set the bit a second time, but it reads better for the reader of the program. The reader is more important than the compiler. -- DaveA