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: 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: References: <5089511E.4090009@earthlink.net> Date: Fri, 26 Oct 2012 02:31:53 +1100 Subject: Re: bit count or bit set && Python3 From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes 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