Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Important:</strong> if you only need the one endian, see the pointer magic by wj32 / dtb</p> <hr> <p>Personally, I would be writing directly to a <code>Stream</code> (perhaps with some buffering), and re-using a shared buffer that I can generally assume is clean. Then you can make some shortcuts and assume index 0/1/2/3.</p> <p>Certainly don't use <code>BitConverter</code>, as that can't be used for both little/big-endian, which you require. I would also be inclined to just use bit-shifting rather than unsafe etc. It is actally the fastest, based on the following (so I'm glad that this is how I already do it my code <a href="http://code.google.com/p/protobuf-net/source/browse/branches/v1/protobuf-net/SerializationContext.Write.cs#184" rel="nofollow noreferrer">here</a>, look for <code>EncodeInt32Fixed</code>):</p> <pre><code>Set1: 371ms Set2: 171ms Set3: 993ms Set4: 91ms &lt;==== bit-shifting ;-p </code></pre> <p>code:</p> <pre><code>using System; using System.Diagnostics; using System.Runtime.InteropServices; static class Program { static void Main() { const int LOOP = 10000000, INDEX = 100, VALUE = 512; byte[] buffer = new byte[1024]; Stopwatch watch; watch = Stopwatch.StartNew(); for (int i = 0; i &lt; LOOP; i++) { Set1(buffer, INDEX, VALUE); } watch.Stop(); Console.WriteLine("Set1: " + watch.ElapsedMilliseconds + "ms"); watch = Stopwatch.StartNew(); for (int i = 0; i &lt; LOOP; i++) { Set2(buffer, INDEX, VALUE); } watch.Stop(); Console.WriteLine("Set2: " + watch.ElapsedMilliseconds + "ms"); watch = Stopwatch.StartNew(); for (int i = 0; i &lt; LOOP; i++) { Set3(buffer, INDEX, VALUE); } watch.Stop(); Console.WriteLine("Set3: " + watch.ElapsedMilliseconds + "ms"); watch = Stopwatch.StartNew(); for (int i = 0; i &lt; LOOP; i++) { Set4(buffer, INDEX, VALUE); } watch.Stop(); Console.WriteLine("Set4: " + watch.ElapsedMilliseconds + "ms"); Console.WriteLine("done"); Console.ReadLine(); } unsafe static void Set1(byte[] target, int index, int value) { fixed (byte* p = &amp;target[0]) { Marshal.WriteInt32(new IntPtr(p), index, value); } } unsafe static void Set2(byte[] target, int index, int value) { int* p = &amp;value; for (int i = 0; i &lt; 4; i++) { target[index + i] = *((byte*)p + i); } } static void Set3(byte[] target, int index, int value) { byte[] data = BitConverter.GetBytes(value); Buffer.BlockCopy(data, 0, target, index, data.Length); } static void Set4(byte[] target, int index, int value) { target[index++] = (byte)value; target[index++] = (byte)(value &gt;&gt; 8); target[index++] = (byte)(value &gt;&gt; 16); target[index] = (byte)(value &gt;&gt; 24); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload