- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我对编程还很陌生,而且我对 OOP 的了解有限,因此决定使用事件在我的类之间进行通信。自然,这会导致很多事件。
我想知道使用事件是否有任何额外开销?我假设除非对事件采取行动(即类中有一个监听器根据被触发的事件执行函数),否则实际上不会有太大影响。但我对 C# 中的事件不是很熟悉,只是想确认是否仅仅为了触发一个事件而产生显着的额外开销?
最佳答案
为了防止多年后有人偶然发现这个问题,我使用了 BenchMarkDotNet 框架来衡量事件调用所需的时间。我区分了 1 个订阅者和 100 个订阅者。
使用的测试代码:
private event EventHandler TestEventSingle;
private event EventHandler TestEventMultiple;
public OtherTests()
{
TestEventSingle += OtherTests_TestEvent;
for (int i = 0; i < 100; i++)
{
TestEventMultiple += OtherTests_TestEventMultiple;
}
}
private void OtherTests_TestEventMultiple(object sender, EventArgs e)
{
//Do something with the event...
}
private void OtherTests_TestEvent(object sender, EventArgs e)
{
//Do something with the event...
}
[Benchmark]
public void InvokeEvent()
{
TestEventSingle.Invoke(this, null);
}
[Benchmark]
public void InvokeEventMore()
{
TestEventMultiple.Invoke(this, null);
}
[Benchmark]
public void CallMethod()
{
OtherTests_TestEvent(this, null);
}
[Benchmark]
public void CallMethodMore()
{
for (int i = 0; i < 100; i++)
{
OtherTests_TestEventMultiple(this, null);
}
}
测试结果:
关于c# - 使用事件有多少性能开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20645033/
我想知道,通过数据 channel 发送数据时 WebRTC 会产生多少开销。 我知道 Websockets 每帧有 2 - 14 字节的开销。 WebRTC 是否使用更多开销?我在网上找不到一些有用
我想知道与创建新类而不是该类的新对象相关的开销是小还是大。我正在使用 dojo,但我将提供纯 JS 的示例。我将在启动时创建 10 到 100 个对象,我认为这不会是一个严重的问题,但我想涵盖所有基础
我有一个如下所示的表设置。 Table comment_flags user_id comment_id 我允许用户标记评论,然后给他们取消标记的选项,因为他们可能犯了一个错误。 问题
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: In MySQL what does “Overhead” mean, what is bad about it,
我正在制作一个非常简单的游戏,只是为了好玩/练习,但无论它现在有多简单,我仍然想很好地编写它,以防我想回到它并只是为了学习 因此,在这种情况下,我的问题是: 对象分配涉及多少开销?解释器对此的优化程度
我有一些资源敏感的东西要写。我想知道与仅将这些变量一起传递(例如作为函数参数)相比,在结构中将变量组合在一起是否真的会导致内存开销。 如果是这样,那么在不产生开销的情况下创建对惰性值进行操作的东西的好
我一直在开发一个实时应用程序,并注意到一些 OOP 设计模式在 Python 中引入了难以置信的开销(使用 2.7.5 进行了测试)。 直截了当,当字典被另一个对象封装时,为什么简单的字典值访问器方法
我正在从 ifstream 中读取随机 ascii 文本文件。我需要能够将整个消息放入字符串类型以进行字符解析。我当前的解决方案有效,但我认为我通过使用等效于此的方式来谋杀更冗长文件的处理时间: st
纯粹从软件工程的角度来看,getActivity() 有多少开销? 我在整个应用程序中经常多次使用此方法,并考虑使用一个引用 getActivity() 的全局变量。 如果为 Activity 设置一
我一直在研究 Riccardo Terrell 的 Akka.NET 分形演示 (https://github.com/rikace/akkafractal) 以尝试理解它。 (这很棒,顺便说一句)
我正在尝试使用高分辨率计时器查找我的代码运行时间,我注意到计时器的结果不一致,我想知道为什么会这样。 我找到了这篇文章 How do you test running time of VBA code
我正在学习WPF。我现在开始装订了。使用 INotifyPropertyChanged 时绑定(bind)是否依赖反射?是这样,价格是多少?我正在考虑使用 WPF 来显示通过 UDP 流式传输的数据,
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
使用GHC.TypeLits中的Sing有任何开销吗? ?以程序为例: {-# LANGUAGE DataKinds #-} module Test (test) where import GHC.T
我有某种模板化基类 template class Base { }; 并希望将其派生实例存储在列表中。为此,我使用 using derived_handle = std::unique_ptr v
我有一个 ORM sqlalchemy 模型,我需要构建一个查询(使用 ORM 类更容易构建),但这需要大量时间。当我直接像 SQL 一样向数据库执行相同的查询时,速度相当快。 使用 SQLAlche
我在 PHP 平台上有一家商店(开发不善),那里有很多不好的查询(没有索引的长查询、rand() 排序、动态计数,..) 我现在无法更改查询,但我必须调整服务器才能保持事件状态。 我尝试了我所知道的一
我有一个使用 JQuery mobile 构建的移动应用程序,响应时间对我来说非常重要,因为我希望为我的用户提供流畅的体验。 我刚刚将网站的安装移至本地服务器,以提高应用程序的性能,因为它连接到本地
关于数据库设计的问题。如果我有 28 个 bool 值并且能够将它们添加为每行 28 个 bool 值或一个整数,哪一个会更快?哪种方法将使磁盘上的表大小保持最低? 这是在假设我需要的可以通过查询中的
我有一个看起来像 Boost.Array 的简单类。有两个模板参数 T 和 N。Boost.Array 的一个缺点是,每个使用这种数组的方法都必须是带有参数 N 的模板(T 可以)。结果是整个程序往往
我是一名优秀的程序员,十分优秀!