Skip to content

Instantly share code, notes, and snippets.

Created July 14, 2021 10:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import matplotlib.pyplot as plt
import timeit
# Initialize a list with 1M numbers
numbers = [i for i in range(0, 1000000)]
# Create a new dictionary of {number: number squared} pairs by using a for loop
def for_loop():
squared_nums = {}
for num in numbers:
squared_nums[num] = num ** 2
# Create a new list by squaring the numbers with set comprehension
def comprehension():
squared_nums = {num: 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()
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)
f"For loop yileds average runtime {loop_average} with {n_runs} iterations")
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 =, 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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment