Passing back an array from an extension module?

Bob Greschke bob at passcal.nmt.edu
Wed Jun 13 19:02:40 EDT 2001


"Chris Barker" <chrishbarker at home.net> wrote in message
news:3B214BCF.8393A2AC at home.net...
> 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.

All right, stop twisting my arm. :-)  I'll give it a try.  Actually,
the initial reading of the file in C goes very quickly.

> > 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?

I can run everything on the same file and it eventually crashes on
about the 6th or 7th reading.  I am free()ing up the C array, but I
suspect that I have to do something with the reference count of the
tuple that I create.  Maybe?  I'm still not clear on all of that.

> > 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.

It's the same file each time, so it should get faster, but then it
crashes.

> 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.

I'll still need to call the C stuff to read, and decode, the file.

Bob






More information about the Python-list mailing list