20 Proven Python Tricks to Supercharge Your Code Performance
This guide presents twenty practical Python performance tips—from algorithmic complexity and data structure choices to generator usage, loop optimizations, C extensions, parallel processing, and profiling tools—each backed by concrete code examples and timing benchmarks.
1. Optimize Algorithm Time Complexity
Algorithmic time complexity has the greatest impact on execution speed; in Python, choosing appropriate data structures can improve it, e.g., list lookup is O(n) while set lookup is O(1). Common strategies include divide‑and‑conquer, branch‑and‑bound, greedy, and dynamic programming.
2. Reduce Redundant Data
Store large symmetric matrices using only the upper or lower triangle, and represent matrices with many zero elements as sparse matrices.
3. Use copy and deepcopy Wisely
Assigning dicts or lists creates references; to duplicate objects, use copy.copy for shallow copies or copy.deepcopy for deep copies. The latter is slower, as shown by the following benchmark:
import copy
a = range(100000)
%timeit -n10 copy.copy(a)
%timeit -n10 copy.deepcopy(a)4. Use dict or set for Element Lookup
Both dict and set are hash‑table based (like C++11 unordered_map) with O(1) lookup time.
a = range(1000)
s = set(a)
d = dict((i, 1) for i in a)
%timeit -n1000 100 in d
%timeit -n1000 100 in s5. Use Generators and yield
Generators create objects whose memory usage is independent of the resulting list size, offering speed benefits in many cases.
%timeit -n100 a = (i for i in range(100000))
%timeit -n100 b = [i for i in range(100000)]6. Optimize Loops
Move invariant calculations out of loops to halve execution time.
a = range(10000)
size_a = len(a)
%timeit -n1000 for i in a: k = len(a)
%timeit -n1000 for i in a: k = size_a7. Order Multiple Conditional Expressions
For and, place the least‑likely condition first; for or, place the most‑likely condition first.
a = range(2000)
%timeit -n100 [i for i in a if 10<i<20 or 1000<i<2000]
%timeit -n100 [i for i in a if 1000<i<2000 or 10<i<20]8. Use join to Concatenate Strings
Joining an iterator of strings is roughly five times faster than repeated += concatenation.
%timeit -n10000 s = ''
for i in a:
s += i
%timeit -n100000 s = ''.join(a)9. Choose Efficient String Formatting
s1, s2 = 'ax', 'bx'
%timeit -n100000 'abc%s%s' % (s1, s2)
%timeit -n100000 'abc{0}{1}'.format(s1, s2)
%timeit -n100000 'abc' + s1 + s2The % style is the slowest, but differences are small.
10. Swap Variables Without a Temporary
a, b = 1, 2
# traditional swap
c = a; a = b; b = c
%timeit -n10000 a, b = 1, 2; c = a; a = b; b = c
# Pythonic swap
%timeit -n10000 a, b = 1, 2; a, b = b, aThe tuple swap is more than twice as fast.
11. Use if is for Boolean Checks
a = range(10000)
%timeit -n100 [i for i in a if i == True]
%timeit -n100 [i for i in a if i is True] isis nearly twice as fast.
12. Use Chained Comparisons
x, y, z = 1, 2, 3
%timeit -n1000000 if x < y < z: pass
%timeit -n1000000 if x < y and y < z: passChained comparisons are slightly faster and more readable.
13. while 1 vs while True
def while_1():
n = 100000
while 1:
n -= 1
if n <= 0:
break
def while_true():
n = 100000
while True:
n -= 1
if n <= 0:
break
%timeit -n100 while_1()
%timeit -n100 while_true() while 1runs noticeably faster because True is a global variable in Python 2.x.
14. Use ** Instead of pow
%timeit -n10000 c = pow(2, 20)
%timeit -n10000 c = 2**20The exponentiation operator is over ten times faster.
15. Use C‑implemented Packages (cProfile, cStringIO, cPickle)
import cPickle
import pickle
a = range(10000)
%timeit -n100 x = cPickle.dumps(a)
%timeit -n100 x = pickle.dumps(a)C‑based modules provide speedups of an order of magnitude.
16. Choose the Best Deserialization Method
import json
import cPickle
a = range(10000)
s1 = str(a)
s2 = cPickle.dumps(a)
s3 = json.dumps(a)
%timeit -n100 eval(s1)
%timeit -n100 cPickle.loads(s2)
%timeit -n100 json.loads(s3)JSON deserialization is ~3× faster than cPickle and >20× faster than eval.
17. Use C Extensions
Three main approaches enable Python code to call C libraries:
CPython native API : Directly include Python.h and manipulate Python objects in C; flexible but verbose.
ctypes : Wrap existing C libraries for use in pure Python; often the fastest on Python 2.
Cython : A superset of Python that compiles to C; concise syntax, excellent NumPy integration, can yield hundreds‑fold speedups.
cffi : Similar to ctypes but works with both CPython and PyPy; allows writing C code inline.
18. Parallel Programming
Due to the GIL, Python cannot fully exploit multiple cores, but the multiprocessing module provides:
Multi‑process : Suitable for CPU‑bound tasks via Process or Pool.
Multi‑thread : For I/O‑bound tasks using multiprocessing.dummy which mirrors the threading API.
Distributed : Share data across processes with Managers, enabling distributed designs.
19. PyPy – The Ultimate Speed Booster
PyPy, implemented in RPython, uses a Just‑in‑Time compiler and is typically >6× faster than CPython. It retains the GIL but ongoing STM work aims to remove it. Pure‑Python code or cffi extensions benefit most from PyPy.
20. Use Profiling Tools
Beyond timeit, the cProfile module can profile entire scripts: python -m cProfile myscript.py, revealing function call counts and execution times to pinpoint bottlenecks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
