Help mi please with optimalization of simple Python code

Jeff Epler jepler at unpythonic.net
Tue Oct 1 12:21:13 EDT 2002


You should probably use the "array" module.

You seem to have an array which is made up of 2-byte unsigned integers.
Well, try this:
    import array
    
    range_maxY = range(maxY)
    range_maxX = range(maxX)
    for y in range_maxY:
	line = array('H').fromstring(f.read(maxX * 2))
	# line.byteswap() if the byteorder is different than your system
	for x in range_maxX:
	    val = line[x]
	    ... process val
You'll probably be a little faster if you write the inner loop as
	for val in line:
	    ... process val
unless the value of x is important.

If you treat all lines uniformly, and the data will all fit into memory, 
then you might want
    contents = array('H').fromfile(f)
    # contents.byteswap() if the byteorder is different than your system
    for val in contents:
	... process val

The Numeric module might also have better tools for performing this
operation.  For instance, if you're simply trying to make a histogram,
the following might be of interest to you:

    Statistics

    The module Scientific.Statistics contains some elementary statistical
    analysis functions.

    The functions average(data), variance(data) and
    standardDeviation(data) all take a sequence of data values and return
    the statistical quantity indicated by their name.

    The submodule Histogram defines a histogram object. Histogram(data,
    number_of_bins) takes a sequence of data and calculates a histogram
    by dividing the range of the data into the specified number of bins
    and counting how many data values fall within each bin interval. An
    optional third argument is a tuple specifying the range to be
    analyzed (lower and upper bound). A histogram is an array of rank
    two representing a sequence of bins, with each bin being described
    by its center and its count.[1]

Jeff
[1] http://starship.python.net/crew/hinsen/part3.html




More information about the Python-list mailing list