无聊做了如题的一个算法的优劣性能比较,由于很多人都只关心结果,那么我先贴出结果如下:

由于我的测试数据量比较小,只能得出Array.Copy()和Buffer.BlockCopy()方法性能要好于Contact(),这个不用比较也能想到,如果想知道前两个谁的性能更好,

有兴趣的可以修改源码中的测试数据量就可以了。

测试源码如下:

  1. static int len1 = ;
  2. static int len2 = ;
  3. static byte[] bytes0 = new byte[len1];
  4. static byte[] bytes1 = new byte[len2];
  5. static void Main(string[] args)
  6. {
  7. // Uses the second Core or Processor for the Test
  8. Process.GetCurrentProcess().ProcessorAffinity = new IntPtr();
  9. Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
  10. // Prevents "Normal" Threads
  11. Thread.CurrentThread.Priority = ThreadPriority.Highest;
  12. Stopwatch sw = new Stopwatch();
  13. byte[] resultBytes = null;
  14. InitBytes();
  15.  
  16. //test
  17. sw.Reset();
  18. sw.Start();
  19. while (sw.ElapsedMilliseconds < ) // A Warmup of 1000-1500 mS
  20. // stabilizes the CPU cache and pipeline.
  21. {
  22. resultBytes = Contact(bytes0, bytes1); // Warmup
  23. }
  24. sw.Stop();
  25. for (int i = ; i < ; i++)
  26. {
  27. sw.Reset();
  28. sw.Start();
  29. resultBytes = Contact(bytes0, bytes1);
  30. sw.Stop();
  31. Console.WriteLine("Contact Ticks: {0} Time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
  32. }
  33. sw.Reset();
  34. sw.Start();
  35. while (sw.ElapsedMilliseconds < ) // A Warmup of 1000-1500 mS
  36. // stabilizes the CPU cache and pipeline.
  37. {
  38. resultBytes = BufferCopy(bytes0, bytes1); // Warmup
  39. }
  40. sw.Stop();
  41. for (int i = ; i < ; i++)
  42. {
  43. sw.Reset();
  44. sw.Start();
  45. resultBytes = BufferCopy(bytes0, bytes1);
  46. sw.Stop();
  47. Console.WriteLine("BufferCopy Ticks: {0} Time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
  48. }
  49. sw.Reset();
  50. sw.Start();
  51. while (sw.ElapsedMilliseconds < ) // A Warmup of 1000-1500 mS
  52. // stabilizes the CPU cache and pipeline.
  53. {
  54. resultBytes = ArrayCopy(bytes0, bytes1); // Warmup
  55. }
  56. sw.Stop();
  57. for (int i = ; i < ; i++)
  58. {
  59. sw.Reset();
  60. sw.Start();
  61. resultBytes = ArrayCopy(bytes0, bytes1);
  62. sw.Stop();
  63. Console.WriteLine("ArrayCopy Ticks: {0} Time: {1} ms ", sw.ElapsedTicks, sw.ElapsedMilliseconds);
  64. }
  65. Console.ReadKey();
  66. }
  67. static void InitBytes()
  68. {
  69. for(int i=;i<len1;i++)
  70. {
  71. bytes0[i] =(byte)(i % );
  72. }
  73. for (int i = ; i < len2; i++)
  74. {
  75. bytes1[i] = (byte)(i % );
  76. }
  77. }
  78. static byte[] Contact(byte[] bytes0,byte[] bytes1)
  79. {
  80. return bytes0.Concat(bytes1).ToArray();
  81. }
  82. static byte[] BufferCopy(byte[] bytes0,byte[] bytes1)
  83. {
  84. byte[] resultBytes = new byte[bytes0.Length+bytes1.Length];
  85. Buffer.BlockCopy(bytes0, , resultBytes, , bytes0.Length);
  86. Buffer.BlockCopy(bytes1, , resultBytes, bytes0.Length, bytes1.Length);
  87. return resultBytes;
  88. }
  89. static byte[] ArrayCopy(byte[] bytes0, byte[] bytes1)
  90. {
  91. byte[] resultBytes = new byte[bytes0.Length + bytes1.Length];
  92. Array.Copy(bytes0, , resultBytes, , bytes0.Length);
  93. Array.Copy(bytes1, , resultBytes, bytes0.Length, bytes1.Length);
  94. return resultBytes;
  95. }

C# 字节数组拼接的速度实验(Array.copy(),Buffer.BlockCopy(),Contact())的更多相关文章

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

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

  2. 字节数组与String类型的转换

    还是本着上篇文章的原则,只不过在Delphi中string有点特殊! 先了解一下Delphi中的string 1. string = AnsiString = 长字符串,理论上长度不受限制,但其实受限 ...

  3. 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来。

    问题 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来. 代码 data segment arrey db 0,1,2,4,6,5,7,9,8, ...

  4. [19/04/01-星期一] IO技术_字节流分类总结(含字节数组(Array)流、字节数据(Data)流、字节对象(Object)流)

    一.字节流分类概括 -->1.ByteArrayInputStream /ByteArrayOutputStream(数组字节输入输出)        InputStream/OutputStr ...

  5. 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream

    一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...

  6. Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换

    public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ publ ...

  7. PHP 数组拼接成字符串

    PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...

  8. Java 中的字符串与 []byte 字节数组

    一.字符串 1.比较 String.HashSet.List 中的 contains 方法 其中, String.List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n).而 Has ...

  9. Scala字节数组转换为数字

    1. 2个字节数组转换为整数 def bytes2uint8(_bytes: Array[Byte], _offset: Int): Int = { val b0 = _bytes(_offset) ...

随机推荐

  1. BZOJ1487 [HNOI2009]无归岛 【仙人掌dp】

    题目链接 BZOJ1487 题解 就是一个简单的仙人掌最大权独立集 还是不会圆方树 老老实实地树形Dp + 环处理 #include<iostream> #include<cstdi ...

  2. 设置pycharm的python版本

    http://blog.csdn.net/github_35160620/article/details/52486986

  3. redis windows安装

    下载:https://github.com/MicrosoftArchive/redis/releases 命令行启动:redis-server.exe redis.windows.conf 以服务启 ...

  4. mac 安装 maven 配置

    前面的话: 记录 在 Mac 下 安装配置 maven 1. 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 2. 打开 Terminal, ...

  5. RowFilter 对于已获取到的dataset进行过滤

    原文发布时间为:2009-11-12 -- 来源于本人的百度文章 [由搬家工具导入] DataView的属性RowFilter使用方法 p.s. 重点在于DataView是DateTable相关联的一 ...

  6. jquery text

    scenario: need to display raw xml, what does text() method do: <> converted to <> i.e. 把 ...

  7. 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 ...

  8. 插入排序Insertion sort(转)

    插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕.     插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内 ...

  9. Python学习杂记_7_文件操作

    文件操作 Python3用open()方法打开文件并返回文件句柄,有了文件句柄就可以对文件进行各种操作了. 打开文件: open(“文件名” , 打开方式)            如: f=open( ...

  10. HNOI 2006 BZOJ 1195 最短母串

    题面 问题描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字 ...