installation via terminal

python -m cProfile -s cumulative myscript.py| https://docs.python.org/3/library/profile.html | profiles a script and sorts by cumulative time |'py_opt_inst1'

Good habits of python programming

Here are some comments:
  1. use vectorized/library operations (e.g. numpy/torch: np.dot, torch.matmul, torch.add) instead of explicit Python loops for better speed and efficiency.
  2. profile before optimizing (cProfile, line_profiler) -- guess-based optimization often targets the wrong bottleneck.
  3. prefer built-in functions and comprehensions (sum(), map(), list/dict/set comprehensions) over manual accumulation loops. True C-implemented builtins like sum()/map() run their iteration loop in C; comprehensions are not that — they still run through the normal bytecode interpreter, just as dedicated, lower-overhead bytecode (no repeated .append attribute lookup, a dedicated append opcode). Both are faster than a manual loop, just for different reasons.

related topics

GPU Optimization — the same profile-first mindset, applied to GPU code instead.
Profiling: perf, cProfile & Flame Graphs — the profiling tools this page assumes you already know.
Python Notes & Cheat Sheet — the Python fundamentals worth having solid before optimizing.

reference

docs.python.org/3/library/profile