Choosing a Programming Language | 53
Patterns and
Domains
pass allocates 26 bytes of memory, this program terminated after nearly 15.9
gigabytes ofallocated memory on a machine with 16 gigabytes of available
memory. To properly release memory once it is no longer needed (as determined
by the programmer), the program mustfreethe memory, which returns the allo-
cated memory to the Heap for future use.
Choosing a Programming Language
Throughout this book we use a variety of programming languages to illustrate
the algorithms. No single language is the right one to use in all circumstances.
Too often, a specific programming language is used simply because it was used
on a similar project. Since you are interested in algorithms, it is likely that you
also want to ensure that your implementations run as fast as possible. This level
of fine-tuning or optimization is beyond the scope of this book, though we
describe several instances where carefully designed code optimizations result in
impressive performance benefits. Choosing a language often depends on a
number of factors:
Garbage collection versus manual memory allocation
In the previous section we described low-level details about the way infor-
mation is stored as a C program executes. Using the standard memory
allocation packages available, most C programmers are used to allocating
memory as needed, and freeing it when done. An alternative approach is to
use a language such as Java or Scheme that provides built-in garbage collec-
tion to manage allocated memory. Garbage collection technology is
increasingly efficient, and there are existing packages available to enable
even C programs to integrate garbage collection with the default memory
allocation schemes.
Bytecode interpretation versus compiled code
The common perception is that compiled code will outperform interpreted
code every time. In Java, for example, the Java compiler produces byte code
that is interpreted and executed by the JVM. You should seriously consider
whether using a language such as Java will improve the understanding of the
code as it is written, even though there may be a runtime penalty in using that
code.
Dynamic versus static typing
Statically typed languages enforce the rules of types to detect errors during
compilation, which should improve productivity because errors are caught
immediately rather than at runtime. For strongly typed functional languages,
such as ML, there is a common perception in the functional language
community that most defects can be prevented by the proper application of
the type system. Dynamic typed languages are often interpreted and variable
values are known only at runtime, and therefore cannot be checked stati-
cally. Many scripting languages offer dynamic typing.
Algorithms in a Nutshell
Algorithms in a Nutshell By Gary Pollice, George T. Heineman, Stanley Selkow ISBN:
9780596516246 Publisher: O'Reilly Media, Inc.
Prepared for Ming Yi, Safari ID: [email protected]
Licensed by Ming Yi
Print Publication Date: 2008/10/21 User number: 594243
© 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other use
Licensed by
Ming Yi