[Numpy-discussion] Numeric memory leak when building Numeric.array from numarray.array

Francesc Altet faltet at carabos.com
Mon Dec 11 09:33:18 CST 2006


El dl 11 de 12 del 2006 a les 14:16 +0100, en/na Alexandre Fayolle va
escriure:
> > > I can work around this by using an intermediate string representation:
> > > 
> > > temp = Numeric.fromstring(atest.tostring(), atest.typecode())
> > > temp.shape = atest.shape
> > 
> > Another (faster) workaround would be:
> > 
> > temp2 = Numeric.fromstring(atest._data, typecode=atest.typecode())
> 
> Nice! 

Well, I've to say that this approach only work for contiguous,
non-offseted arrays, as can be seen in:

In [59]:atest = numarray.arange(10)
In [60]:Numeric.fromstring(atest[5:]._data, typecode=atest.typecode())
Out[60]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],'i')  # wrong!
In [61]:Numeric.fromstring(atest[5:].tostring(), atest.typecode())
Out[61]:array([5, 6, 7, 8, 9],'i')                 # good
In [62]:Numeric.fromstring(atest[::2]._data, typecode=atest.typecode())
Out[62]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],'i')  # wrong!
In [63]:Numeric.fromstring(atest[::2].tostring(), atest.typecode())
Out[63]:array([0, 2, 4, 6, 8],'i')                 # good

So, be careful when using it. I'd rather keep using your approach, which
is the faster one that is completely general.

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth



More information about the Numpy-discussion mailing list