[Python.NET] Efficient copy of .NET Array to ctypes or numpy array.

Jeffrey Bush jeff at coderforlife.com
Wed May 21 19:58:42 CEST 2014


You could write a .NET function to do this with fixed pointers and "memcpy"
from the .NET array to the numpy data (the raw data). This would be the
absolute fastest way, but does involve a number of assumptions (for
example that the data in the two arrays are laid out in the same way). If
you want I could probably write something up real quick.

Jeff

On Wednesday, May 21, 2014, Brad Friedman <brad at fie.us> wrote:

> An aside that may be useful:
>
> .net will skip array bounds checking within simple for-loops, as an
> optimization. But only if the binaries have all their optimizations turned
> on. A binary built for debug has them turned off. There is a huge speed up
> for iterating over an array when these optimizations are used. So make sure
> you are not looking at a compiler optimization configuration problem.
>
> > On May 21, 2014, at 3:21 AM, Dave Cook <daverz at gmail.com <javascript:;>>
> wrote:
> >
> > I need to copy a .NET Array (e.g. Double[] or Byte[]) to a numpy array,
> but it seems the only way to do so is element by element, which is very
> slow.  Since we are copying a lot of data in real time, it creates a real
> bottleneck.
> >
> > Alternatively, efficient conversion of the .NET array to a Python style
> byte string would allow numpy.fromstring() to be used for creating the
> numpy array.
> >
> > (I see a similar question went unanswered on the list in August 2011,
> but I was hoping someone may have figured it out by now.)
> >
> > Thanks,
> > Dave Cook
> > _________________________________________________
> > Python.NET mailing list - PythonDotNet at python.org <javascript:;>
> > https://mail.python.org/mailman/listinfo/pythondotnet
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org <javascript:;>
> https://mail.python.org/mailman/listinfo/pythondotnet
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20140521/e703361d/attachment.html>


More information about the PythonDotNet mailing list