Re: bit count or bit set && Python3

2012-10-26 Thread Charles Hixson
cas...@gmail.com wrote: On Thursday, October 25, 2012 7:56:25 AM UTC-7, Charles Hixson wrote: In Python3 is there any good way to count the number of on bits in an integer (after an& operation)? You may want to look at gmpy2[1] and the popcount() function. Alternatively, is there any VERY li

Re: bit count or bit set && Python3

2012-10-26 Thread casevh
On Thursday, October 25, 2012 7:56:25 AM UTC-7, Charles Hixson wrote: > In Python3 is there any good way to count the number of on bits in an > integer (after an & operation)? You may want to look at gmpy2[1] and the popcount() function. > > Alternatively, is there any VERY light-weight impleme

Re: bit count or bit set && Python3

2012-10-26 Thread Neil Cerutti
On 2012-10-25, Ian Kelly wrote: > On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti > wrote: >> Yes indeed! Python string operations are fast enough and its >> arithmetic slow enough that I no longer assume I can beat a >> neat lexicographical solution. Try defeating the following >> with arithmetic:

Re: bit count or bit set && Python3

2012-10-26 Thread Antoon Pardon
On 25-10-12 16:47, Charles Hixson wrote: > In Python3 is there any good way to count the number of on bits in an > integer (after an & operation)? > Alternatively, is there any VERY light-weight implementation of a bit > set? I'd prefer to use integers, as I'm probably going to need > thousands of

Re: bit count or bit set && Python3

2012-10-25 Thread Steven D'Aprano
On Thu, 25 Oct 2012 14:20:00 -0600, Ian Kelly wrote: > On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti wrote: >> Yes indeed! Python string operations are fast enough and its arithmetic >> slow enough that I no longer assume I can beat a neat lexicographical >> solution. Try defeating the following

Re: bit count or bit set && Python3

2012-10-25 Thread Mark Lawrence
On 25/10/2012 17:08, Charles Hixson wrote: On 10/25/2012 08:57 AM, Steven D'Aprano wrote: On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: 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"

bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm probably going to need thousands of these, if the tests work out. But before I can

Re: bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
On 10/25/2012 08:57 AM, Steven D'Aprano wrote: On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: 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. Oh I don't know abo

bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm probably going to need thousands of these, if the tests work out. But before I can

Re: bit count or bit set && Python3

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti wrote: > Yes indeed! Python string operations are fast enough and its > arithmetic slow enough that I no longer assume I can beat a neat > lexicographical solution. Try defeating the following with > arithmetic: > > def is_palindrom(n): >s = str(n)

Re: bit count or bit set && Python3

2012-10-25 Thread Terry Reedy
On 10/25/2012 12:13 PM, rusi wrote: On Oct 25, 7:56 pm, Charles Hixson wrote: In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm prob

Re: bit count or bit set && Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Neil Cerutti wrote: > Try defeating the following with arithmetic: > > def is_palindrom(n): >s = str(n) >return s = s[::-1] Sorry for the typos. It should've been: def is_palindrome(n): s = str(n) return s == s[::-1] -- Neil Cerutti -- http://mail.python.org/mailm

Re: bit count or bit set && Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Steven D'Aprano wrote: > On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: >> 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. > > O

Re: bit count or bit set && Python3

2012-10-25 Thread Serhiy Storchaka
Chris Angelico's suggestion: >>> bitcount = bytes(bin(i).count("1") for i in range(256)) >>> t = Timer('sum(number.to_bytes((number.bit_length() + 7) // 8,' ... '"little").translate(bitcount))', ... setup='from __main__ import bitcount; number=2**10001-1') >>> min(t.repeat(number=1, repeat=7)

Re: bit count or bit set && Python3

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 11:25 AM, Christian Gollwitzer wrote: > There is a very geeky algorithm with only a few integer operations. > > Checkout > http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64 > > for a C version. Maybe the same thing is equally fast when ported to Python. It

Re: bit count or bit set && Python3

2012-10-25 Thread Christian Gollwitzer
Am 25.10.12 16:47, schrieb Charles Hixson: In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm probably going to need thousands of these

Re: bit count or bit set && Python3

2012-10-25 Thread Steven D'Aprano
On Thu, 25 Oct 2012 09:17:40 -0700, rusi wrote: > On Oct 25, 8:57 pm, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: >> > On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes >> > wrote: >> >> Simple, easy, faster than a Python l

Re: bit count or bit set && Python3

2012-10-25 Thread rusi
On Oct 25, 9:30 pm, Chris Angelico wrote: > On Fri, Oct 26, 2012 at 3:17 AM, rusi wrote: > > On Oct 25, 8:57 pm, Steven D'Aprano > +comp.lang.pyt...@pearwood.info> wrote: > >> py> min(t.repeat(number=1, repeat=7)) > >> 0.6819710731506348 > >> py> min(t.repeat(number=100, repeat=7)) > >> 4.14

Re: bit count or bit set && Python3

2012-10-25 Thread Mark Lawrence
On 25/10/2012 17:29, Chris Angelico wrote: On Fri, Oct 26, 2012 at 3:17 AM, rusi wrote: On Oct 25, 8:57 pm, Steven D'Aprano wrote: py> min(t.repeat(number=1, repeat=7)) 0.6819710731506348 py> min(t.repeat(number=100, repeat=7)) 4.141788959503174 That makes the "inelegant" solution using

Re: bit count or bit set && Python3

2012-10-25 Thread Dan Stromberg
On Thu, Oct 25, 2012 at 9:24 AM, Mark Lawrence wrote: > On 25/10/2012 15:47, Charles Hixson wrote: > >> In Python3 is there any good way to count the number of on bits in an >> integer (after an & operation)? >> Alternatively, is there any VERY light-weight implementation of a bit >> set? I'd pre

Re: bit count or bit set && Python3

2012-10-25 Thread Chris Angelico
On Fri, Oct 26, 2012 at 3:17 AM, rusi wrote: > On Oct 25, 8:57 pm, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> py> min(t.repeat(number=1, repeat=7)) >> 0.6819710731506348 >> py> min(t.repeat(number=100, repeat=7)) >> 4.141788959503174 >> >> That makes the "inelegant" solution u

Re: bit count or bit set && Python3

2012-10-25 Thread Mark Lawrence
On 25/10/2012 15:47, Charles Hixson wrote: In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm probably going to need thousands of these

Re: bit count or bit set && Python3

2012-10-25 Thread rusi
On Oct 25, 8:57 pm, Steven D'Aprano wrote: > On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: > > 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. >

Re: bit count or bit set && Python3

2012-10-25 Thread rusi
On Oct 25, 7:56 pm, Charles Hixson wrote: > In Python3 is there any good way to count the number of on bits in an > integer (after an & operation)? > Alternatively, is there any VERY light-weight implementation of a bit > set?  I'd prefer to use integers, as I'm probably going to need > thousands

Re: bit count or bit set && Python3

2012-10-25 Thread Steven D'Aprano
On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: > 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. Oh I don't know about that. Here's some timing results

Re: bit count or bit set && Python3

2012-10-25 Thread Chris Angelico
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

Re: bit count or bit set && Python3

2012-10-25 Thread Christian Heimes
Am 25.10.2012 16:47, schrieb Charles Hixson: > In Python3 is there any good way to count the number of on bits in an > integer (after an & operation)? Simple, easy, faster than a Python loop but not very elegant: bin(number).count("1") Christian -- http://mail.python.org/mailman/listinfo/

Re: bit count or bit set && Python3

2012-10-25 Thread MRAB
On 2012-10-25 15:47, Charles Hixson wrote: In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm probably going to need thousands of these

bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
In Python3 is there any good way to count the number of on bits in an integer (after an & operation)? Alternatively, is there any VERY light-weight implementation of a bit set? I'd prefer to use integers, as I'm probably going to need thousands of these, if the tests work out. But before I can