Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-performance-optimization-guide/README.md
Original file line number Diff line number Diff line change
@@ -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.
36 changes: 36 additions & 0 deletions python-performance-optimization-guide/step-1.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 17 additions & 0 deletions python-performance-optimization-guide/step-2.py
Original file line number Diff line number Diff line change
@@ -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")
30 changes: 30 additions & 0 deletions python-performance-optimization-guide/step-3.py
Original file line number Diff line number Diff line change
@@ -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")
33 changes: 33 additions & 0 deletions python-performance-optimization-guide/step-4.py
Original file line number Diff line number Diff line change
@@ -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")
Loading