C # method for fast copying with pointers

Source: Internet
Author: User
The example in this paper describes the method of C # for fast copying with pointers. Share to everyone for your reference. The implementation method is as follows:

fastcopy.cs//compile-time use:/unsafeusing system;class test{//unsafe keyword allows pointers to be used in the following//methods: Static unsafe void Copy (byte[] Sr      c, int srcindex, byte[] DST, int dstindex, int count) {if (src = = NULL | | Srcindex < 0 | | DST = = NULL | | Dstindex < 0 | |    Count < 0) {throw new ArgumentException (); } int srclen = src.    Length; int Dstlen = DST.    Length;    if (Srclen-srcindex < count | | Dstlen-dstindex < count) {throw new ArgumentException ();    }//The following fixed statement fixed//SRC objects and DST objects in memory position so that both objects//are not moved by garbage collection.      Fixed (byte* PSRC = src, pDst = DST) {byte* PS = PSRC;      byte* PD = pDst; Loop count in 4-byte blocks, one copy//integer (4 bytes): for (int n = 0; n < count/4; n++) {* ((int*) pd) = * ((i        nt*) PS);        PD + = 4;      PS + = 4;        }//Move all bytes not moved with a 4-byte block,//To complete replication: for (int n = 0; n < count% 4; n++) {*pd = *ps;        pd++;      ps++; }}}} static void Main (StrinG[] args) {byte[] a = new byte[100];    Byte[] B = new byte[100];    for (int i = 0; i < ++i) a[i] = (byte) i;    Copy (A, 0, b, 0, 100);    Console.WriteLine ("The first elements is:");    for (int i = 0; i < ++i) Console.Write (B[i] + "");  Console.WriteLine ("\ n"); }}

Hopefully this article will help you with your C # programming.

Related Article

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.