Re: [ctypes-users] copying/slicing ctypes arrays, (c_ulong *n)()
Brought to you by:
theller
From: RayS <ra...@bl...> - 2004-12-16 15:45:53
|
Thanks Florian, My preliminary assessment from trials yesterday (based only on a rough benchmark of idle time) is that for i in range(2000): numArray[pntr+i] = buf[i] is >10x slower than numArray[pntr:pntr+2000] = map(None, buf) which is 25% slower than using array.array() & memmove memmove(array.buffer_info()[0]+pntr, buf) I'll test the direct assignment today, as below, as subsequent operations on numarray should be faster than array. I don't recall why I didn't. The numarray buffer interface is interesting, but I mainly need to do a (deep) copy out of the ctypes memory. Since the main issue is that src (the ctypes obj) is being updated with 2KB A/D data @125x per second (250K samp./s), I need to get it out of the cytpes obj into storage as efficiently as possible. That storage must then work with slicing and FFT. If the ctypes construct buf = (c_ulong * 2000)() supported slicing, I would just leave it there... The small buffer_read thread I made has a time.sleep(.001) for when the A/D buffer is not ready, but even at .001s, it only gets hit once per cycle, if at all. It also seemed that sleep(.0001)==sleep(0); too bad. Thanks, Ray At 10:14 AM 12/16/2004 +0100, Florian Schulze wrote: >On Wed, 15 Dec 2004 20:16:07 -0800, RJ <ra...@sa...> wrote: > >>I'm posting here to see if numarray has a method that could work like array.buffer_info(). numarray.info() returns a text output that can't be used like the array method is to memmove() between ctypes and Python arrays without parsing, apparently. >> >>ctypes thread with Thomas Heller below. His main question: "Maybe you should ask to make sure that there's no way to copy between >>objects implementing the buffer protocol with some Python function that >>I do not know about?" > >I just tried some things: >>>>import ctypes >>>>a = (ctypes.c_int * 5)() >>>>a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5 >>>>list(a) >[1, 2, 3, 4, 5] >>>>import numarray > >>>>buf = numarray.zeros(shape=20, type='i4') >>>>buf >array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) >>>>buf[2:7] = a >>>>buf >array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) > >>>>temp = numarray.array(sequence=buffer(a), shape=5, type='i4') >>>>temp >array([1, 2, 3, 4, 5]) >>>>temp._data ><read-only buffer for 0x00FA9230, ptr 0x00BFA668, size 20 at 0x010D6DA0> >>>>buffer(a) ><read-only buffer for 0x00FA9230, ptr 0x00BFA668, size 20 at 0x00FAF600> > >>>>a[2] = 10 >>>>temp >array([ 1, 2, 10, 4, 5]) > >The first block just creates the ctypes data. > >The second block uses slice assignment to copy the data from the ctypes array into the numarray.array. > >The third block uses the buffer interface to create a numarray.array which points to the same memory location as the ctypes array. > >The forth block illustrates that it's really the same memory. > >You have to benchmark which one of the two solutions is the better one for you. > >Regards, >Florian Schulze > > >______________________________________________ >Numpy-discussion mailing list >Num...@li... >https://lists.sourceforge.net/lists/listinfo/numpy-discussion |