<div dir="ltr">I was tempted to code it up, and it turns out you can do it in pure python.<div><br></div><div>I thought of 4 ways to copy the data: using a for loop (like you did), using numpy.fromiter, using numpy.fromstring, and using Marshal.Copy.</div>

<div><br></div><div>Obviously the for loop is the slowest. numpy.fromiter is still slow, but ~2.5x faster than the for loop (still has all .NET array checks since this is in Python and the indexers cannot be optimized away). The last two do direct memory copies (fromstring gets the memory pointer of the .NET array while Marshal.Copy gets the memory pointer of the NumPy array). They are both MUCH faster than a for loop, especially for larger arrays (>200x faster). numpy.fromstring is faster for smaller arrays, but by the time I got to 10000000 doubles it was twice as slow as Marshal.Copy.</div>

<div><br></div><div>Here is the code:</div><div><br></div><div><div>import clr</div><div>from System import Array, Double, IntPtr, Random</div><div>import numpy as np</div><div>import time</div><div><br></div><div>def check_arrays(a, b):</div>

<div>    if len(a) != len(b): print("Arrays are different size!")</div><div>    if any(A != B for A,B in zip(a, b)): print("Arrays have different values!")</div><div><br></div><div>print("Creating source...")</div>

<div>r = Random()</div><div>src = Array.CreateInstance(Double, 10000000)</div><div>for i in xrange(len(src)): src[i] = r.NextDouble()</div><div><br></div><div>print('Copy using for loop'),</div><div>start = time.clock()</div>

<div>dest = np.empty(len(src))</div><div>for i in xrange(len(src)): dest[i] = src[i]</div><div>end = time.clock()</div><div>print('in %f sec' % (end-start))</div><div>check_arrays(src, dest)</div><div><br></div><div>

print('Copy using fromiter'),</div><div>start = time.clock()</div><div>dest = np.fromiter(src, float)</div><div>end = time.clock()</div><div>print('in %f sec' % (end-start))</div><div>check_arrays(src, dest)</div>

<div><br></div><div>print('Copy using fromstring'),</div><div>from ctypes import string_at</div><div>from System.Runtime.InteropServices import GCHandle, GCHandleType</div><div>start = time.clock()</div><div>src_hndl = GCHandle.Alloc(src, GCHandleType.Pinned)</div>

<div>try:</div><div>    src_ptr = src_hndl.AddrOfPinnedObject().ToInt32()</div><div>    dest = np.fromstring(string_at(src_ptr, len(src)*8)) # note: 8 is size of double...</div><div>finally:</div><div>    if src_hndl.IsAllocated: src_hndl.Free()</div>

<div>end = time.clock()</div><div>print('in %f sec' % (end-start))</div><div>check_arrays(src, dest)</div><div><br></div><div>print('Copy using Marshal.Copy'),</div><div>from System.Runtime.InteropServices import Marshal<br>

</div><div>start = time.clock()</div><div>dest = np.empty(len(src))</div><div>Marshal.Copy(src, 0, IntPtr.__overloads__[int](dest.__array_interface__['data'][0]), len(src))</div><div>end = time.clock()</div><div>
print('in %f sec' % (end-start))</div>
<div>check_arrays(src, dest)</div></div><div><br></div><div>Jeff</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, May 21, 2014 at 10:58 AM, Jeffrey Bush <span dir="ltr"><<a href="mailto:jeff@coderforlife.com" target="_blank">jeff@coderforlife.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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. <div>


<br></div><div>Jeff<div><div class="h5"><br><br>On Wednesday, May 21, 2014, Brad Friedman <<a href="mailto:brad@fie.us" target="_blank">brad@fie.us</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


An aside that may be useful:<br>
<br>
.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.<br>



<br>
> On May 21, 2014, at 3:21 AM, Dave Cook <<a>daverz@gmail.com</a>> wrote:<br>
><br>
> 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.<br>



><br>
> 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.<br>
><br>
> (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.)<br>
><br>
> Thanks,<br>
> Dave Cook<br>
> _________________________________________________<br>
> Python.NET mailing list - <a>PythonDotNet@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/pythondotnet" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br>
_________________________________________________<br>
Python.NET mailing list - <a>PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br>
</blockquote></div></div></div>
</blockquote></div><br></div>