1.3 Okay, But What's the Downside?
Perhaps the only downside
to Python is
that, as currently implemented, its execution speed may not always be
as fast as compiled languages such as C and C++.
We'll talk about implementation concepts later in
this book. But in short, the standard implementations of Python today
compile (i.e., translate) source code statements to an intermediate
format known as byte code, and then interpret
the byte code.
Byte code provides portability, as it is a platform-independent
format. However, because Python is not compiled all the way down to
binary machine code (e.g., instructions for an Intel chip), some
programs will run more slowly in Python than in a fully compiled
language like C.
Whether you will ever care about the execution
speed difference depends on what kinds of programs you write. Python
has been optimized numerous times, and Python code runs fast enough
by itself in most application domains. Furthermore, whenever you do
something "real" in a Python
script, like process a file or construct a GUI, your program is
actually running at C speed since such tasks are immediately
dispatched to compiled C code inside the Python interpreter. More
fundamentally, Python's speed-of-development gain is
often far more important than any speed-of-execution loss, especially
given modern computer speeds.
Even at today's CPU speeds there still are some
domains that do require optimal execution speed. Numeric programming
and animation, for example, often need at least their core
number-crunching components to run at C speed (or better). If you
work in such a domain, you can still use Python—simply split
off the parts of the application that require optimal speed into
compiled
extensions
, and link those
into your system for use in Python scripts.
We won't talk about extensions much in this text,
but this is really just an instance of the Python-as-control-language
role that we discussed earlier. A prime example of this dual language
strategy is the NumPy numeric programming
extension for Python; by combining compiled and
optimized numeric extension libraries with the Python language, NumPy
turns Python into a numeric programming tool that is both efficient
and easy to use. You may never need to code such extensions in your
own Python work, but they provide a powerful optimization mechanism
if you ever do.
|