c# - Get subarray of byte -
in c#, how can sub array of bytes this
byte[] arrbyte1 = {11,22,33,44,55,66}
i need reference sub array of 2 bytes 33 , 44 values.
i found multiple options there array.copy, arraysegment, linq (skip , take) in c#. best solution performance point of view?
easy perfomance test:
public void test() { const int max = 1000000; byte[] arrbyte1 = { 11, 22, 33, 44, 55, 66 }; byte[] arrbyte2 = new byte[2]; stopwatch sw = new stopwatch(); // array.copy sw.start(); (int = 0; < max; i++) { array.copy(arrbyte1, 2, arrbyte2, 0, 2); } sw.stop(); console.writeline("array.copy: {0}ms", sw.elapsedmilliseconds); // linq sw.start(); (int = 0; < max; i++) { arrbyte2 = arrbyte1.skip(2).take(2).toarray(); } sw.stop(); console.writeline("linq: {0}ms", sw.elapsedmilliseconds); }
result:
array.copy: 28ms linq: 189ms
perfomance test on large data:
public void test() { const int max = 1000000; int[] arrbyte1 = enumerable.range(0, 1000).toarray(); int[] arrbyte2 = new int[500]; stopwatch sw = new stopwatch(); // array.copy sw.start(); (int = 0; < max; i++) { array.copy(arrbyte1, 500, arrbyte2, 0, 500); } sw.stop(); console.writeline("array.copy: {0}ms", sw.elapsedmilliseconds); // linq sw.start(); (int = 0; < max; i++) { arrbyte2 = arrbyte1.skip(500).take(500).toarray(); } sw.stop(); console.writeline("linq: {0}ms", sw.elapsedmilliseconds); }
result:
array.copy: 186ms linq: 12666ms
as see, on large data linq have troubles.
Comments
Post a Comment