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-26 Thread Neil Cerutti
On 2012-10-25, Ian Kelly ian.g.ke...@gmail.com wrote: On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti ne...@norwich.edu 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

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

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

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 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

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 --

Re: bit count or bit set Python3

2012-10-25 Thread Chris Angelico
On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes christ...@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

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 christ...@python.org 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

Re: bit count or bit set Python3

2012-10-25 Thread rusi
On Oct 25, 7:56 pm, Charles Hixson charleshi...@earthlink.net 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

Re: bit count or bit set Python3

2012-10-25 Thread rusi
On Oct 25, 8:57 pm, Steven D'Aprano steve +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 christ...@python.org wrote: Simple, easy, faster than a Python loop but not very elegant:    

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

Re: bit count or bit set Python3

2012-10-25 Thread Chris Angelico
On Fri, Oct 26, 2012 at 3:17 AM, rusi rustompm...@gmail.com wrote: On Oct 25, 8:57 pm, Steven D'Aprano steve +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

Re: bit count or bit set Python3

2012-10-25 Thread Dan Stromberg
On Thu, Oct 25, 2012 at 9:24 AM, Mark Lawrence breamore...@yahoo.co.ukwrote: 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

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 rustompm...@gmail.com wrote: On Oct 25, 8:57 pm, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: py min(t.repeat(number=1, repeat=7)) 0.6819710731506348 py min(t.repeat(number=100, repeat=7))

Re: bit count or bit set Python3

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

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 steve +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 christ...@python.org wrote: Simple, easy, faster

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

Re: bit count or bit set Python3

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 11:25 AM, Christian Gollwitzer aurio...@gmx.de 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

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 Neil Cerutti
On 2012-10-25, Steven D'Aprano steve+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 christ...@python.org wrote: Simple, easy, faster than a Python loop but not very elegant:

Re: bit count or bit set Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Neil Cerutti ne...@norwich.edu 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 --

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 charleshi...@earthlink.net 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

Re: bit count or bit set Python3

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti ne...@norwich.edu 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):

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 Heimeschrist...@python.org wrote: Simple, easy, faster than a Python loop but not very elegant: bin(number).count(1) Unlikely to be fast.

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 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 Heimeschrist...@python.org wrote: Simple, easy, faster than a Python loop but not very elegant:

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 ne...@norwich.edu 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