Re: [ctypes-users] copying/slicing ctypes arrays, (c_ulong *n)()
Brought to you by:
theller
From: Thomas H. <th...@py...> - 2004-12-21 18:49:18
|
Ray S <ra...@bl...> writes: > New results, corrected code... > > >python test.py > Array address: 11784976 > n address: 17039424 <memory at 01040040 with size:4000000 held by object > 01040020 aliasing object 00000000> > N address: 11384616 <built-in method __copy__ of array object at 0x00ADB6D8> > > for loop 1929.0 us ea > map 1630.0 us ea > slice 1589.0 us ea > assign n 1164.0 us ea > assign N 337.0 us ea > memmove A 3.9847 us ea > memmove n 3.2005 us ea The memmove results are probably still somewhat misleading, because of the adress arithmetic done in the timed loop, that *may* explain this: > I'm not sure why a memmove to Array is always slower than numarray. > I could not do a memmove() with Numeric because I couldn't find a > corresponding output to repr(n._data) (thanks for that tip); > repr(N.__copy__) gives the address of an object(array or method?). > dir(N) and dir(Numeric) were no help. In test.py I tried adding 40 > hex (al-la numarray) to get the first data element, then I tried 80. > Ninfo = string.split(repr(N.__copy__)) > NAddress = int(Ninfo[7][:-1], 16)+int(hex(80), 16) > Adding 40 to the address of the object apparently overwrites the > object, as a GC error is thrown. Adding 80 allows the loop to run, but > MS Win2K complains that python23.dll had a problem and wants to send > an error report. I might be overwriting the __copy__ method in the dll > for all I know - the error makes that likely. I have thought about all this a little bit, and it seems that it would be very useful to accept Python objects which implement the buffer interface as function parameters (currently only ctype instances, strings and integers are allowed). Of course, sometimes you don't want the data to be copied just at the beginning, but in this case you should wrap the object into a buffer object (with the buildin buffer() type/function). The buffer() call accepts optional parameters for offset and size, so address arithmetic would be done by the buffer object itself - nicer than to add more parameters to the memmove function and friends. How does that sound? Thomas |