Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Last active July 14, 2021 10:28
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/2e657b67f34fc344bdad07aaa5ae6512 to your computer and use it in GitHub Desktop.
Save artturijalli/2e657b67f34fc344bdad07aaa5ae6512 to your computer and use it in GitHub Desktop.
Python for loop vs list comprehension runtime analysis on squaring a list of numbers with both approaches
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():
squared_nums = []
for num in numbers:
squared_nums.append(num ** 2)
# Create a new list by squaring the numbers with list comprehension
def comprehension():
squared_nums = [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