Skip to content

Commit bb07854

Browse files
JatinR05pre-commit-ci[bot]cclauss
authored
Update count_number_of_one_bits.py (TheAlgorithms#7589)
* 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>
1 parent bd49061 commit bb07854

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed
+68-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,91 @@
1-
def get_set_bits_count(number: int) -> int:
1+
from timeit import timeit
2+
3+
4+
def get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int:
25
"""
36
Count the number of set bits in a 32 bit integer
4-
>>> get_set_bits_count(25)
7+
>>> get_set_bits_count_using_brian_kernighans_algorithm(25)
58
3
6-
>>> get_set_bits_count(37)
9+
>>> get_set_bits_count_using_brian_kernighans_algorithm(37)
710
3
8-
>>> get_set_bits_count(21)
11+
>>> get_set_bits_count_using_brian_kernighans_algorithm(21)
912
3
10-
>>> get_set_bits_count(58)
13+
>>> get_set_bits_count_using_brian_kernighans_algorithm(58)
1114
4
12-
>>> get_set_bits_count(0)
15+
>>> get_set_bits_count_using_brian_kernighans_algorithm(0)
1316
0
14-
>>> get_set_bits_count(256)
17+
>>> get_set_bits_count_using_brian_kernighans_algorithm(256)
1518
1
16-
>>> get_set_bits_count(-1)
19+
>>> get_set_bits_count_using_brian_kernighans_algorithm(-1)
1720
Traceback (most recent call last):
1821
...
19-
ValueError: the value of input must be positive
22+
ValueError: the value of input must not be negative
2023
"""
2124
if number < 0:
22-
raise ValueError("the value of input must be positive")
25+
raise ValueError("the value of input must not be negative")
26+
result = 0
27+
while number:
28+
number &= number - 1
29+
result += 1
30+
return result
31+
32+
33+
def get_set_bits_count_using_modulo_operator(number: int) -> int:
34+
"""
35+
Count the number of set bits in a 32 bit integer
36+
>>> get_set_bits_count_using_modulo_operator(25)
37+
3
38+
>>> get_set_bits_count_using_modulo_operator(37)
39+
3
40+
>>> get_set_bits_count_using_modulo_operator(21)
41+
3
42+
>>> get_set_bits_count_using_modulo_operator(58)
43+
4
44+
>>> get_set_bits_count_using_modulo_operator(0)
45+
0
46+
>>> get_set_bits_count_using_modulo_operator(256)
47+
1
48+
>>> get_set_bits_count_using_modulo_operator(-1)
49+
Traceback (most recent call last):
50+
...
51+
ValueError: the value of input must not be negative
52+
"""
53+
if number < 0:
54+
raise ValueError("the value of input must not be negative")
2355
result = 0
2456
while number:
2557
if number % 2 == 1:
2658
result += 1
27-
number = number >> 1
59+
number >>= 1
2860
return result
2961

3062

63+
def benchmark() -> None:
64+
"""
65+
Benchmark code for comparing 2 functions, with different length int values.
66+
Brian Kernighan's algorithm is consistently faster than using modulo_operator.
67+
"""
68+
69+
def do_benchmark(number: int) -> None:
70+
setup = "import __main__ as z"
71+
print(f"Benchmark when {number = }:")
72+
print(f"{get_set_bits_count_using_modulo_operator(number) = }")
73+
timing = timeit("z.get_set_bits_count_using_modulo_operator(25)", setup=setup)
74+
print(f"timeit() runs in {timing} seconds")
75+
print(f"{get_set_bits_count_using_brian_kernighans_algorithm(number) = }")
76+
timing = timeit(
77+
"z.get_set_bits_count_using_brian_kernighans_algorithm(25)",
78+
setup=setup,
79+
)
80+
print(f"timeit() runs in {timing} seconds")
81+
82+
for number in (25, 37, 58, 0):
83+
do_benchmark(number)
84+
print()
85+
86+
3187
if __name__ == "__main__":
3288
import doctest
3389

3490
doctest.testmod()
91+
benchmark()

0 commit comments

Comments
 (0)