Skip to main content
Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1326947902648967170
Rollback to Revision 2
Source Link
pacmaninbw
  • 24.8k
  • 11
  • 46
  • 109

Now I want to compute at how many of the L positions in the binary representation there is a 1 as well as a zero 0. In my example the result would be return=2 since there is always a 1 in the third (last) position for these entries. I want to compute this inside a function with a numba decoratorI want to compute this inside a function with a numba decorator. Currently my code is:

Comparing the performance:

def count_mixed_bits_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        xor = reduce(and_, lst) ^ reduce(or_, lst)
        retu = bin(xor).count("1")
    return retu

@nb.njit
def count_mixed_bits_v2_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        andnumber = ornumber = lst[0]
        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
            result += xornumber & 1
            xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda: count_mixed_bits_timeit(), number=10))
print(timeit(lambda: count_mixed_bits_v2_timeit(), number=10))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=10))

>>>2.4878
>>>0.1198
>>>0.1279

Now I want to compute at how many of the L positions in the binary representation there is a 1 as well as a zero 0. In my example the result would be return=2 since there is always a 1 in the third (last) position for these entries. I want to compute this inside a function with a numba decorator. Currently my code is:

Comparing the performance:

def count_mixed_bits_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        xor = reduce(and_, lst) ^ reduce(or_, lst)
        retu = bin(xor).count("1")
    return retu

@nb.njit
def count_mixed_bits_v2_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        andnumber = ornumber = lst[0]
        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
            result += xornumber & 1
            xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda: count_mixed_bits_timeit(), number=10))
print(timeit(lambda: count_mixed_bits_v2_timeit(), number=10))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=10))

>>>2.4878
>>>0.1198
>>>0.1279

Now I want to compute at how many of the L positions in the binary representation there is a 1 as well as a zero 0. In my example the result would be return=2 since there is always a 1 in the third (last) position for these entries. I want to compute this inside a function with a numba decorator. Currently my code is:

deleted 24 characters in body
Source Link
def count_mixed_bits_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        xor = reduce(and_, lst) ^ reduce(or_, lst)
    return    retu = bin(xor).count("1")
    return retu

@nb.njit
def count_mixed_bits_v2_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        andnumber = ornumber = lst[0]
        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
            result += xornumber & 1
            xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda: count_mixed_bits_timeit(), number=100number=10))
print(timeit(lambda: count_mixed_bits_v2_timeit(), number=100number=10))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=100number=10))

>>>0>>>2.0840504878
>>>1>>>0.3184901198
>>>0.0687231279
def count_mixed_bits_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
        xor = reduce(and_, lst) ^ reduce(or_, lst)
    return bin(xor).count("1")


@nb.njit
def count_mixed_bits_v2_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
        andnumber = ornumber = lst[0]
        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
           result += xornumber & 1
           xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda: count_mixed_bits_timeit(), number=100))
print(timeit(lambda: count_mixed_bits_v2_timeit(), number=100))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=100))

>>>0.084050
>>>1.318490
>>>0.068723
def count_mixed_bits_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        xor = reduce(and_, lst) ^ reduce(or_, lst)
        retu = bin(xor).count("1")
    return retu

@nb.njit
def count_mixed_bits_v2_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        andnumber = ornumber = lst[0]
        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
            result += xornumber & 1
            xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    lst = [random.randint(0, 500) for i in range(10000)]
    for _ in range(300):

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda: count_mixed_bits_timeit(), number=10))
print(timeit(lambda: count_mixed_bits_v2_timeit(), number=10))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=10))

>>>2.4878
>>>0.1198
>>>0.1279
added 1072 characters in body
Source Link

Comparing the performance it seems that count_mixed_bits is much faster.:

fromdef timeitcount_mixed_bits_timeit():
 import timeit  for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
        xor = reduce(and_, lst) ^ reduce(or_, lst)
    return bin(xor).count("1")


@nb.njit
def count_mixed_bits_v2_timeit():
    for _ in range(300):
        lst = 100[random.randint(0, 500) for i in range(10000)]
import random       andnumber = ornumber = lst[0]
randomlist        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
           result += xornumber & 1
           xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    for _ in range(300):
        lst = [random.randint(0,100 500) for i in range(10000)] 

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda:count_mixed_bits count_mixed_bits_timeit(randomlist), number=countnumber=100))
print(timeit(lambda:count_mixed_bits_v2 count_mixed_bits_v2_timeit(randomlist), number=countnumber=100))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=100))

>>>0.07977239999999997084050
>>>1.4027323318490
>>>0.068723

Comparing the performance it seems that count_mixed_bits is much faster.

from timeit import timeit
count = 100
import random
randomlist = [random.randint(0,100) for i in range(10000)]
print(timeit(lambda:count_mixed_bits(randomlist), number=count))
print(timeit(lambda:count_mixed_bits_v2(randomlist), number=count))

>>>0.07977239999999997
>>>1.4027323

Comparing the performance:

def count_mixed_bits_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
        xor = reduce(and_, lst) ^ reduce(or_, lst)
    return bin(xor).count("1")


@nb.njit
def count_mixed_bits_v2_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)]
        andnumber = ornumber = lst[0]
        for value in lst:
            andnumber &= value
            ornumber |= value
        xornumber = andnumber ^ ornumber
        result = 0
        while xornumber > 0:
           result += xornumber & 1
           xornumber >>= 1
    return result


@nb.njit
def count_mixed_bits_v3_timeit():
    for _ in range(300):
        lst = [random.randint(0, 500) for i in range(10000)] 

        and_, or_ = ~0, 0
        for x in lst:
            and_ &= x
            or_ |= x
        xor_ = and_ ^ or_
        result = 0
        while xor_ > 0:
           result += 1
           xor_ &= xor_ - 1
    return result


print(timeit(lambda: count_mixed_bits_timeit(), number=100))
print(timeit(lambda: count_mixed_bits_v2_timeit(), number=100))
print(timeit(lambda: count_mixed_bits_v3_timeit(), number=100))

>>>0.084050
>>>1.318490
>>>0.068723
added 212 characters in body
Source Link
Loading
added 212 characters in body
Source Link
Loading
Source Link
Loading