C#中copy的效率问题,很不错,学习!
2016-06-07 11:21
465 查看
在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadBytes,Buffer.BlockCopy,以及System.Buffer.memcpyimpl,由于最后一种需要使用指针,所以本文不引入该方法。
本次测试,使用以上前4种方法,各运行1000万次,观察结果。
[csharp] view
plain copy
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace BenchmarkCopyArray
{
class Program
{
private const int TestTimes = 10000000;
static void Main()
{
var testArrayCopy = new TestArrayCopy();
TestCopy(testArrayCopy.TestBinaryReader, "Binary.ReadBytes");
TestCopy(testArrayCopy.TestConvertToList, "ConvertToList");
TestCopy(testArrayCopy.TestArrayDotCopy, "Array.Copy");
TestCopy(testArrayCopy.TestBlockCopy, "Buffer.BlockCopy");
Console.Read();
}
private static void TestCopy(Action testMethod, string methodName)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < TestTimes; i++)
{
testMethod();
}
testMethod();
stopWatch.Stop();
Console.WriteLine("{0}: {1} seconds, {2}.", methodName, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds);
}
}
class TestArrayCopy
{
private readonly byte[] _sourceBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public void TestBinaryReader()
{
var binaryReader = new BinaryReader(new MemoryStream(_sourceBytes));
binaryReader.ReadBytes(_sourceBytes.Length);
}
public void TestConvertToList()
{
IList<byte> bytesSourceList = new List<byte>(_sourceBytes);
var bytesNew = new byte[_sourceBytes.Length];
bytesSourceList.CopyTo(bytesNew, 0);
}
public void TestArrayDotCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Array.Copy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
public void TestBlockCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Buffer.BlockCopy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
}
}
运行结果如下:
![](http://img.blog.csdn.net/20130816112323625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmd6aGFuY2hhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
本次测试,使用以上前4种方法,各运行1000万次,观察结果。
[csharp] view
plain copy
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace BenchmarkCopyArray
{
class Program
{
private const int TestTimes = 10000000;
static void Main()
{
var testArrayCopy = new TestArrayCopy();
TestCopy(testArrayCopy.TestBinaryReader, "Binary.ReadBytes");
TestCopy(testArrayCopy.TestConvertToList, "ConvertToList");
TestCopy(testArrayCopy.TestArrayDotCopy, "Array.Copy");
TestCopy(testArrayCopy.TestBlockCopy, "Buffer.BlockCopy");
Console.Read();
}
private static void TestCopy(Action testMethod, string methodName)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < TestTimes; i++)
{
testMethod();
}
testMethod();
stopWatch.Stop();
Console.WriteLine("{0}: {1} seconds, {2}.", methodName, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds);
}
}
class TestArrayCopy
{
private readonly byte[] _sourceBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public void TestBinaryReader()
{
var binaryReader = new BinaryReader(new MemoryStream(_sourceBytes));
binaryReader.ReadBytes(_sourceBytes.Length);
}
public void TestConvertToList()
{
IList<byte> bytesSourceList = new List<byte>(_sourceBytes);
var bytesNew = new byte[_sourceBytes.Length];
bytesSourceList.CopyTo(bytesNew, 0);
}
public void TestArrayDotCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Array.Copy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
public void TestBlockCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Buffer.BlockCopy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
}
}
运行结果如下:
相关文章推荐
- C# 中的memset实现
- C# windows服务打开网页或者应用程序
- C#中的WebBrowser控件加载ActiveX插件
- C# 预处理器指令列表
- C#递归方法实现无限级分类显示效果实例
- c#可重载和不可重载运算符
- C# 中的析构函数
- C# Struct的特点
- C# 操作 Excel 文件(.xls 或 .xlsx)
- c#转换ASCII,很不错
- C#委托和事件的困惑
- C#之五十 扫雷
- C#之五十 扫雷
- C#之五十 扫雷
- C#之四十九 游戏编程周每日总结
- C#之四十九 游戏编程周每日总结
- C#之四十九 游戏编程周每日总结
- C#之四十八 俄罗斯方块设计
- C#之四十八 俄罗斯方块设计
- C#之四十八 俄罗斯方块设计