Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Created July 14, 2021 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artturijalli/1c19a2fadb6a1af3f0b77f36524a5f3d to your computer and use it in GitHub Desktop.
Save artturijalli/1c19a2fadb6a1af3f0b77f36524a5f3d to your computer and use it in GitHub Desktop.
Comparing performance of a for loop + yield and a generator expression. In reality this is a meaningless comparison because both instantly return a generator object.
import matplotlib.pyplot as plt
import timeit
# Initialize a list with 1M numbers
numbers = [i for i in range(0, 1000000)]
# Create a new list by squaring the numbers with for loop
def for_loop():
for num in numbers:
yield num ** 2
# Create a new list by squaring the numbers with set comprehension
def comprehension():
return (num ** 2 for num in numbers)
# Compute the runtime of a function
def measure_runtime(func, n_times):
total_runtime = 0.0
for i in range(n_times):
start = timeit.default_timer()
func()
stop = timeit.default_timer()
total_runtime += stop - start
return total_runtime / n_times
n_runs = 10
# Compute runtimes for both for loop and list comprehension approaches
loop_average = measure_runtime(for_loop, n_runs)
comprehension_average = measure_runtime(comprehension, n_runs)
print(
f"For loop yileds average runtime {loop_average} with {n_runs} iterations")
print(
f"Comprehension yileds average runtime {comprehension_average} with {n_runs} iterations")
fig, ax = plt.subplots()
approaches = ['For loop', 'Comprehension']
runtimes = [loop_average, comprehension_average]
rects = ax.bar(approaches, runtimes)
for rect, label in zip(rects, runtimes):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width() / 2,
height, label, ha='center', va='bottom')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment