Online Book Reader

Home Category

Beautiful Code [232]

By Root 5141 0
avoided. Code such as the previous example therefore runs without an exception.

Python's Dictionary Implementation: Being All Things to All People > Conclusion

18.6. Conclusion

Despite the many features and options presented by Python dictionaries, and their widespread use internally, the CPython implementation is still mostly straightforward. The optimizations that have been done are largely algorithmic, and their effects on collision rates and on benchmarks have been tested experimentally where possible. To learn more about the dictionary implementation, the source code is your best guide. First, read the Objects/dictnotes.txt file at http://svn.python.org/view/python/trunk/Objects/dictnotes.txt?view=markup for a discussion of the common use cases for dictionaries and of various possible optimizations. (Not all the approaches described in the file are used in the current code.) Next, read the Objects/dictobject.c source file at http://svn.python.org/view/python/trunk/Objects/dictobject.c?view=markup.

You can get a good understanding of the issues by reading the comments and taking an occasional clarifying glance at the code.

Python's Dictionary Implementation: Being All Things to All People > Acknowledgments

18.7. Acknowledgments

Thanks to Raymond Hettinger for his comments on this chapter. Any errors are my own.

Multidimensional Iterators in NumPy > Key Challenges in N-Dimensional Array Operations

19. Multidimensional Iterators in NumPy

Travis E. Oliphant

Numpy is an optional package for the python language that provides a powerful N-dimensional array object. An N-dimensional array is a data structure that uses N integers, or indices, to access individual elements. It is a useful model for a wide variety of data processed by a computer.

For example, a one-dimensional array can store the samples of a sound wave, a two-dimensional array can store a grayscale image, a three-dimensional array can store a color image (with one of the dimensions having a length of 3 or 4), and a four-dimensional array can store the value of pressure in a room during a concert. Even higher-dimensional arrays are often useful.

NumPy provides an environment for the mathematical and structural manipulation of arrays of arbitrary dimensions. These manipulations are at the heart of much scientific, engineering, and multimedia code that routinely deals with large amounts of data. Being able to perform these mathematical and structural manipulations in a high-level language can considerably simplify the development and later reuse of these algorithms.

NumPy provides an assortment of mathematical calculations that can be done on arrays, as well as providing very simple syntax for structural operations. As a result, Python (with NumPy) can be successfully used for the development of significant and fast-performing engineering and scientific code.

One feature that allows fast structural manipulation is that any subregion of a NumPy array can be selected using the concept of slicing. In Python, a slice is defined by a starting index, a stopping index, and a stride, using the notation start:stop:stride (inside square brackets).

For example, suppose we want to crop and shrink a 656x498 image to a 160x120 image by selecting a region in the middle of the image. If the image is held in the NumPy array, im, this operation can be performed using:

im2=im[8:-8:4, 9:-9:4]

An important feature of NumPy, however, is that the new image selected in this way actually shares data with the underlying image. A copy is not performed. This can be an important optimization when calculating with large data sets where indiscriminate copying can overwhelm the computing resources.

19.1. Key Challenges in N-Dimensional Array Operations

In order to provide fast implementations of all mathematical operations, NumPy implements loops (in C) that work quickly over an array or several arrays of any number of dimensions. Writing such generic code that works quickly on arrays of arbitrary dimension can be a mind-stretching task. It may be easy to

Return Main Page Previous Page Next Page

®Online Book Reader