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

标签 c# performance io stream

当我决定在 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/

相关文章:

c# - 防止 BroadcastBlock 在 LinkTo 上发送缓冲消息

c - 将 float 写入数组需要太多时间

java - Java 编写互斥锁的最快方法?

c# - Directory.Exists 对时间敏感吗?

python - 从目录读取 csv 文件并将数据存储到多索引 Pandas 数据框

javascript - 如何从 Javascript FileReader 获取文件名?

c# - 在 Compact Framework 中使用 DLL VB6

c# - 使用 C# 和 HTTPrequest 登录网站

c# - 我首先使用实体​​框架数据库,我想将连接字符串存储在 Key Vault 中,并且需要使用 C# 代码中的应用程序设置从代码进行访问?

C++ vector 问题