From 22b433c70419d44a873d4c591574389676bb1952 Mon Sep 17 00:00:00 2001 From: mostafaibrahim17 Date: Sat, 8 Aug 2026 14:46:57 +0300 Subject: [PATCH 1/3] 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/3] 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/3] 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