Appendix A
Pedal to the Metal: Accelerating
Python
Make it work, make it right, make it fast.
— Kent Beck
This appendix is a tiny peek at some of the options for tweaking the constant factors of your implementations.
Although this kind of optimization in many cases won’t take the place of proper algorithm design—especially if your
problems can grow large—making your program run ten times as fast can indeed be useful.
Before calling for external help, you should make sure you’re using Python’s built-in tools to their full potential.
I’ve given you some pointers throughout the book, including the proper uses of list versus deque and how bisect
and heapq can give you a great performance boost under the right circumstances. As a Python programmer, you’re
also lucky enough to have easy access to one of the most advanced and efficient (and efficiently implemented) sorting
algorithms out there (list.sort), as well as a really versatile and fast hash table (dict). You might even find that
itertools and functools can give your code a performance boost.
1
Also, when choosing your technology, make sure you optimize only what you must. Optimizations do tend to
make either your code or your tool setup more complicated, so make sure it’s worth it. If your algorithm scales “well
enough” and your code is “fast enough,” introducing the extension modules in another language such as C might not
be worth it. What is enough is, of course, up to you to determine. (For some hints on timing and profiling your code,
see Chapter 2.)
Note that the packages and extensions discussed in this appendix are mainly about optimizing single-processor
code, either by providing efficiently implemented functionality, by letting you create or wrap extension modules, or
by simply speeding up your Python interpreter. Distributing your processing to multiple cores and processors can
certainly also be a big help. The multiprocessing module can be a place to start. If you want to explore this approach,
you should be able to find a lot of third-party tools for distributed computing as well. For example, you could have
a look at the Parallel Processing page in the Python Wiki.
In the following pages, I describe a selection of acceleration tools. There are several efforts in this area, and the
landscape is of course a changing one: New projects appear from time to time, and some old ones fade and die. If you
think one of these solutions sounds interesting, you should check out its web site and consider the size and activity of
its community—as well, of course, as your own needs. For web site URLs, see Table
A-1
later in the appendix.
Do'stlaringiz bilan baham: |