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


Groups > comp.lang.python > #28661

Re: Bitshifts and "And" vs Floor-division and Modular

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 <d@davea.name>
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 <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To jimbo1qaz <jimmyli1528@gmail.com>
Subject Re: Bitshifts and "And" vs Floor-division and Modular
References <d8d77115-dcb2-4769-a592-5fca0fc264bc@googlegroups.com>
In-Reply-To <d8d77115-dcb2-4769-a592-5fca0fc264bc@googlegroups.com>
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 <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>
Newsgroups comp.lang.python
Message-ID <mailman.343.1346981842.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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


Thread

Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 17:01 -0700
  Re: Bitshifts and "And" vs Floor-division and Modular Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-07 01:30 +0100
    Re: Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 18:05 -0700
    Re: Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 18:05 -0700
  Re: Bitshifts and "And" vs Floor-division and Modular jimbo1qaz <jimmyli1528@gmail.com> - 2012-09-06 18:30 -0700
    Re: Bitshifts and "And" vs Floor-division and Modular Dave Angel <d@davea.name> - 2012-09-06 21:46 -0400
    Re: Bitshifts and "And" vs Floor-division and Modular Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-07 02:23 +0000
  Re: Bitshifts and "And" vs Floor-division and Modular Dave Angel <d@davea.name> - 2012-09-06 21:36 -0400
  Re: Bitshifts and "And" vs Floor-division and Modular Terry Reedy <tjreedy@udel.edu> - 2012-09-06 21:53 -0400
  Re: Bitshifts and "And" vs Floor-division and Modular Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-07 02:00 +0000
    Re: Bitshifts and "And" vs Floor-division and Modular Grant Edwards <invalid@invalid.invalid> - 2012-09-07 14:19 +0000
  Re: Bitshifts and "And" vs Floor-division and Modular rusi <rustompmody@gmail.com> - 2012-09-06 20:38 -0700
    Re: Bitshifts and "And" vs Floor-division and Modular Paul Rubin <no.email@nospam.invalid> - 2012-09-06 21:32 -0700
      Re: Bitshifts and "And" vs Floor-division and Modular rusi <rustompmody@gmail.com> - 2012-09-07 09:59 -0700
        Re: Bitshifts and "And" vs Floor-division and Modular Dave Angel <d@davea.name> - 2012-09-07 13:12 -0400

csiph-web