http://stackoverflow.com/questions/1389821/array-copy-vs-buffer-blockcopy

Since the parameters to Buffer.BlockCopy are byte-based rather than index-based, you're more likely to screw up your code than if you use Array.Copy,

so I would only use Buffer.BlockCopy in a performance-critical section of my code.

http://stackoverflow.com/questions/5099604/any-faster-way-of-copying-arrays-in-c

Use Buffer.BlockCopy. Its entire purpose is to perform fast (see Buffer):

This class provides better performance for manipulating primitive types than similar methods in the System.Array class.

Admittedly无可否认, I haven't done any benchmarks基准;标准检查程序, but that's the documentation. It also works on multidimensional多维 arrays; just make sure that you're always specifying how many bytes to copy, not how many elements, and also that you're working on a primitive基元 array.

Also, I have not tested this, but you might be able to squeeze挤 a bit more performance out of the system if you bind a delegate to System.Buffer.memcpyimpl and call that directly. The signature is:

  1. internal static unsafe void memcpyimpl(byte* src, byte* dest, int len)

It does require pointers, but I believe it's optimized for the highest speed possible, and so I don't think there's any way to get faster than that, even if you had assembly at hand.

Update:

Due to requests (and to satisfy my curiosity), I tested this:

  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4.  
  5. unsafe delegate void MemCpyImpl(byte* src, byte* dest, int len);
  6.  
  7. static class Temp
  8. {
  9. //There really should be a generic CreateDelegate<T>() method... -___-
  10. static MemCpyImpl memcpyimpl = (MemCpyImpl)Delegate.CreateDelegate(
  11. typeof(MemCpyImpl), typeof(Buffer).GetMethod("memcpyimpl",
  12. BindingFlags.Static | BindingFlags.NonPublic));
  13. const int COUNT = , SIZE = << ;
  14.  
  15. //Use different buffers to help avoid CPU cache effects
  16. static byte[]
  17. aSource = new byte[SIZE], aTarget = new byte[SIZE],
  18. bSource = new byte[SIZE], bTarget = new byte[SIZE],
  19. cSource = new byte[SIZE], cTarget = new byte[SIZE];
  20.  
  21. static unsafe void TestUnsafe()
  22. {
  23. Stopwatch sw = Stopwatch.StartNew();
  24. fixed (byte* pSrc = aSource)
  25. fixed (byte* pDest = aTarget)
  26. for (int i = ; i < COUNT; i++)
  27. memcpyimpl(pSrc, pDest, SIZE);
  28. sw.Stop();
  29. Console.WriteLine("Buffer.memcpyimpl: {0:N0} ticks", sw.ElapsedTicks);
  30. }
  31.  
  32. static void TestBlockCopy()
  33. {
  34. Stopwatch sw = Stopwatch.StartNew();
  35. sw.Start();
  36. for (int i = ; i < COUNT; i++)
  37. Buffer.BlockCopy(bSource, , bTarget, , SIZE);
  38. sw.Stop();
  39. Console.WriteLine("Buffer.BlockCopy: {0:N0} ticks",
  40. sw.ElapsedTicks);
  41. }
  42.  
  43. static void TestArrayCopy()
  44. {
  45. Stopwatch sw = Stopwatch.StartNew();
  46. sw.Start();
  47. for (int i = ; i < COUNT; i++)
  48. Array.Copy(cSource, , cTarget, , SIZE);
  49. sw.Stop();
  50. Console.WriteLine("Array.Copy: {0:N0} ticks", sw.ElapsedTicks);
  51. }
  52.  
  53. static void Main(string[] args)
  54. {
  55. for (int i = ; i < ; i++)
  56. {
  57. TestArrayCopy();
  58. TestBlockCopy();
  59. TestUnsafe();
  60. Console.WriteLine();
  61. }
  62. }
  63. }

or, in other words: they're very competitive; as a general rule, memcpyimpl is fastest, but it's not necessarily worth worrying about.

Array.Copy vs Buffer.BlockCopy的更多相关文章

  1. C#把某个数组的一部分复制到另一个数组中的两种方法:Buffer.BlockCopy和Array.Copy

    static void Main(string[] args) { , , , , , }; ;//目标数组大小 int int_size = sizeof(int);//用于获取值类型的字节大小. ...

  2. C# 字节数组拼接的速度实验(Array.copy(),Buffer.BlockCopy(),Contact())

    无聊做了如题的一个算法的优劣性能比较,由于很多人都只关心结果,那么我先贴出结果如下: 由于我的测试数据量比较小,只能得出Array.Copy()和Buffer.BlockCopy()方法性能要好于Co ...

  3. C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:

    public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ...

  4. Array.Copy

    var bt = new byte[] { 0x03, 0x00, 0x01, 0xD9, 0x23 }; var result = new byte[] { 0x01, 0x00, 0x03, 0x ...

  5. Swift Array copy 的线程安全问题

    Swift Array copy 的线程安全问题 NSArray 继承自 NSObject,属于对象,有 copy 方法.Swift 的 Array 是 struct,没有 copy 方法.把一个 A ...

  6. Array.Copy 数据是克隆吗?

    偶然看到 Array.Copy 方法的时候,想到,它是否是克隆,又是否是深克隆. 做了一个测试 public class abc { public string hello; } [TestMetho ...

  7. c# Buffer.BlockCopy 合并 byte 数组

    今天遇到点问题需要合并 多个  byte[] 参见 :  http://q.cnblogs.com/q/30534/ 今天复习了 所有数组的基类是 Array

  8. array copy rotate in Pointwise

    goal: # # Copyright 2010 (c) Pointwise, Inc. # All rights reserved. # # This sample Pointwise script ...

  9. js array copy method

    //浅拷贝: let arr=[1,2,3,4] let arr2=arr arr[3]=0 console.log(arr,arr2) //output: (4) [1, 2, 3, 0] (4) ...

随机推荐

  1. MATLAB优化——减少for的使用

    Table of Contents 1. MATLAB 2. 矩阵计算--全0行整体替换 MATLAB MATLAB作为一个强大的工具(可惜是收费的),在矩阵运算.绘制函数和数据.实现算法.创建用户界 ...

  2. 集训第六周 古典概型 期望 D题 Discovering Gold 期望

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

  3. 什么是CPU密集型、IO密集型?(转发)

    CPU密集型(CPU-bound) CPU密集型也叫计算密集型,指的是系统的硬盘.内存性能相对CPU要好很多,此时,系统运作大部分的状况是CPU Loading 100%,CPU要读/写I/O(硬盘/ ...

  4. codeforces 362A找规律

    刚开始以为是搜索白忙活了原来是个简单的找规律,以后要多想啊 此题是两马同时跳 A. Two Semiknights Meet time limit per test 1 second memory l ...

  5. Nginx 的 server_names_hash_bucket_size 问题

    在 Nginx 0.6.35 的版本中,配置多个 server 虚拟主机,必须要在配置文档中 http { 里头加上 server_names_hash_bucket_size 64; 这么一句 ht ...

  6. request详究

    本文主要是对在学习过程中遇到的request用法进行归纳总结,彻底的搞明白request在jsp中的作用. 百度百科的介绍如下: Request对象的作用是与客户端交互,收集客户端的Form.Cook ...

  7. Linux 攻击防护基础排查

    作者:Zzang 来源:cnblogs 原文:https://www.cnblogs.com/Zzang/p/LinuxHack.html 版权声明:本文为博主原创文章,转载请附上博文链接! 基本网络 ...

  8. TP-Link的Atheros芯片的WR886n v5 安装SuperWRT系统

    安装SuperWRT系统 本教程以TP-Link的Atheros芯片的WR886n v5为例,教新手如何刷入一个已支持设备的固件. 下载设备固件请访问:这里 (没有支持你的设备?自由动手一下:hack ...

  9. 条款十: 如果写了operator new就要同时写operator delete

    为什么有必要写自己的operator new和operator delete? 答案通常是:为了效率.缺省的operator new和operator delete具有非常好的通用性,它的这种灵活性也 ...

  10. k-d树及八叉树

    http://en.wikipedia.org/wiki/K-d_tree http://en.wikipedia.org/wiki/Octree