- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当我决定在 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/
我正在使用Delphi 7,并且有一个新单元要在我的项目中使用。我已经编译了新的。当我尝试通过将其添加到uses子句在项目中使用此单元时,出现错误,提示未找到.dcu文件。我还尝试将文件的完整路径放在
我有一个项目正在使用 mysql_connect()。 (我无法将其更改为 mysqli) 我收到错误:Call to undefined function mysql_connect() 我已经使用
我现在正在学习 jQuery,第一个“更大”的项目是一个自制的 Accordion /滑动导航。 我的 HTML 代码如下所示: Prologue TEST Projekte
我得到了这段用 JavaScript 编写的代码,但是对于大输入它返回了错误的数字。 它应该用模(mo)计算指数(ex)幂的底数。 我用 C 编写了等效代码并且正在运行。请有人告诉我出了什么问题。 尝
我正在通读 How can I write a power function myself? dan04 给出的答案引起了我的注意,主要是因为我不确定 fortran 给出的答案,但我接受了它并实现了
更新:可能是 jQuery 的 trigger() 在测试中做了一些额外的工作,我打开了一个 issue在 github 上。 ===== 我正在关注 learnQuery构建我的简单 jQuery。
我尝试了各种图库插件,但没有一个适合我的需要。 我有一些标记如下:
为了熟悉 swift 语言和 Cocoa,我决定创建一个小的基数转换,首先从基数 2 到基数 10,然后我想添加一些其他基数,例如 16 和 5。我仍然无法使其正常工作:Running App Vie
我开始编写自己的 vector 类,然后着手重载 = 运算符。我在下面有类似 somevector = someothervector 的代码,尽管我不确定它是否正确,因为我一直在为类似 someve
这里有一些非常相似的问题,但是它们无法帮助我解决这个问题。 另外,我提供了完整的示例代码,因此其他人可能更容易理解。 我制作了一个 vector 容器(出于内存原因,不能使用STL),过去只将oper
我制作了自己的 vector 模板,operator[]一部分: template T& vector::operator[](unsigned int index) { return m_
我们知道我们不应该写 SELECT *;相反,应该写下确实将在业务逻辑中使用的列(例如,在有关整篇文章的一次查询中SELECT blog_title, blog_content,以及SELECT bl
我使用 netbeans,当我准备为应用程序用户的管理创建一个 bean 时,我开始想知道:哪种方式更有益,保留数据库表并在其上应用 EJB,还是创建一个处理与数据库的连接并“手动”进行测试的 bea
所以基本上,作为安全措施(和学习过程),我尝试做的是我自己的“Capthca”系统。发生的情况是我有 20 个“标签”(为简洁起见,下面仅显示一个),每个 ID 都在 1 到 20 之间。我的 jav
我有一个类,它本质上是一个 std::vector具有一些附加功能。类(class)有find(const T& value )返回 value 第一次出现索引的方法或-1: int my::find
最近几天,我一直在尝试使用 Python 进行一些音频开发。 问题是,Mac OSX 不能很好地处理卸载问题。实际上,没有办法卸载任何东西。一旦它出现在您的系统上,您最好祈祷它没有做任何有趣的事情。因
我不明白以下错误消息,但我知道我之前已经在 Linux 上安装了 utf8-light ,没有出现任何问题。有人可以阐明出了什么问题吗?这是 LLVM 问题、GHC 7 问题还是 utf8-light
我是一名优秀的程序员,十分优秀!