C# method to implement fast copy through pointer

高洛峰
Release: 2017-01-19 13:22:01
Original
1552 people have browsed it

The example in this article describes how C# implements fast copying through pointers. Share it with everyone for your reference. The specific implementation method is as follows:

// fastcopy.cs
// 编译时使用:/unsafe
using System;
class Test
{
  // unsafe 关键字允许在下列
  // 方法中使用指针:
  static unsafe void Copy(byte[] src, 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();
    }
    // 以下固定语句固定
    // src 对象和 dst 对象在内存中的位置,以使这两个对象
    // 不会被垃圾回收移动。
    fixed (byte* pSrc = src, pDst = dst)
    {
      byte* ps = pSrc;
      byte* pd = pDst;
      // 以 4 个字节的块为单位循环计数,一次复制
      // 一个整数(4 个字节):
      for (int n = 0; n < count / 4; n++)
      {
        *((int*)pd) = *((int*)ps);
        pd += 4;
        ps += 4;
      }
      // 移动未以 4 个字节的块移动的所有字节,
      // 从而完成复制:
      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 < 100; ++i)
      a[i] = (byte)i;
    Copy(a, 0, b, 0, 100);
    Console.WriteLine("The first 10 elements are:");
    for (int i = 0; i < 10; ++i)
      Console.Write(b[i] + " ");
    Console.WriteLine("\n");
  }
}
Copy after login

I hope this article will be helpful to everyone’s C# programming.

For more C# methods to achieve fast copying through pointers, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!