Tham khảo tài liệu 'dive into python-chapter 18. performance tuning', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Chapter 18. Performance Tuning Performance tuning is a many-splendored thing. Just because Python is an interpreted language doesn t mean you shouldn t worry about code optimization. But don t worry about it too much. . Diving in There are so many pitfalls involved in optimizing your code it s hard to know where to start. Let s start here are you sure you need to do it at all Is your code really so bad Is it worth the time to tune it Over the lifetime of your application how much time is going to be spent running that code compared to the time spent waiting for a remote database server or waiting for user input Second are you sure you re done coding Premature optimization is like spreading frosting on a half-baked cake. You spend hours or days or more optimizing your code for performance only to discover it doesn t do what you need it to do. That s time down the drain. This is not to say that code optimization is worthless but you need to look at the whole system and decide whether it s the best use of your time. Every minute you spend optimizing code is a minute you re not spending adding new features or writing documentation or playing with your kids or writing unit tests. Oh yes unit tests. It should go without saying that you need a complete set of unit tests before you begin performance tuning. The last thing you need is to introduce new bugs while fiddling with your algorithms. With these caveats in place let s look at some techniques for optimizing Python code. The code in question is an implementation of the Soundex algorithm. Soundex was a method used in the early 20th century for categorizing surnames in the United States census. It grouped similarsounding names together so even if a name was misspelled researchers had a chance of finding it. Soundex is still used today for much the same reason although of course we use computerized database servers now. Most database servers include a Soundex function. There are several subtle variations of the .