Array.Copy vs Buffer.BlockCopy
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的更多相关文章
- C#把某个数组的一部分复制到另一个数组中的两种方法:Buffer.BlockCopy和Array.Copy
static void Main(string[] args) { , , , , , }; ;//目标数组大小 int int_size = sizeof(int);//用于获取值类型的字节大小. ...
- C# 字节数组拼接的速度实验(Array.copy(),Buffer.BlockCopy(),Contact())
无聊做了如题的一个算法的优劣性能比较,由于很多人都只关心结果,那么我先贴出结果如下: 由于我的测试数据量比较小,只能得出Array.Copy()和Buffer.BlockCopy()方法性能要好于Co ...
- C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:
public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ...
- Array.Copy
var bt = new byte[] { 0x03, 0x00, 0x01, 0xD9, 0x23 }; var result = new byte[] { 0x01, 0x00, 0x03, 0x ...
- Swift Array copy 的线程安全问题
Swift Array copy 的线程安全问题 NSArray 继承自 NSObject,属于对象,有 copy 方法.Swift 的 Array 是 struct,没有 copy 方法.把一个 A ...
- Array.Copy 数据是克隆吗?
偶然看到 Array.Copy 方法的时候,想到,它是否是克隆,又是否是深克隆. 做了一个测试 public class abc { public string hello; } [TestMetho ...
- c# Buffer.BlockCopy 合并 byte 数组
今天遇到点问题需要合并 多个 byte[] 参见 : http://q.cnblogs.com/q/30534/ 今天复习了 所有数组的基类是 Array
- array copy rotate in Pointwise
goal: # # Copyright 2010 (c) Pointwise, Inc. # All rights reserved. # # This sample Pointwise script ...
- 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) ...
随机推荐
- centos7配置静态IP步骤
centos7按照初始安装时候的developer类型一路装好,在vmware里已经设置为bridge模式,按理说是会自动按照DHCP联网成功的,结果却发现连网卡都没有激活,这里记录下. 1:我要把L ...
- 20181225模拟赛 T1 color (转化思想,分拆思想)
题目: 有⼀块有 n 段的栅栏,要求第 i 段栅栏最终被刷成颜色 ci .每⼀次可以选择 l, r 把第l . . . r 都刷成某种颜色,后刷的颜⾊会覆盖之前的.⼀共有 m 种颜色,雇主知道只需要用 ...
- http chunked 理解
https://imququ.com/post/transfer-encoding-header-in-http.html #! /usr/bin/python #coding:utf8 import ...
- linux shell & man chmod
man chomd MBP xgqfrms-mbp:~ xgqfrms-mbp$ man chmod and entries will be removed regardless of their i ...
- [ C语言 ] 迷宫 迷宫生成器 [ 递归与搜索 ]
[原创]转载请注明出处 [浙江大学 程序设计专题] [地图求解器] 本题目要求输入一个迷宫地图,输出从起点到终点的路线. 基本思路是从起点(Sx,Sy)每次枚举该格子上下左右四个方向,直到走到终点(T ...
- maven坐标查询
使用maven时,一个经常用到的操作就是去 中央仓库查询相关库的坐标,但在哪里查呢? 1 http://mvnrepository.com/ 服务器是由sonatype提供的,采用的是Nexus服务器 ...
- CALayer之 customizing timing of an animation
customizing timing of an animation Timing is an important part of animations, and with Core Animatio ...
- vue生成包报错error from UglifyJs
mangle: { keep_fnames: true},
- 洛谷——P1454 圣诞夜的极光
P1454 圣诞夜的极光 题目背景 圣诞夜系列~~ 题目描述 圣诞老人回到了北极圣诞区,已经快到12点了.也就是说极光表演要开始了.这里的极光不是极地特有的自然极光景象.而是圣诞老人主持的人造极光. ...
- WCF的Binding模型之四:信道工厂(Channel Factory)
由于信道管理器在客户端和服务端所起的不同作用,分为信道监听器和信道工厂.和服务端的信道监听其相比,处于客户端的信道工厂显得简单.从名称就可以看得出来,信道工厂的作用就是单纯的创建用于消息发送的信道.我 ...