Static voidMain (string[] args) { int[] src =New[] {1,2,3,4,5,6 }; Const intDestlen =4;//Target array size intInt_size =sizeof(int);//the size of the byte used to get the value type. int[] Dest =New int[Destlen]; //only primitive types are supported, copy by byte offsetBuffer.blockcopy (SRC, src. Length-destlen) * Int_size, dest,0, Destlen *int_size); foreach(varIinchdest) {Console.Write (i+" "); } Console.WriteLine ("\ n-------------------------------------------"); string[] Srcstr =New[] {"A","B","C","D","E","F" }; Object[] Destobj =New Object[SRC. Length-2]; //number of elements removed Const intDellen =2; //guarantees that the elements of the target array (rollback) are not destroyed. Do not box, unboxing, or downward conversion, otherwise error. //if the srcstr is changed to SRC then the error is due to boxing. Array.constrainedcopy (Srcstr, Dellen, Destobj,0, Srcstr. Length-Dellen); foreach(varSinchdestobj) {Console.Write (S+" "); } }
Copy 1 billion times for the specified array and the target array, with the same type.
The consumption time is as follows:
copy:59.374s,constrainecopy:48.415 s,blockcopy:23.219s
The code is nothing but the test, the core test is as follows:
Int[] INTs = {1534, 233, 332, 423, 524, 3246, 4357, 734, 567, 43, 34254, 325, 3325, 2423, 345, 575, 235, 1, 342, 1, 6, 54 645, 5432, 5};
int[] Dest = new int[ints. Length];
Array.copy (INTs, dest, INTs. Length);
Array.constrainedcopy (ints, 0, dest, 0, INTs. Length);
Buffer.blockcopy (ints, 0, dest, 0, INTs. Length * 4);
Annotation Analysis:
1,array.copy in the CLR processing mechanism of the most flexible, most powerful, can be boxed, unboxing replication, can widen the CLR primitive type, can internally determine the implementation of the Ifarmattable interface compatible conversion, of course, this powerful way will inevitably bring some performance loss.
2,array.constrainedcopy requires strict replication, can only be of the same type or source array type is the derived element type of the target type, does not execute boxing, unboxing, down conversion
3,buffer.blockcopy is essentially byte-copied units, which in the underlying language c,c++ the processing advantage, the same, the high efficiency can be understood.
Of course, if the performance requirements are not high, copy is sufficient, after all, after thousands of copies, the three basically did not consume much time. You can choose according to the requirements of the project when using!
Question:How C # copies part of a long array into another short array
Byte[] Shortary=new byte[4];
Byte[] Longary=new byte[20];
How do I copy the 4 bytes of longary[5,9 (not included) into Shortary?
Do not use loops.
Use the Array.copy method to copy an array or part of an array to another group. Array.copy is a static method and has multiple overloaded versions. Among the most commonly used are:
Public Static void Copy ( array sourcearray, int sourceIndex, array destinationarray, int Destinationindex, int length);
Each parameter has the following meanings
sourcearray--Source Array
sourceindex--represents the index at the beginning of replication in Sourcearray
destinationarray--the target array, which receives the data
destinationindex--represents the index at the beginning of storage in Destinationarray
length--the number of elements to copy.
Examples of usage are as follows:
(1) Copy part of an array to another array
int[] src = {1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the };int[] Dest =New int[4];//Copy the element 2,3,4,5 in the array src to the destArray.copy (SRC,1, Dest,0,4);
(2) Copying the entire array
int[] src = {1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the };int[] Dest =New int[SRC. Length];//Copy all elements of the array src to destArray.copy (SRC,0, Dest,0Src. Length);
C # Two ways to copy part of an array into another array: Buffer.blockcopy and Array.copy