From 22b433c70419d44a873d4c591574389676bb1952 Mon Sep 17 00:00:00 2001 From: mostafaibrahim17 Date: Sat, 8 Aug 2026 14:46:57 +0300 Subject: [PATCH 1/5] Add materials for "Python Performance Optimization: A Practical Guide" --- python-performance-guide/step-1.py | 36 ++++++++++++++++++++++++++++++ python-performance-guide/step-2.py | 17 ++++++++++++++ python-performance-guide/step-3.py | 30 +++++++++++++++++++++++++ python-performance-guide/step-4.py | 33 +++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 python-performance-guide/step-1.py create mode 100644 python-performance-guide/step-2.py create mode 100644 python-performance-guide/step-3.py create mode 100644 python-performance-guide/step-4.py diff --git a/python-performance-guide/step-1.py b/python-performance-guide/step-1.py new file mode 100644 index 0000000000..7f0113462f --- /dev/null +++ b/python-performance-guide/step-1.py @@ -0,0 +1,36 @@ +import timeit + + +def count_pairs_slow(numbers, target): + count = 0 + for i in range(len(numbers)): + for j in range(i + 1, len(numbers)): + if numbers[i] + numbers[j] == target: + count += 1 + return count + + +sample = list(range(5_000)) +target = 4_999 + +print(count_pairs_slow(sample, target)) + +slow_time = timeit.timeit(lambda: count_pairs_slow(sample, target), number=10) +print(f"count_pairs_slow: {slow_time:.4f} seconds") + + +def count_pairs_fast(numbers, target): + seen = set() + count = 0 + for number in numbers: + complement = target - number + if complement in seen: + count += 1 + seen.add(number) + return count + + +print(count_pairs_fast(sample, target)) + +fast_time = timeit.timeit(lambda: count_pairs_fast(sample, target), number=10) +print(f"count_pairs_fast: {fast_time:.4f} seconds") diff --git a/python-performance-guide/step-2.py b/python-performance-guide/step-2.py new file mode 100644 index 0000000000..a95dddf053 --- /dev/null +++ b/python-performance-guide/step-2.py @@ -0,0 +1,17 @@ +import timeit + +numbers_list = list(range(100_000)) + +target = 99_999 + +list_time = timeit.timeit(lambda: target in numbers_list, number=1000) + +print(target in numbers_list) +print(f"list membership: {list_time:.8f} seconds") + +numbers_set = set(numbers_list) + +set_time = timeit.timeit(lambda: target in numbers_set, number=1000) + +print(target in numbers_set) +print(f"set membership: {set_time:.8f} seconds") diff --git a/python-performance-guide/step-3.py b/python-performance-guide/step-3.py new file mode 100644 index 0000000000..37d5e8107c --- /dev/null +++ b/python-performance-guide/step-3.py @@ -0,0 +1,30 @@ +import timeit +from collections import Counter + +words = ["apple", "banana", "apple", "cherry", "banana", "apple"] * 1000 + + +def manual_count(items): + counts = {} + for item in items: + if item in counts: + counts[item] += 1 + else: + counts[item] = 1 + return counts + + +print(manual_count(words)) + +manual_time = timeit.timeit(lambda: manual_count(words), number=1000) +print(f"manual loop: {manual_time:.4f} seconds") + + +def counter_count(items): + return Counter(items) + + +print(counter_count(words)) + +counter_time = timeit.timeit(lambda: counter_count(words), number=1000) +print(f"collections.Counter: {counter_time:.4f} seconds") diff --git a/python-performance-guide/step-4.py b/python-performance-guide/step-4.py new file mode 100644 index 0000000000..dc08a5fec9 --- /dev/null +++ b/python-performance-guide/step-4.py @@ -0,0 +1,33 @@ +import timeit +from functools import cache + +numbers = list(range(2_000_000)) + + +def find_index(target): + for i, value in enumerate(numbers): + if value == target: + return i + return -1 + + +target = 1_999_999 + +print(find_index(target)) + +slow_time = timeit.timeit(lambda: find_index(target), number=20) +print(f"without cache: {slow_time:.4f} seconds") + + +@cache +def find_index_cached(target): + for i, value in enumerate(numbers): + if value == target: + return i + return -1 + + +print(find_index_cached(target)) + +fast_time = timeit.timeit(lambda: find_index_cached(target), number=20) +print(f"with cache: {fast_time:.8f} seconds") From fa829691c9bdbd1898e7f9d719fc8ca9fd57368e Mon Sep 17 00:00:00 2001 From: mostafaibrahim17 Date: Sat, 8 Aug 2026 14:51:55 +0300 Subject: [PATCH 2/5] folder name changed to match slug --- .../step-1.py | 0 .../step-2.py | 0 .../step-3.py | 0 .../step-4.py | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename {python-performance-guide => python-performance-optimization-guide}/step-1.py (100%) rename {python-performance-guide => python-performance-optimization-guide}/step-2.py (100%) rename {python-performance-guide => python-performance-optimization-guide}/step-3.py (100%) rename {python-performance-guide => python-performance-optimization-guide}/step-4.py (100%) diff --git a/python-performance-guide/step-1.py b/python-performance-optimization-guide/step-1.py similarity index 100% rename from python-performance-guide/step-1.py rename to python-performance-optimization-guide/step-1.py diff --git a/python-performance-guide/step-2.py b/python-performance-optimization-guide/step-2.py similarity index 100% rename from python-performance-guide/step-2.py rename to python-performance-optimization-guide/step-2.py diff --git a/python-performance-guide/step-3.py b/python-performance-optimization-guide/step-3.py similarity index 100% rename from python-performance-guide/step-3.py rename to python-performance-optimization-guide/step-3.py diff --git a/python-performance-guide/step-4.py b/python-performance-optimization-guide/step-4.py similarity index 100% rename from python-performance-guide/step-4.py rename to python-performance-optimization-guide/step-4.py From c287bf5e5651be62ef70b471811fa65257fa02b4 Mon Sep 17 00:00:00 2001 From: mostafaibrahim17 Date: Sat, 8 Aug 2026 14:54:46 +0300 Subject: [PATCH 3/5] added README.md --- python-performance-optimization-guide/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 python-performance-optimization-guide/README.md diff --git a/python-performance-optimization-guide/README.md b/python-performance-optimization-guide/README.md new file mode 100644 index 0000000000..d780ecf82e --- /dev/null +++ b/python-performance-optimization-guide/README.md @@ -0,0 +1,3 @@ +# Python Performance Optimization: A Practical Guide + +This folder provides the code examples for the Real Python tutorial Python Performance Optimization: A Practical Guide. \ No newline at end of file From abffb3cd6b03d6d7e673f903b63e04236a41c690 Mon Sep 17 00:00:00 2001 From: mostafaibrahim17 Date: Sun, 16 Aug 2026 20:15:29 +0300 Subject: [PATCH 4/5] updated step 1 --- .../step-1.py | 52 +++++++------------ 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/python-performance-optimization-guide/step-1.py b/python-performance-optimization-guide/step-1.py index 7f0113462f..4d85eb0c4c 100644 --- a/python-performance-optimization-guide/step-1.py +++ b/python-performance-optimization-guide/step-1.py @@ -1,36 +1,20 @@ import timeit - -def count_pairs_slow(numbers, target): - count = 0 - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target: - count += 1 - return count - - -sample = list(range(5_000)) -target = 4_999 - -print(count_pairs_slow(sample, target)) - -slow_time = timeit.timeit(lambda: count_pairs_slow(sample, target), number=10) -print(f"count_pairs_slow: {slow_time:.4f} seconds") - - -def count_pairs_fast(numbers, target): - seen = set() - count = 0 - for number in numbers: - complement = target - number - if complement in seen: - count += 1 - seen.add(number) - return count - - -print(count_pairs_fast(sample, target)) - -fast_time = timeit.timeit(lambda: count_pairs_fast(sample, target), number=10) -print(f"count_pairs_fast: {fast_time:.4f} seconds") +def calculate_order_total(items): + total = 0 + for item in items: + total = total + item["price"] * item["quantity"] + return total + +order = [ + {"price": 19.99, "quantity": 3}, + {"price": 5.50, "quantity": 10}, + {"price": 42.00, "quantity": 1}, +] * 100 + +print(calculate_order_total(order)) + +order_time = timeit.timeit( + lambda: calculate_order_total(order), number=500_000 +) +print(f"calculate_order_total: {order_time:.4f} seconds") \ No newline at end of file From a25e56c53830040babc3ad990b5ca58c94b6a4cd Mon Sep 17 00:00:00 2001 From: mostafaibrahim17 Date: Sun, 16 Aug 2026 22:28:47 +0300 Subject: [PATCH 5/5] updated step 3 --- python-performance-optimization-guide/step-3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-performance-optimization-guide/step-3.py b/python-performance-optimization-guide/step-3.py index 37d5e8107c..2310ab222d 100644 --- a/python-performance-optimization-guide/step-3.py +++ b/python-performance-optimization-guide/step-3.py @@ -1,5 +1,4 @@ import timeit -from collections import Counter words = ["apple", "banana", "apple", "cherry", "banana", "apple"] * 1000 @@ -19,6 +18,7 @@ def manual_count(items): manual_time = timeit.timeit(lambda: manual_count(words), number=1000) print(f"manual loop: {manual_time:.4f} seconds") +from collections import Counter def counter_count(items): return Counter(items)