C# 字节数组拼接的速度实验(Array.copy(),Buffer.BlockCopy(),Contact())
无聊做了如题的一个算法的优劣性能比较,由于很多人都只关心结果,那么我先贴出结果如下:

由于我的测试数据量比较小,只能得出Array.Copy()和Buffer.BlockCopy()方法性能要好于Contact(),这个不用比较也能想到,如果想知道前两个谁的性能更好,
有兴趣的可以修改源码中的测试数据量就可以了。
测试源码如下:
static int len1 = ;
static int len2 = ;
static byte[] bytes0 = new byte[len1];
static byte[] bytes1 = new byte[len2];
static void Main(string[] args)
{
// Uses the second Core or Processor for the Test
Process.GetCurrentProcess().ProcessorAffinity = new IntPtr();
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
// Prevents "Normal" Threads
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Stopwatch sw = new Stopwatch();
byte[] resultBytes = null;
InitBytes(); //test
sw.Reset();
sw.Start();
while (sw.ElapsedMilliseconds < ) // A Warmup of 1000-1500 mS
// stabilizes the CPU cache and pipeline.
{
resultBytes = Contact(bytes0, bytes1); // Warmup
}
sw.Stop();
for (int i = ; i < ; i++)
{
sw.Reset();
sw.Start();
resultBytes = Contact(bytes0, bytes1);
sw.Stop();
Console.WriteLine("Contact Ticks: {0} Time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
}
sw.Reset();
sw.Start();
while (sw.ElapsedMilliseconds < ) // A Warmup of 1000-1500 mS
// stabilizes the CPU cache and pipeline.
{
resultBytes = BufferCopy(bytes0, bytes1); // Warmup
}
sw.Stop();
for (int i = ; i < ; i++)
{
sw.Reset();
sw.Start();
resultBytes = BufferCopy(bytes0, bytes1);
sw.Stop();
Console.WriteLine("BufferCopy Ticks: {0} Time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
}
sw.Reset();
sw.Start();
while (sw.ElapsedMilliseconds < ) // A Warmup of 1000-1500 mS
// stabilizes the CPU cache and pipeline.
{
resultBytes = ArrayCopy(bytes0, bytes1); // Warmup
}
sw.Stop();
for (int i = ; i < ; i++)
{
sw.Reset();
sw.Start();
resultBytes = ArrayCopy(bytes0, bytes1);
sw.Stop();
Console.WriteLine("ArrayCopy Ticks: {0} Time: {1} ms ", sw.ElapsedTicks, sw.ElapsedMilliseconds);
}
Console.ReadKey();
}
static void InitBytes()
{
for(int i=;i<len1;i++)
{
bytes0[i] =(byte)(i % );
}
for (int i = ; i < len2; i++)
{
bytes1[i] = (byte)(i % );
}
}
static byte[] Contact(byte[] bytes0,byte[] bytes1)
{
return bytes0.Concat(bytes1).ToArray();
}
static byte[] BufferCopy(byte[] bytes0,byte[] bytes1)
{
byte[] resultBytes = new byte[bytes0.Length+bytes1.Length];
Buffer.BlockCopy(bytes0, , resultBytes, , bytes0.Length);
Buffer.BlockCopy(bytes1, , resultBytes, bytes0.Length, bytes1.Length);
return resultBytes;
}
static byte[] ArrayCopy(byte[] bytes0, byte[] bytes1)
{
byte[] resultBytes = new byte[bytes0.Length + bytes1.Length];
Array.Copy(bytes0, , resultBytes, , bytes0.Length);
Array.Copy(bytes1, , resultBytes, bytes0.Length, bytes1.Length);
return resultBytes;
}
C# 字节数组拼接的速度实验(Array.copy(),Buffer.BlockCopy(),Contact())的更多相关文章
- C#把某个数组的一部分复制到另一个数组中的两种方法:Buffer.BlockCopy和Array.Copy
static void Main(string[] args) { , , , , , }; ;//目标数组大小 int int_size = sizeof(int);//用于获取值类型的字节大小. ...
- 字节数组与String类型的转换
还是本着上篇文章的原则,只不过在Delphi中string有点特殊! 先了解一下Delphi中的string 1. string = AnsiString = 长字符串,理论上长度不受限制,但其实受限 ...
- 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来。
问题 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来. 代码 data segment arrey db 0,1,2,4,6,5,7,9,8, ...
- [19/04/01-星期一] IO技术_字节流分类总结(含字节数组(Array)流、字节数据(Data)流、字节对象(Object)流)
一.字节流分类概括 -->1.ByteArrayInputStream /ByteArrayOutputStream(数组字节输入输出) InputStream/OutputStr ...
- 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream
一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...
- Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换
public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ publ ...
- PHP 数组拼接成字符串
PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...
- Java 中的字符串与 []byte 字节数组
一.字符串 1.比较 String.HashSet.List 中的 contains 方法 其中, String.List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n).而 Has ...
- Scala字节数组转换为数字
1. 2个字节数组转换为整数 def bytes2uint8(_bytes: Array[Byte], _offset: Int): Int = { val b0 = _bytes(_offset) ...
随机推荐
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- BZOJ2916 [Poi1997]Monochromatic Triangles 数论
答案等于总三角形数-不合法数 一个不合法三角形一定存在两个顶点,在这个三角形中这个顶点的角的两边不同色 #include<cstring> #include<cmath> #i ...
- 讲的很详细的一篇关于object equals() & hashCode() 的文章
转: 讲的很详细的一篇关于object equals() & hashCode() 的文章 哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java ...
- L-Gap Substrings(uva 10829)
题意:有一种形如uvu形式的字符串,其中u是非空字符串,且V的长度正好为L,那么称这个字符串为L-Gap字符串 给出一个字符串S,以及一个正整数L,问S中有多少个L-Gap子串. /* 这道题用到一个 ...
- 【HDOJ5981】Guess the number(DP)
题意:A和B玩一个游戏:A在[L,R]之间随机选取一个数X,之后由B来猜这个数, 如果猜的数比X小,则A就告诉B你猜的数小了, 如果猜的数等于X则游戏结束, 如果猜的数大于X,则在这之后A只会回答B是 ...
- 记录: 一次解决整型溢出攻击(使用scala,隐式转换)
最近项目遇到一次整型溢出攻击 有一个功能,玩家购买num个物品. 每个物品花费14货币. 客户端限制玩家只能购买 1-9999个该物品. 但是某玩家通过技术手段,获得了客户端的运行权限. 于是发送协议 ...
- 连接mysql
1.nuget 所搜MySql.Data 2.appsettings.json { "ConnectionStrings": { "DefaultConnection& ...
- Pacman常用命令
Pacman是Arch Linux 的包管理器.它将一个简单的二进制包格式和易用的构建系统结合了起来.不管软件包是来自官方的 Arch 库还是用户自己创建,Pacman 都能方便得管理. 更新系统 在 ...
- Codeforces 509E Pretty Song (思维)
E. Pretty Song time limit per test:1 seco ...
- weblogic10.3.6忘记用户名或者密码的解决方法
weblogic安装后,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 版本:WebLogic Server 10.3 说明:%DOMAIN_HOME%:指WebLogic Serv ...