2

I have a large byte array with mostly 0's but some values that I need to process. If this was C++ or unsafe C# I would use a 32bit pointer and only if the current 32bit were not 0, I would look at the individual bytes. This enables much faster scanning through the all 0 blocks. Unfortunately this must be safe C# :-)

I could use an uint array instead of a byte array and then manipulate the individual bytes but it makes what I'm doing much more messy than I like. I'm looking for something simpler, like the pointer example (I miss pointers sigh)

Thanks!

4 Answers 4

2

If the code must be safe, and you don't want to use a larger type and "shift", them you'll have to iterate each byte.

(edit) If the data is sufficiently sparse, you could use a dictionary to store the non-zero values; then finding the non-zeros is trivial (and enormous by sparse arrays become cheap).

Sign up to request clarification or add additional context in comments.

2 Comments

You are probably right, I don't know of any way to do it either. A shame, it's a very easy perf gain. Shifting etc. would actually make it slower in the case where most bytes were non-zero (but usually they are mostly zero).
@user72185 - one thing to check; does WP7 support explicit-layout on structs? I don't think it does, but worth a try?
2

I'd follow what this guy said: Using SSE in c# is it possible?

Basically, write a little bit of C/C++, possibly using SSE, to implement the scanning part efficiently, and call it from C#.

3 Comments

In some ways that is "unsafe via stealth: put the unsafe code where we don't see it"... Up to the OP whether that is sensible, of course.
I'd like to but it has to run on Windows Phone 7 so it's not an option :(
@Marc: it's not just stealth, it allows the application of SSE instructions where none would otherwise be used (since they cannot be explicitly coded in .NET itself, to my knowledge).
0

You can access the characters

string.ToCharArray()

Or you can access the raw byte[]

Text.Encoding.UTF8Encoding.GetBytes(stringvalue)

Ultimately, what I think you'd need here is

MemoryStream stream;
stream.Write(...)

then you will be able to directly hadnle the memory's buffer

There is also UnmanagedMemoryStream but I'm not sure whether it'd use unsafe calls inside

1 Comment

Mmmm I suppose I can say: "I did" LOL
0

You can use the BitConverter class:

byte[] byteArray = GetByteArray(); // or whatever
for (int i = 0; i < b.Length; I += 2)
{
    uint x = BitConverter.ToUInt32(byteArray, i);
    // do what you want with x
}

Another option is to create a MemoryStream from the byte array, and then use a BinaryReader to read 32-bit values from it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.