[SciPy-user] [newb] how to create arrays

Bill Baxter wbaxter at gmail.com
Thu Jan 3 03:29:36 EST 2008


On Jan 3, 2008 4:56 PM, Anne Archibald <peridot.faceted at gmail.com> wrote:
> The main reason for this is conceptual clarity: you can start thinking
> of array operations as single steps in your program, allowing you to
> do more with the same size of program. Secondarily, using the numpy
> functions allows the looping to be done in compiled code, which is
> much faster than python code. If you are filling an array, element by
> element, with a loop in python, the time the python code takes to run
> will be much longer than the time spend initializing with zeros. Thus
> numpy.empty() gets much less use than you might expect. Look into
> using numpy functions - linspace, arange, zeros, ones,
> exp/sin/arctan/etc. to create your array.

That's interesting.  I'd put it almost exactly backwards.  Efficiency
is the #1 reason for arrays, and as a bonus it happens that *some*
things are shorter and cleaner expressed as array operations.  I've
been back to using a compiled language for a stint and it's absolute
bliss to just be able to write a blasted for-loop when I want to,
without worrying about my program slowing to a crawl.  As opposed to
always having to think of some clever way to use the array machinery
to achieve the same thing efficiently (and sometimes having to think
of two or three different ways when the first one doesn't turn out to
be fast enough).

But I still fire up matplotlib and hack up some quick NumPy scripts
whenever I want to take a look at the numbers coming out of my
compiled code. :-)

--bb



More information about the SciPy-User mailing list