16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 17<br />

■ ■ ■<br />

Extending <strong>Python</strong><br />

You can implement anything in <strong>Python</strong>, really; it’s a powerful language, but sometimes it can<br />

get a bit . . . <strong>to</strong>o slow. For example, if you’re writing a scientific simulation of some form of<br />

nuclear reaction, or you’re rendering the graphics for the next Star Wars movie (wait—there<br />

won’t be any more now, will there?), writing the high-performance code in <strong>Python</strong> will probably<br />

not be a good choice. <strong>Python</strong> is meant <strong>to</strong> be easy <strong>to</strong> work with, and <strong>to</strong> help make the development<br />

fast. The flexibility needed for this comes with a hefty price in terms of efficiency. It’s certainly<br />

fast enough for most common programming tasks, but if you need real speed, languages such<br />

as C, C++, and Java can usually beat it by several orders of magnitude.<br />

Now, I don’t want <strong>to</strong> encourage the speed freaks among you <strong>to</strong> start developing exclusively<br />

in C; remember, this may speed up the program itself, but it will most certainly slow down your<br />

programming. So you need <strong>to</strong> consider what is most important: getting the program done quickly,<br />

or eventually (in the distant future) getting a program that runs really, really fast. If <strong>Python</strong> is<br />

fast enough, the extra pain involved will make using a low-level language such as C something<br />

of a meaningless choice (unless you have other requirements, such as running on an embedded<br />

device that doesn’t have room for <strong>Python</strong>, or something like that).<br />

This chapter deals with the cases where you do need extra speed. The best solution then<br />

probably isn’t <strong>to</strong> switch entirely <strong>to</strong> C (or some other low- or mid-level language); instead, I<br />

recommend the following approach, which has worked for plenty of industrial-strength speed<br />

freaks out there (in one form or another):<br />

1. Develop a pro<strong>to</strong>type in <strong>Python</strong>. (See Chapter 19 for some material on pro<strong>to</strong>typing.)<br />

2. Profile your program and determine the bottlenecks. (See Chapter 16 for some material<br />

on testing.)<br />

3. Rewrite the bottlenecks as a C (or C++, C#, Java, Fortran, etc.) extension.<br />

The resulting architecture—a <strong>Python</strong> framework with one or more C components—is a<br />

very powerful one, because it combines the best of two worlds. It’s a matter of choosing the<br />

right <strong>to</strong>ols for each job. It affords you the benefits of developing a complex system in a high-level<br />

language (<strong>Python</strong>), and it lets you develop your smaller (and presumably simpler) speed-critical<br />

components in a low-level language (C).<br />

If you have some knowledge of what the bottlenecks of your system will be even before you<br />

begin, you can (and probably should) design your pro<strong>to</strong>type so that replacing the critical parts<br />

is easy. I think I might as well state this in the form of a tip:<br />

357

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!