C # Performance Comparison of array replication, array. Copy, buffer. blockcopy, array. constrainedcopy

Source: Internet
Author: User

If the types of the specified array and target array are the same, 1 billion copies are performed,

The consumed time is as follows:

CodeThe core test is as follows:

Int [] ints = {1534,233,332,423,524,324 6, 4357,734,567, 43,342 54, 325,332 5, 2423,345,575,235, 1,342, 1, 6, 54645,543 2, 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. in the CLR processing mechanism, copy is the most flexible, powerful, boxed, and case-free, and the CLR primitive type can be extended. The ifarmattable interface compatible conversion can be implemented through internal judgment, of course, this powerful method will inevitably cause certain performance losses.

2. array. constrainedcopy has strict requirements on replication. It can only be of the same type or the source array type is a derived element type of the target type. No packing, unboxing, and downward conversion are performed.

3. Buffer. blockcopy is essentially in bytes as the unit of replication. In terms of the processing advantages of the underlying Language C and C ++, the efficiency is also high.

Of course, if you do not have high performance requirements, copy is enough. After all, the three do not consume much time for thousands of copies. You can select a project based on your needs!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.