.net - C#. How fast insert Byte to Struct all time? -


Net 2.0 for code

I write ByteArrayToObject function to insert offset bytes in the structure, but is it possible to quickly? It is planned that there will be several structures in which the changes in network information are required to be attached. If I can insert these bytes quickly in the right place, then it will be organized in the form of a large structure in the protocol. Thank you for any help.

In my case, I do not like to copy all of the Object Focus Object broken arrows to change the bytes every time.

  /// & lt; Summary & gt; Change the object straight to a byte array & lt; / Summary & gt; Private stationary byte [] Object obie (object obege) {var size = martial. Size of (OBJ); // Both managed and unmanaged buffers are required by bytes = new byte [size]; Var ptr = martial. Olloc hinge (size); // Copy object byte-to-byte Marshall in unmanaged memory. StructureToptr (Ozz, PTR, False); // Copy the managed buffer from unmanaged memory. Martial Copy (PTR, bytes, 0, size); // Release unmanaged memory Marshal.FreeHGlobal (PTR); Return bytes; } /// & lt; Summary & gt; Need fast? & Lt; / Summary & gt; Public stable tee biterereobabjak & lt; T & gt; (Referee t ojjay, int stieoffset, paramet byte [] bytes) {int size = Marshal.SizeOf (obj); Int length = (bytes; size> size)? Size: Bytes. Long; Byte [] Obbets = Objects broken (OBJ); Array.Copy (bytes, 0, albumites, startoffs, length - startoffs); Var ptr = martial. Olloc hinge (size); Marshall.Copy (Albites, 0, PTR, length); Obj = (t) martial.tterstroke (ptr, typef (t)); Marshal.FreeHGlobal (PTR); Return obj; }   

Example usage

  [serializable] [System.Runtime.InteropServices.StructLayout (System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1 , Charset = System.Runtime.InteropServices.CharSet.Ansi)] Protocol Protocol {Public byte f0; Public byte f1; Public low F2; Public byte F3; Public long F4; [System.Runtime.InteropServices.MarshalAs (System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 20000)] Public int [] Array; // 20000} System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch (); For (Byte Eye = 1; I <10; i ++) {sw.Reset (); Sw.Start (); Ob = bytereoobaback & lt; Protocol & gt; (Referee ob, 1, i, 0x11, i, 0x22, i); Sw.Stop (); Console.light line ("tick" + sw.ElapsedTicks); }   

output

  tic = 9940 tick = 686 tick = 593 tick = 474 tick = 562 tick = 5283 tick = 1 = tick = 173 tick = 164 It is too long for a comment, but to expand on an unsafe approach:    

  Unsafe structure pre {public byte f0, f1, f2, f3, f4; Public Definitive Info Buffer [20000]; } Class program {public static unsecured zero byteetoax (pre * obj, int offset, paramet byte [] bytes) {// You can add net safely here size (pre) structure byte * p = (byte *) obj ; Forchach (byte in byte) {p [offset ++] = b; } // return value is not expensive, it is expensive! } Unsafe stationary zero main (string [] arg) {stopwatch SW = new stopwatch (); Console.WriteLine (Stopwatch.Frequency); Ex e = new Ex {f0 = 0, f1 = 1, f2 = 2, f3 = 3, f4 = 4}; Byteretex (& amp; A, 2, 5, 6, 7); For (Int i = 0; I   

This can not work for you or may not return the price. You are already mutating the pointer in it, adding 10 ticks to call a copy of such a large structure per call.

In addition, you will have to make at least 1 warmup during benchmarking. This is the reason that the first number is so bad.

Result on my PC:

  3312929 4 2 0 0 0 0 0 0    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -