zoukankan      html  css  js  c++  java
  • C# 字节数组拼接的速度实验(Array.copy(),Buffer.BlockCopy(),Contact())

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

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

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

    测试源码如下:

            static int len1 = 1470;
            static int len2 = 906;
            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(1);
                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 < 1200)  // A Warmup of 1000-1500 mS 
                                                       // stabilizes the CPU cache and pipeline.
                {
                    resultBytes = Contact(bytes0, bytes1);       // Warmup
                }
                sw.Stop();
                for (int i = 0; i < 10; 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 < 1200)  // A Warmup of 1000-1500 mS 
                                                       // stabilizes the CPU cache and pipeline.
                {
                    resultBytes = BufferCopy(bytes0, bytes1);       // Warmup
                }
                sw.Stop();
                for (int i = 0; i < 10; 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 < 1200)  // A Warmup of 1000-1500 mS 
                                                       // stabilizes the CPU cache and pipeline.
                {
                    resultBytes = ArrayCopy(bytes0, bytes1);       // Warmup
                }
                sw.Stop();
                for (int i = 0; i < 10; 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=0;i<len1;i++)
                {
                    bytes0[i] =(byte)(i % 255);
                }
                for (int i = 0; i < len2; i++)
                {
                    bytes1[i] = (byte)(i % 255);
                }
            }
            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, 0, resultBytes, 0, bytes0.Length);
                Buffer.BlockCopy(bytes1, 0, 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, 0, resultBytes, 0, bytes0.Length);
                Array.Copy(bytes1, 0, resultBytes, bytes0.Length, bytes1.Length);
                return resultBytes;
            }
  • 相关阅读:
    struts2接收参数的几种形式
    oracle merge into函数中插入clob字段
    程序员能力矩阵
    spring mvc工作原理
    struts2核心和工作原理
    mysql主从复制(windows下)
    mysql主从复制(linux下)
    spring 注解事务
    异常错误集锦
    Redis 作为缓存服务器的配置
  • 原文地址:https://www.cnblogs.com/cangyue080180/p/5905414.html
Copyright © 2011-2022 走看看