Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32135
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| 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.09; 'integers': 0.09; 'subject:Python3': 0.09; 'subject:set': 0.09; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'oct': 0.16; 'subject:bit': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'test.': 0.17; 'sort': 0.21; 'bit': 0.21; 'received:209.85.214.174': 0.21; 'this:': 0.23; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fast.': 0.29; 'unlikely': 0.29; 'fri,': 0.30; 'could': 0.32; 'to:addr:python-list': 0.33; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'christian': 0.34; 'list': 0.35; 'faster': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'profile': 0.61; '26,': 0.65; 'counts': 0.81; 'approach.': 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=y4g7QRTlxwTxQbTv651QnR9r/VMdON8362GNh0eiTAA=; b=effAsDkX1WIz95SI7fL9HXb/l1lvX5PzThVxHD9FXgcy0mhChGBl24V49HcUF4D9Zi eNHtJ1PHI22pu28V4rMMwPfkXCn0bj86g0RZ1E58ePIJrjtSfIHEJI4nUx4PZSYwU2Od 52TxGer/onlaq3kKp/TSgayzw7MDwHmpeMZjgS4EGPtexEzCtHyPWdkG7Btbq/YVk91h JHSofTzrjVcKGkqT55VwvU5BNVVkiCJQpGsakM8ruBFfnXMzumvAC57CQYtzG10xZ4mW kzqxggLNl5tB8JyMkRW2aE4cbHzEImAEU+zsX8Y245vSLDEKf0dOqgnVPAyQUElPd/ym NcTA== |
| MIME-Version | 1.0 |
| In-Reply-To | <k6bllq$3gt$1@ger.gmane.org> |
| References | <5089511E.4090009@earthlink.net> <k6bllq$3gt$1@ger.gmane.org> |
| Date | Fri, 26 Oct 2012 02:31:53 +1100 |
| Subject | Re: bit count or bit set && Python3 |
| From | Chris Angelico <rosuav@gmail.com> |
| 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 <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.2849.1351179123.27098.python-list@python.org> (permalink) |
| Lines | 19 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1351179123 news.xs4all.nl 6852 [2001:888:2000:d::a6]:43142 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:32135 |
Show key headers only | View raw
On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes <christian@python.org> wrote:
> Simple, easy, faster than a Python loop but not very elegant:
>
> bin(number).count("1")
Unlikely to be fast.
What you may want is some sort of hybrid loop/lookup approach. Do you
know what your highest bit number is going to be? For instance, are
all your integers 32-bit? You could use something like this:
c = bitcount[n&255] + bitcount[n>>8&255] + bitcount[n>>16&255] + bitcount[n>>24]
where bitcount is a list of 256 values, giving the counts for each
value from 0 to 255.
Profile and test. :)
ChrisA
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: bit count or bit set && Python3 Chris Angelico <rosuav@gmail.com> - 2012-10-26 02:31 +1100
Re: bit count or bit set && Python3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 15:57 +0000
Re: bit count or bit set && Python3 rusi <rustompmody@gmail.com> - 2012-10-25 09:17 -0700
Re: bit count or bit set && Python3 Chris Angelico <rosuav@gmail.com> - 2012-10-26 03:29 +1100
Re: bit count or bit set && Python3 rusi <rustompmody@gmail.com> - 2012-10-25 09:37 -0700
Re: bit count or bit set && Python3 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-25 17:44 +0100
Re: bit count or bit set && Python3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 17:16 +0000
Re: bit count or bit set && Python3 Serhiy Storchaka <storchaka@gmail.com> - 2012-10-25 22:07 +0300
Re: bit count or bit set && Python3 Neil Cerutti <neilc@norwich.edu> - 2012-10-25 20:00 +0000
Re: bit count or bit set && Python3 Neil Cerutti <neilc@norwich.edu> - 2012-10-25 20:04 +0000
Re: bit count or bit set && Python3 Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-25 14:20 -0600
Re: bit count or bit set && Python3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 23:48 +0000
Re: bit count or bit set && Python3 Neil Cerutti <neilc@norwich.edu> - 2012-10-26 12:56 +0000
Re: bit count or bit set && Python3 Charles Hixson <charleshixsn@earthlink.net> - 2012-10-25 09:08 -0700
Re: bit count or bit set && Python3 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-25 22:51 +0100
csiph-web