From 3597d0c8fa3c5d93b4fad83b6154d7869a8001ea Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:01:36 +0530 Subject: [PATCH 01/10] Update count_number_of_one_bits.py removed the modulo operator as it is very time consuming in comparison to the and operator --- bit_manipulation/count_number_of_one_bits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 51fd2b630483..93ab0d748197 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -22,7 +22,7 @@ def get_set_bits_count(number: int) -> int: raise ValueError("the value of input must be positive") result = 0 while number: - if number % 2 == 1: + if number & 1: result += 1 number = number >> 1 return result From 62e21187af2c7449a6b9b17ec16754a9a0eca151 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 13:28:00 +0530 Subject: [PATCH 02/10] 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. --- bit_manipulation/count_number_of_one_bits.py | 107 +++++++++++++++++-- 1 file changed, 98 insertions(+), 9 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 93ab0d748197..be439c6805e5 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -1,19 +1,20 @@ -def get_set_bits_count(number: int) -> int: +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(25) + >>> get_set_bits_count_using_brian_kernighans_algorithm(25) 3 - >>> get_set_bits_count(37) + >>> get_set_bits_count_using_brian_kernighans_algorithm(37) 3 - >>> get_set_bits_count(21) + >>> get_set_bits_count_using_brian_kernighans_algorithm(21) 3 - >>> get_set_bits_count(58) + >>> get_set_bits_count_using_brian_kernighans_algorithm(58) 4 - >>> get_set_bits_count(0) + >>> get_set_bits_count_using_brian_kernighans_algorithm(0) 0 - >>> get_set_bits_count(256) + >>> get_set_bits_count_using_brian_kernighans_algorithm(256) 1 - >>> get_set_bits_count(-1) + >>> get_set_bits_count_using_brian_kernighans_algorithm(-1) Traceback (most recent call last): ... ValueError: the value of input must be positive @@ -22,13 +23,101 @@ def get_set_bits_count(number: int) -> int: raise ValueError("the value of input must be positive") result = 0 while number: - if number & 1: + 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 be positive + """ + if number < 0: + raise ValueError("the value of input must be positive") + result = 0 + while number: + if number % 2 == 1: result += 1 number = number >> 1 return result +def benchmark() -> None: + """ + Benchmark code for comparing 2 functions, + with 3 different length int values. + """ + print("\nFor 25 = :") + print( + "> get_set_bits_count_using_modulo_operator()", + "\t\tans =", + get_set_bits_count_using_modulo_operator(25), + "\ttime =", + timeit("z.get_set_bits_count_using_modulo_operator(25)", setup="import __main__ as z"), + "seconds", + ) + print( + "> get_set_bits_count_using_brian_kernighans_algorithm()", + "\tans =", + get_set_bits_count_using_brian_kernighans_algorithm(25), + "\ttime =", + timeit("z.get_set_bits_count_using_brian_kernighans_algorithm(25)", setup="import __main__ as z"), + "seconds", + ) + + print("\nFor 37 = :") + print( + "> get_set_bits_count_using_modulo_operator()", + "\t\tans =", + get_set_bits_count_using_modulo_operator(37), + "\ttime =", + timeit("z.get_set_bits_count_using_modulo_operator(37)", setup="import __main__ as z"), + "seconds", + ) + print( + "> get_set_bits_count_using_brian_kernighans_algorithm()", + "\tans =", + get_set_bits_count_using_brian_kernighans_algorithm(37), + "\ttime =", + timeit("z.get_set_bits_count_using_brian_kernighans_algorithm(37)", setup="import __main__ as z"), + "seconds", + ) + + print("\nFor 58 = :") + print( + "> get_set_bits_count_using_modulo_operator()", + "\t\tans =", + get_set_bits_count_using_modulo_operator(58), + "\ttime =", + timeit("z.get_set_bits_count_using_modulo_operator(58)", setup="import __main__ as z"), + "seconds", + ) + print( + "> get_set_bits_count_using_brian_kernighans_algorithm()", + "\tans =", + get_set_bits_count_using_brian_kernighans_algorithm(58), + "\ttime =", + timeit("z.get_set_bits_count_using_brian_kernighans_algorithm(58)", setup="import __main__ as z"), + "seconds", + ) + if __name__ == "__main__": import doctest + benchmark() doctest.testmod() From 881d48039298c19b6340e72c149c112ac4e85055 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 07:58:53 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/count_number_of_one_bits.py | 38 +++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index be439c6805e5..4beb33edb25d 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -1,4 +1,6 @@ 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 @@ -23,10 +25,11 @@ def get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int: raise ValueError("the value of input must be positive") result = 0 while number: - number &= (number-1) - result+= 1 + 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 @@ -56,6 +59,7 @@ def get_set_bits_count_using_modulo_operator(number: int) -> int: number = number >> 1 return result + def benchmark() -> None: """ Benchmark code for comparing 2 functions, @@ -67,7 +71,10 @@ def benchmark() -> None: "\t\tans =", get_set_bits_count_using_modulo_operator(25), "\ttime =", - timeit("z.get_set_bits_count_using_modulo_operator(25)", setup="import __main__ as z"), + timeit( + "z.get_set_bits_count_using_modulo_operator(25)", + setup="import __main__ as z", + ), "seconds", ) print( @@ -75,7 +82,10 @@ def benchmark() -> None: "\tans =", get_set_bits_count_using_brian_kernighans_algorithm(25), "\ttime =", - timeit("z.get_set_bits_count_using_brian_kernighans_algorithm(25)", setup="import __main__ as z"), + timeit( + "z.get_set_bits_count_using_brian_kernighans_algorithm(25)", + setup="import __main__ as z", + ), "seconds", ) @@ -85,7 +95,10 @@ def benchmark() -> None: "\t\tans =", get_set_bits_count_using_modulo_operator(37), "\ttime =", - timeit("z.get_set_bits_count_using_modulo_operator(37)", setup="import __main__ as z"), + timeit( + "z.get_set_bits_count_using_modulo_operator(37)", + setup="import __main__ as z", + ), "seconds", ) print( @@ -93,7 +106,10 @@ def benchmark() -> None: "\tans =", get_set_bits_count_using_brian_kernighans_algorithm(37), "\ttime =", - timeit("z.get_set_bits_count_using_brian_kernighans_algorithm(37)", setup="import __main__ as z"), + timeit( + "z.get_set_bits_count_using_brian_kernighans_algorithm(37)", + setup="import __main__ as z", + ), "seconds", ) @@ -103,7 +119,10 @@ def benchmark() -> None: "\t\tans =", get_set_bits_count_using_modulo_operator(58), "\ttime =", - timeit("z.get_set_bits_count_using_modulo_operator(58)", setup="import __main__ as z"), + timeit( + "z.get_set_bits_count_using_modulo_operator(58)", + setup="import __main__ as z", + ), "seconds", ) print( @@ -111,7 +130,10 @@ def benchmark() -> None: "\tans =", get_set_bits_count_using_brian_kernighans_algorithm(58), "\ttime =", - timeit("z.get_set_bits_count_using_brian_kernighans_algorithm(58)", setup="import __main__ as z"), + timeit( + "z.get_set_bits_count_using_brian_kernighans_algorithm(58)", + setup="import __main__ as z", + ), "seconds", ) From dcaf2627a38a56fad517420f419f07fd39759822 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 14:05:01 +0530 Subject: [PATCH 04/10] Update bit_manipulation/count_number_of_one_bits.py Co-authored-by: Christian Clauss --- bit_manipulation/count_number_of_one_bits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 4beb33edb25d..75cead6128b2 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -19,7 +19,7 @@ def get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int: >>> get_set_bits_count_using_brian_kernighans_algorithm(-1) Traceback (most recent call last): ... - ValueError: the value of input must be positive + ValueError: the value of input must not be negative """ if number < 0: raise ValueError("the value of input must be positive") From 72704c20502c67d92e7edb49274de9a05619dfaf Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 14:36:42 +0530 Subject: [PATCH 05/10] Update count_number_of_one_bits.py Updated the code --- bit_manipulation/count_number_of_one_bits.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 75cead6128b2..13379388e6f2 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -22,7 +22,7 @@ def get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int: ValueError: the value of input must not be negative """ if number < 0: - raise ValueError("the value of input must be positive") + raise ValueError("the value of input must not be negative") result = 0 while number: number &= number - 1 @@ -48,10 +48,10 @@ def get_set_bits_count_using_modulo_operator(number: int) -> int: >>> get_set_bits_count_using_modulo_operator(-1) Traceback (most recent call last): ... - ValueError: the value of input must be positive + ValueError: the value of input must not be negative """ if number < 0: - raise ValueError("the value of input must be positive") + raise ValueError("the value of input must not be negative") result = 0 while number: if number % 2 == 1: From df79a7f317df2eeaa684b0056c97a3028ef9a34b Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Mon, 24 Oct 2022 15:17:19 +0530 Subject: [PATCH 06/10] Update count_number_of_one_bits.py Updated code --- bit_manipulation/count_number_of_one_bits.py | 90 +++++--------------- 1 file changed, 19 insertions(+), 71 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 13379388e6f2..787a4a3b6e3c 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -59,84 +59,32 @@ def get_set_bits_count_using_modulo_operator(number: int) -> int: number = number >> 1 return result - def benchmark() -> None: """ - Benchmark code for comparing 2 functions, - with 3 different length int values. + Benchmark code for comparing 2 functions, with different length int values. """ - print("\nFor 25 = :") - print( - "> get_set_bits_count_using_modulo_operator()", - "\t\tans =", - get_set_bits_count_using_modulo_operator(25), - "\ttime =", - timeit( - "z.get_set_bits_count_using_modulo_operator(25)", - setup="import __main__ as z", - ), - "seconds", - ) - print( - "> get_set_bits_count_using_brian_kernighans_algorithm()", - "\tans =", - get_set_bits_count_using_brian_kernighans_algorithm(25), - "\ttime =", - timeit( + 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="import __main__ as z", - ), - "seconds", - ) + setup=setup, + ) + print(f"timeit() runs in {timing} seconds") + + for number in (25, 37, 58, 0): + do_benchmark(number) + print() + + - print("\nFor 37 = :") - print( - "> get_set_bits_count_using_modulo_operator()", - "\t\tans =", - get_set_bits_count_using_modulo_operator(37), - "\ttime =", - timeit( - "z.get_set_bits_count_using_modulo_operator(37)", - setup="import __main__ as z", - ), - "seconds", - ) - print( - "> get_set_bits_count_using_brian_kernighans_algorithm()", - "\tans =", - get_set_bits_count_using_brian_kernighans_algorithm(37), - "\ttime =", - timeit( - "z.get_set_bits_count_using_brian_kernighans_algorithm(37)", - setup="import __main__ as z", - ), - "seconds", - ) - print("\nFor 58 = :") - print( - "> get_set_bits_count_using_modulo_operator()", - "\t\tans =", - get_set_bits_count_using_modulo_operator(58), - "\ttime =", - timeit( - "z.get_set_bits_count_using_modulo_operator(58)", - setup="import __main__ as z", - ), - "seconds", - ) - print( - "> get_set_bits_count_using_brian_kernighans_algorithm()", - "\tans =", - get_set_bits_count_using_brian_kernighans_algorithm(58), - "\ttime =", - timeit( - "z.get_set_bits_count_using_brian_kernighans_algorithm(58)", - setup="import __main__ as z", - ), - "seconds", - ) + if __name__ == "__main__": import doctest From 97592357a37e9887b4ed2478512bef584b897be0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 09:49:23 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/count_number_of_one_bits.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 787a4a3b6e3c..b3d1368255bb 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -59,10 +59,12 @@ def get_set_bits_count_using_modulo_operator(number: int) -> int: number = number >> 1 return result + def benchmark() -> None: """ Benchmark code for comparing 2 functions, with different length int values. """ + def do_benchmark(number: int) -> None: setup = "import __main__ as z" print(f"Benchmark when {number = }:") @@ -81,11 +83,6 @@ def do_benchmark(number: int) -> None: print() - - - - - if __name__ == "__main__": import doctest From fd2379b6be10b5f553ae1c8e5167844fffd2ece1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 24 Oct 2022 12:01:24 +0200 Subject: [PATCH 08/10] Run the tests before running the benchmarks --- bit_manipulation/count_number_of_one_bits.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index b3d1368255bb..3a0d426728db 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -56,15 +56,15 @@ def get_set_bits_count_using_modulo_operator(number: int) -> int: while number: if number % 2 == 1: result += 1 - number = number >> 1 + number >>= 1 return result def benchmark() -> None: """ Benchmark code for comparing 2 functions, with different length int values. + Brian Kernighan's algorithm is consistantly faster than using modulo_operator. """ - def do_benchmark(number: int) -> None: setup = "import __main__ as z" print(f"Benchmark when {number = }:") @@ -86,5 +86,5 @@ def do_benchmark(number: int) -> None: if __name__ == "__main__": import doctest - benchmark() doctest.testmod() + benchmark() From 922dbf9b6afcef74fb5f1c0690c29b30080ce030 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Oct 2022 10:03:00 +0000 Subject: [PATCH 09/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/count_number_of_one_bits.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 3a0d426728db..e8821cebf103 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -65,6 +65,7 @@ def benchmark() -> None: Benchmark code for comparing 2 functions, with different length int values. Brian Kernighan's algorithm is consistantly faster than using modulo_operator. """ + def do_benchmark(number: int) -> None: setup = "import __main__ as z" print(f"Benchmark when {number = }:") From ccf2b6a21b7b8856f07fba5e9ff14ae0799e97a8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 24 Oct 2022 12:06:17 +0200 Subject: [PATCH 10/10] consistently --- bit_manipulation/count_number_of_one_bits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index e8821cebf103..a1687503a383 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -63,7 +63,7 @@ def get_set_bits_count_using_modulo_operator(number: int) -> int: def benchmark() -> None: """ Benchmark code for comparing 2 functions, with different length int values. - Brian Kernighan's algorithm is consistantly faster than using modulo_operator. + Brian Kernighan's algorithm is consistently faster than using modulo_operator. """ def do_benchmark(number: int) -> None: