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!

Rabbit
  • 1,741
  • 2
  • 18
  • 27

4 Answers4

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#.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 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. – Marc Gravell Apr 24 '11 at 18:23
  • I'd like to but it has to run on Windows Phone 7 so it's not an option :( – Rabbit Apr 24 '11 at 19:44
  • @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). – John Zwinck Apr 24 '11 at 21:09
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).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 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). – Rabbit Apr 24 '11 at 19:49
  • 2
    @user72185 - one thing to check; does WP7 support explicit-layout on structs? I don't *think* it does, but worth a try? – Marc Gravell Apr 24 '11 at 20:22
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

sehe
  • 374,641
  • 47
  • 450
  • 633
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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351