mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
bb078541dd
* Update count_number_of_one_bits.py removed the modulo operator as it is very time consuming in comparison to the and operator * Update count_number_of_one_bits.py Updated with the timeit library to compare. Moreover I have updated my code which helps us in reaching the output comparatively faster. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update bit_manipulation/count_number_of_one_bits.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update count_number_of_one_bits.py Updated the code * Update count_number_of_one_bits.py Updated code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Run the tests before running the benchmarks * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * consistently Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
from timeit import timeit
|
|
|
|
|
|
def get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int:
|
|
"""
|
|
Count the number of set bits in a 32 bit integer
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(25)
|
|
3
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(37)
|
|
3
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(21)
|
|
3
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(58)
|
|
4
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(0)
|
|
0
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(256)
|
|
1
|
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(-1)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: the value of input must not be negative
|
|
"""
|
|
if number < 0:
|
|
raise ValueError("the value of input must not be negative")
|
|
result = 0
|
|
while number:
|
|
number &= number - 1
|
|
result += 1
|
|
return result
|
|
|
|
|
|
def get_set_bits_count_using_modulo_operator(number: int) -> int:
|
|
"""
|
|
Count the number of set bits in a 32 bit integer
|
|
>>> get_set_bits_count_using_modulo_operator(25)
|
|
3
|
|
>>> get_set_bits_count_using_modulo_operator(37)
|
|
3
|
|
>>> get_set_bits_count_using_modulo_operator(21)
|
|
3
|
|
>>> get_set_bits_count_using_modulo_operator(58)
|
|
4
|
|
>>> get_set_bits_count_using_modulo_operator(0)
|
|
0
|
|
>>> get_set_bits_count_using_modulo_operator(256)
|
|
1
|
|
>>> get_set_bits_count_using_modulo_operator(-1)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: the value of input must not be negative
|
|
"""
|
|
if number < 0:
|
|
raise ValueError("the value of input must not be negative")
|
|
result = 0
|
|
while number:
|
|
if number % 2 == 1:
|
|
result += 1
|
|
number >>= 1
|
|
return result
|
|
|
|
|
|
def benchmark() -> None:
|
|
"""
|
|
Benchmark code for comparing 2 functions, with different length int values.
|
|
Brian Kernighan's algorithm is consistently faster than using modulo_operator.
|
|
"""
|
|
|
|
def do_benchmark(number: int) -> None:
|
|
setup = "import __main__ as z"
|
|
print(f"Benchmark when {number = }:")
|
|
print(f"{get_set_bits_count_using_modulo_operator(number) = }")
|
|
timing = timeit("z.get_set_bits_count_using_modulo_operator(25)", setup=setup)
|
|
print(f"timeit() runs in {timing} seconds")
|
|
print(f"{get_set_bits_count_using_brian_kernighans_algorithm(number) = }")
|
|
timing = timeit(
|
|
"z.get_set_bits_count_using_brian_kernighans_algorithm(25)",
|
|
setup=setup,
|
|
)
|
|
print(f"timeit() runs in {timing} seconds")
|
|
|
|
for number in (25, 37, 58, 0):
|
|
do_benchmark(number)
|
|
print()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
benchmark()
|