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:

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:

using System;
using System.Diagnostics;
using System.Reflection; unsafe delegate void MemCpyImpl(byte* src, byte* dest, int len); static class Temp
{
//There really should be a generic CreateDelegate<T>() method... -___-
static MemCpyImpl memcpyimpl = (MemCpyImpl)Delegate.CreateDelegate(
typeof(MemCpyImpl), typeof(Buffer).GetMethod("memcpyimpl",
BindingFlags.Static | BindingFlags.NonPublic));
const int COUNT = , SIZE = << ; //Use different buffers to help avoid CPU cache effects
static byte[]
aSource = new byte[SIZE], aTarget = new byte[SIZE],
bSource = new byte[SIZE], bTarget = new byte[SIZE],
cSource = new byte[SIZE], cTarget = new byte[SIZE]; static unsafe void TestUnsafe()
{
Stopwatch sw = Stopwatch.StartNew();
fixed (byte* pSrc = aSource)
fixed (byte* pDest = aTarget)
for (int i = ; i < COUNT; i++)
memcpyimpl(pSrc, pDest, SIZE);
sw.Stop();
Console.WriteLine("Buffer.memcpyimpl: {0:N0} ticks", sw.ElapsedTicks);
} static void TestBlockCopy()
{
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
for (int i = ; i < COUNT; i++)
Buffer.BlockCopy(bSource, , bTarget, , SIZE);
sw.Stop();
Console.WriteLine("Buffer.BlockCopy: {0:N0} ticks",
sw.ElapsedTicks);
} static void TestArrayCopy()
{
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
for (int i = ; i < COUNT; i++)
Array.Copy(cSource, , cTarget, , SIZE);
sw.Stop();
Console.WriteLine("Array.Copy: {0:N0} ticks", sw.ElapsedTicks);
} static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
TestArrayCopy();
TestBlockCopy();
TestUnsafe();
Console.WriteLine();
}
}
}

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. 20181228 模拟赛 T3 字符串游戏 strGame 博弈论 字符串

    3  字符串游戏(strGame.c/cpp/pas) 3.1  题目描述 pure 和 dirty 决定玩 T 局游戏.对于每一局游戏,有n个字符串,并且每一局游戏由K轮组成.具体规则如下:在每一轮 ...

  2. C#基础学习(一)

    ---恢复内容开始--- 1.最近被安排去做C#开发,然后开始一连串的看文档·看视屏,发现学C#给自己补了很多基础,C#每个函数变量什么都要先声名,而python可以直接定义: 一.数据类型 1.整数 ...

  3. assert.ifError()函数详解

    assert.ifError(value) 如果 value 为真值时,抛出 value.当测试在回调函数里的参数 error 时非常有用. const assert = require('asser ...

  4. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  5. Vue如何使用vue-awesome-swiper实现轮播效果

    在Vue项目中如何实现轮播图的效果呢,在传统项目中第一个想到的一般都是swiper插件,代码简单好用.一开始我也是直接npm安装swiper然后照着之前的传统写法写,然而却没有效果,只会显示图片但没有 ...

  6. BZOJ 3754 Tree之最小方差树 MST

    Description Wayne 在玩儿一个很有趣的游戏.在游戏中,Wayne 建造了N 个城市,现在他想在这些城市间修一些公路,当然并不是任意两个城市间都能修,为了道路系统的美观,一共只有M 对城 ...

  7. [NOIP2004] 普及组

    不高兴的津津 纯模拟 #include<cmath> #include<cstdio> #include<iostream> using namespace std ...

  8. codevs——1081 线段树练习 2

    1081 线段树练习 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 给你N个数,有两种操作 1:给 ...

  9. maven的安装与环境变量配置

    1.下载maven 地址:http://maven.apache.org/download.cgi 点击下载 apache-maven-3.2.1-bin.zip. 2.安装配置,假设maven 解压 ...

  10. Oldboy 基于Linux的C/C++自动化开发---MYSQL

    http://www.eimhe.com/forum.php?mod=viewthread&tid=142952#lastpost http://www.eimhe.com/thread-14 ...