Fastest Python First, Tips and Tricks 🏃

📅️ Published: June 27, 2019  • 🕣 1 min read

Here are some tips that I learned to make Python Programs faster (WIP).

  1. Always compile regular expression, if you have to match patterns a lot of times.
import re

pattern = re.compile(some_regular_expression)
some_text = re.sub(pattern, '', data)

  1. Use List comprehension for iterating over a list.
import time

itemlist = [23, 45, 56, 67, 1, 100, 340, 90]
""" Normal Iteration """
start_time = time.time()
for item in itemlist:
	if item % 2 == 0:
		print(item)
end_time = time.time()

print("Without List Comprehension : " + str(end_time - start_time))

""" List Comprehension """
start_time = time.time()
[print(item) for item in itemlist if item % 2 == 0]
end_time = time.time()

print("With List Comprehension : " + str(end_time - start_time))

Output :

56
100
340
90
Without List Comprehension : 0.0002067089080810547
56
100
340
90
With List Comprehension : 0.00019121170043945312

Tired of fucking up with git everyday?

ugit helps you undo git commands with ease. Undo from 20+ git scenarios

ugit cli demo screen