Passing back an array from an extension module?

Chris Barker chrishbarker at home.net
Sat Jun 9 00:03:59 CEST 2001


Bob Greschke wrote:
> Hmmm...I was wrong.  Creating a tuple object, then creating long
> objects and adding them to the tuple 1,600,000 times doesn't take very
> long.  On
> the order of about 5 seconds (on a Sun Ultra10).

It would probably be a whole lot faster with a NumPy Array, and a lot
cleaner, you don't have to create long objects, just make a NumPy array
with your C array directly. But 5 seconds is probably a lot faster than
it takes to read from disk anyway.

> Well, I'm not actually going to be doing any math on the values in the
> array, not in an array-wide sense anyway.

NumPy arrays are pretty handy for other things as well, they are
mutable, memory efficient, have more available types, etc.

> But it takes it
> about 10 seconds if I do the same thing a 2nd, 3rd, 4th, etc. time.
> Is it leaking memory,

Check your memory use. With tuples of this size, you should notice real
quick! Have you freed up the C array the you allocate?

> or is it just taking that long to garbage
> collect the old tuple?  There is a lot of more disk activity on the
> sebsequent runs than there is the first time through.

Swapping? this would be explained by the memory leak option. Maybe the
first file was in the disk cache already.

I'm still curious whether you could do the whole thing in NumPy, and
forget all this C stuff altogether. Once you are using NumPy, you really
don't need to write C very often at all.

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list