-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Would it be more scalable and have better performance if ndarray.tobytes() returned a Span<byte>? #64
Comments
I don't have time right now, but I will experiment with this when I get a chance. Question: is this code MemoryMarshal.Cast<byte, T>(ndarray.tobytes()); even valid in a .NET standard application? In other words , will it work on .net versions for linux and mac too? I welcome assistance. If you want branch the code and try to turn this into a generic API for different data types, please fell free. I will review it and possibly accept it into the main branch. |
I tried to add a unit test to experiment with your sample. I does not compile cleanly for me. I have not used Span before so it may be user error on my part. These lines do not compile cleanly: values[start..end]; Can you help?
|
Hi Kevin, I just happened to click on this and am interested... so please pardon me for butting in. The two lines that you mention The compile error is not specific to Spans,
can be read as
can be read as With regards to your other question.. A mini primer on Spans:
Memory is more amenable to storing and async code, Memory is also a typed reference with a length, but is an object and not a struct. if you have some time, (and dont mind watching videos) the following recent presentation does a reasonable job at explaining how revolutionary the introduction of Span is: Alex |
I appreciate you butting in :) Thanks for the tips. I appreciate you pointing the info. |
As I interpret your question, the answer is that YES, converting an existing array to a Span via MemoryMarshal.Cast is much faster than using the existing "tobytes" method. In this unit test below, it took 7743 ms to convert using tobytes and only 205ms using the MemoryMarshal.Cast method.
|
In the unit test below, the call to tobytes returns a new array of bytes, copied from the source array. If you modify the bytes1 array it does not affect the original. If you use the MemoryMarshall.Cast method, you get a view into the original array and any modifications made to the bytes2 will be reflected in the original array. So, they are very different.
|
Unless I misunderstood the question, I think the existing implementation of .tobytes() is closer to the python version (although that actually returns a string instead of a byte[]). But this was a great exercise to show that anyone who wants to map an ndarray object to a different data type can do so much faster with MemoryMarshall.Cast as long as they don't want a copy. |
In this example, the conversion is particularly slow.
The text was updated successfully, but these errors were encountered: