Что дальше?
Мелочи и подчистка
eval() и exec(), полная версия (simple_stmts.html, functions.html, functions.html)
«TypeError: media_is_eligible() takes at least 2 arguments (2 given)»
- …
Вопросы быстродействия
- 1 ms vs. 1 µs
- Быстродействие и ЗБЧ
- Быстродействие и вычислительная сложность
- Быстродействие и дзен Python (см. далее)
- Действительные случаи потери быстродействия
Python Patterns - An Optimization Anecdote (довольно старое эссе Гвидо, здесь он ещё любил reduce() ):
Rule number one: only optimize when there is a proven speed bottleneck. Only optimize the innermost loop. (This rule is independent of Python, but it doesn't hurt repeating it, since it can save a lot of work. )
- Small is beautiful. Given Python's hefty charges for bytecode instructions and variable look-up, it rarely pays off to add extra tests to save a little bit of work.
Use intrinsic operations. An implied loop in map() is faster than an explicit for loop; a while loop with an explicit loop counter is even slower.
- Avoid calling functions written in Python in your inner loop. This includes lambdas. In-lining the inner loop can save a lot of time.
- Local variables are faster than globals; if you use a global constant in a loop, copy it to a local variable before the loop. And in Python, function names (global or built-in) are also global constants!
Try to use map(), filter() or reduce() to replace an explicit for loop, but only if you can use a built-in function: map with a built-in function beats for loop, but a for loop with in-line code beats map with a lambda function!
- Check your algorithms for quadratic behavior. But notice that a more complex algorithm only pays off for large N - for small N, the complexity doesn't pay off. In our case, 256 turned out to be small enough that the simpler version was still a tad faster. Your mileage may vary - this is worth investigating.
And last but not least: collect data. Python's excellent profile module can quickly show the bottleneck in your code. if you're considering different versions of an algorithm, test it in a tight loop using the time.clock() function.
Разбор каких-то задач?
Дзен Python
import this:
The Zen of Python, by Tim Peters (structured by FrBrGeorge CO)
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.
- Special cases aren't special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one — and preferably only one — obvious way to do it.
- Although that way may not be obvious at first unless you're Dutch.
- Now is better than never.
Although never is often better than right now.
- If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea -- let's do more of those!
Дальнейшее движение
- Изучение алгоритмического программирования на Python:
Проект «Interactivepython»: Problem Solving with Algorithms and Data Structures и перевод (!): http://aliev.me/runestone/ (via Иван Морозов)
- Инструментальная разработка и промышленное программирование
- «Академическое» программирование
- Сам Python и его реализации
EE: import antigravity