gpt4 book ai didi

c# - 为什么c#内置的IO类比自制的快?

转载 作者:可可西里 更新时间:2023-11-01 09:15:41 24 4
gpt4 key购买 nike

当我决定在 C# 中自己实现 Java ByteBuffer 时,我认为它会比 MemoryStream + BinaryWriter/二进制阅读器。我通过 ILSpy 查看了它们的源代码,发现有很多检查和辅助方法调用,而在我的实现中,我直接使用底层字节数组。但是,当测试表明重型内置类的方法调用比我的轻型方法调用快将近两倍时,我感到非常惊讶。

例如:

public void WriteBytes(Byte[] buffer, Int32 offset, Int32 count)
{
this.EnsureFreeSpace(count);

Buffer.BlockCopy(buffer, offset, this.buffer, this.position, count);

this.position += count;
if (this.length < this.position)
{
this.length = this.position;
}
}

public void ReadBytes(Byte[] buffer, Int32 offset, Int32 count)
{
this.EnsureDataExist(count);

Buffer.BlockCopy(this.buffer, this.position, buffer, offset, count);

this.position += count;
}

private void EnsureFreeSpace(Int32 count)
{
if (this.buffer.Length - this.position < count)
{
throw new InvalidOperationException();
}
}

private void EnsureDataExist(Int32 count)
{
if (this.length - this.position < count)
{
throw new InvalidOperationException();
}
}

~比

慢1.5-2倍
memoryStream.Write(...)
memoryStream.Read(...)

在这个简单的测试中

Byte[] temp = new byte[64];
stopWatch.Restart();
for (int i = 0; i < 100000; i++)
{
ms.Write(temp, 0, temp.Length);
ms.Position = 0;
ms.Read(temp, 0, temp.Length);
ms.Position = 0;
}
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds);

stopWatch.Restart();
for (int i = 0; i < 100000; i++)
{
mb.WriteBytes(temp, 0, temp.Length);
mb.Position = 0;
mb.ReadBytes(temp, 0, temp.Length);
mb.Position = 0;
}
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds);

为什么?

在所有测试中都启用了优化。在 Debug模式下,平均差异如我所说的 ~1.7 倍。在 Release 模式下 ~1.3 倍,更少但仍然存在。

编辑多亏了这些建议,我发现在 Visual Studio 之外,我的代码比内置代码快几倍或至少一样快。那么现在的问题是,为什么会这样?

最佳答案

我的看法是您未能正确测试其性能。这个主题之前已经讨论过多次,我发现 Eric Lippert 的这个博客系列非常有启发性:http://tech.pro/blog/1293/c-performance-benchmark-mistakes-part-one

作为快捷方式,执行以下操作:将整个测试代码放在一个 for 循环中并运行几次,比较输出并始终丢弃第一个结果。

关于c# - 为什么c#内置的IO类比自制的快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19652165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com