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) ...
随机推荐
- BZOJ1487 [HNOI2009]无归岛 【仙人掌dp】
题目链接 BZOJ1487 题解 就是一个简单的仙人掌最大权独立集 还是不会圆方树 老老实实地树形Dp + 环处理 #include<iostream> #include<cstdi ...
- 设置pycharm的python版本
http://blog.csdn.net/github_35160620/article/details/52486986
- redis windows安装
下载:https://github.com/MicrosoftArchive/redis/releases 命令行启动:redis-server.exe redis.windows.conf 以服务启 ...
- mac 安装 maven 配置
前面的话: 记录 在 Mac 下 安装配置 maven 1. 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 2. 打开 Terminal, ...
- RowFilter 对于已获取到的dataset进行过滤
原文发布时间为:2009-11-12 -- 来源于本人的百度文章 [由搬家工具导入] DataView的属性RowFilter使用方法 p.s. 重点在于DataView是DateTable相关联的一 ...
- jquery text
scenario: need to display raw xml, what does text() method do: <> converted to <> i.e. 把 ...
- AtCoder Regular Contest 090 F - Number of Digits
题目链接 Description For a positive integer \(n\), let us define \(f(n)\) as the number of digits in bas ...
- 插入排序Insertion sort(转)
插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内 ...
- Python学习杂记_7_文件操作
文件操作 Python3用open()方法打开文件并返回文件句柄,有了文件句柄就可以对文件进行各种操作了. 打开文件: open(“文件名” , 打开方式) 如: f=open( ...
- HNOI 2006 BZOJ 1195 最短母串
题面 问题描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字 ...