[Numpy-discussion] Efficiently converting a numarray object to Numeric
Todd Miller
jmiller at stsci.edu
Wed Feb 9 08:38:13 EST 2005
On Wed, 2005-02-09 at 11:27 -0500, Todd Miller wrote:
> On Wed, 2005-02-09 at 16:37 +0100, Francesc Altet wrote:
> > Hi,
> >
> > I'm wondering if there is a more efficient way to convert a numarray
> > object into a Numeric one than:
> >
> > >>> import Numeric
> > >>> import numarray
> > >>> from time import time
> > >>> a = numarray.arange(100*1000)
> > >>> t1=time();b=Numeric.array(a);time()-t1
> > 0.43448090553283691
> >
> > Any suggestions?
>
> Yes!
>
> import Numeric
> import numarray
> from time import time
>
> a = numarray.arange(100*1000)
>
> t1=time();b=Numeric.array(a);
> print time()-t1
>
> t1 = time(); c=Numeric.fromstring(a._data, typecode='i')
> print time()-t1
>
> print b[-10:]
> print c[-10:]
>
> I get:
>
> 1.58109998703
> 0.00659704208374
> [99990 99991 99992 99993 99994 99995 99996 99997 99998 99999]
> [99990 99991 99992 99993 99994 99995 99996 99997 99998 99999]
>
> Note that in CVS, fromstring(a, typecode='i') now also works because
> numarray now supplies the buffer protocol as well as consuming buffers.
>
One more thing is necessary to handle a misbehaved numarray and make its
buffer usable by Numeric:
if not a.is_c_array():
a = a.copy()
Regards,
Todd
More information about the NumPy-Discussion
mailing list