|
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: |
2 | 5 | """
|
3 | 6 | 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) |
5 | 8 | 3
|
6 |
| - >>> get_set_bits_count(37) |
| 9 | + >>> get_set_bits_count_using_brian_kernighans_algorithm(37) |
7 | 10 | 3
|
8 |
| - >>> get_set_bits_count(21) |
| 11 | + >>> get_set_bits_count_using_brian_kernighans_algorithm(21) |
9 | 12 | 3
|
10 |
| - >>> get_set_bits_count(58) |
| 13 | + >>> get_set_bits_count_using_brian_kernighans_algorithm(58) |
11 | 14 | 4
|
12 |
| - >>> get_set_bits_count(0) |
| 15 | + >>> get_set_bits_count_using_brian_kernighans_algorithm(0) |
13 | 16 | 0
|
14 |
| - >>> get_set_bits_count(256) |
| 17 | + >>> get_set_bits_count_using_brian_kernighans_algorithm(256) |
15 | 18 | 1
|
16 |
| - >>> get_set_bits_count(-1) |
| 19 | + >>> get_set_bits_count_using_brian_kernighans_algorithm(-1) |
17 | 20 | Traceback (most recent call last):
|
18 | 21 | ...
|
19 |
| - ValueError: the value of input must be positive |
| 22 | + ValueError: the value of input must not be negative |
20 | 23 | """
|
21 | 24 | 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") |
23 | 55 | result = 0
|
24 | 56 | while number:
|
25 | 57 | if number % 2 == 1:
|
26 | 58 | result += 1
|
27 |
| - number = number >> 1 |
| 59 | + number >>= 1 |
28 | 60 | return result
|
29 | 61 |
|
30 | 62 |
|
| 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 | + |
31 | 87 | if __name__ == "__main__":
|
32 | 88 | import doctest
|
33 | 89 |
|
34 | 90 | doctest.testmod()
|
| 91 | + benchmark() |
0 commit comments