天天看点

Python优化轶事

Python Patterns - An Optimization Anecdote

The golden rule of optimization: don’t optimize unless you know you need to, and measure rather than guessing.

  • Rule number one: only optimize when there is a proven speed bottleneck
  • Small is beautiful
  • Use intrinsic operations
  • Avoid calling functions written in Python in your inner loop
  • Local variables are faster than globals
  • Try to use map(), filter() or reduce() to replace an explicit for loop, but only if you can use a built-in function
  • Check your algorithms for quadratic behavior
  • And last but not least: collect data

附上链接:https://www.python.org/doc/essays/list2str/