- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要不断地向委托(delegate)添加和删除操作,我应该使用哈希集而不是操作吗?
我目前拥有的:
System.Action _callbacks =null;
//...somewhere in code, from many different places (2000 places or so, every frame):
_callbacks -= anyFunction;
_callbacks += someOtherFunc;
我可能会使用什么(基本上是回调的哈希集):
Hashset<FastFunc> _callbacks = new Hashset<FastFunc>();
//...somewhere in code, from many different places
_callbacks.add( new FastFunc(someOtherFunc) );
// works A LOT faster with hashsets that System.Action does. //(March2019 profiler)
// It remembers its hashcode once, then can provide it super-quickly every time it's required.
public class FastFunc{
public System.Action _func;
private int _hashCode;
public FastFunc(System.Action function){
_func = function;
remember_HashCode(function);
}
public override int GetHashCode(){
return _hashCode;
}
// important to override Equals as well, so that objects created from the same method
// will be identical.
public override bool Equals(object other){
System.Action otherAction = ((FastFunc)other)._func;
bool isEqual = otherAction.Method.Equals( _func.Method ) && otherAction.Target.Equals( _func.Target );
return isEqual;
}
//only called once, during constructor
void remember_HashCode(System.Action myFunc){
_hashCode = myFunc.GetHashCode();
}
}
使用这个 -= 和 += 运算符是否可以订阅 System.Action
?
它是否在幕后使用了一个数组,如果我们从它的起始索引中删除一些函数,它就必须被移回?如果是这样,我想 hashset 会是更好的选择 + 没有重复项。
最佳答案
多播委托(delegate)的调用列表是数组。
https://referencesource.microsoft.com/#mscorlib/system/multicastdelegate.cs,284
(文档已过时(代表们可能在 .Net 1.0 中使用过 linked-linsts,但不再使用)https://learn.microsoft.com/en-us/dotnet/api/system.multicastdelegate?view=netframework-4.8 )
您可以使用您的方法使添加/删除操作更快。事实上,AFAIR,WPF 就是这样做的。
不过你需要小心多线程。
关于c# - System.Action 内部是一个数组吗? += 和 -= 运算符的效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922311/
第一个 .on 函数比第二个更有效吗? $( "div.container" ).on( "click", "p", function(){ }); $( "body" ).on( "click",
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 7 年前。 Improve
我有这样的查询: $('#tabContainer li'); JetBrains WebStorm IDE 将其突出显示为低效查询。它建议我改用这个: $('#tabContainer').find
我刚刚在 coursera ( https://www.coursera.org/saas/) 上听了一个讲座,教授说 Ruby 中的一切都是对象,每个方法调用都是在对象上调用发送方法,将一些参数传递
这可能是用户“不喜欢”的另一个问题,因为它更多的是与建议相关而不是与问题相关。 我有一个在保存和工作簿打开时触发的代码。 它在 f(白天与夜晚,日期与实际日期)中选择正确的工作表。 周一到周三我的情况
这只是我的好奇心,但是更有效的是递归还是循环? 给定两个功能(使用通用lisp): (defun factorial_recursion (x) (if (> x 0) (*
这可能是一个愚蠢的问题,但是while循环的效率与for循环的效率相比如何?我一直被教导,如果可以使用for循环,那我应该这样做。但是,实际上之间的区别是什么: $i = 0; while($i <
我有一个Elasticsearch索引,其中包含几百万条记录。 (基于时间戳的日志记录) 我需要首先显示最新记录(即,按时间戳降序排列的记录) 在时间戳上排序desc是否比使用时间戳的函数计分功能更有
使用Point2D而不是double x和y值时,效率有很大差异吗? 我正在开发一个程序,该程序有许多圆圈在屏幕上移动。他们各自从一个点出发,并越来越接近目的地(最后,他们停下来)。 使用 .getC
我正在编写一个游戏,并且有一个名为 GameObject 的抽象类和三个扩展它的类(Player、Wall 和 Enemy)。 我有一个定义为包含游戏中所有对象的列表。 List objects; 当
我是 Backbone 的初学者,想知道两者中哪一个更有效以及预期的做事方式。 A 型:创建一个新集合,接受先前操作的结果并从新集合中提取 key result = new Backbone.Coll
最近,关于使用 LIKE 和通配符搜索 MS SQL 数据库的最有效方法存在争论。我们正在使用 %abc%、%abc 和 abc% 进行比较。有人说过,术语末尾应该始终有通配符 (abc%)。因此,根
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我想知道,这样做会更有效率吗: setVisible(false) // if the component is invisible 或者像这样: if(isVisible()){
我有一个静态方法可以打开到 SQL Server 的连接、写入日志消息并关闭连接。我在整个代码中多次调用此方法(平均每 2 秒一次)。 问题是 - 它有效率吗?我想也许积累一些日志并用一个连接插入它们
这个问题在这里已经有了答案: Best practice to avoid memory or performance issues related to binding a large numbe
我为我的 CS 课(高中四年级)制作了一个石头剪刀布游戏,我的老师给我的 shell 文件指出我必须将 do while 循环放入运行者中,但我不明白为什么?我的代码可以工作,但她说最好把它写在运行者
我正在编写一个需要通用列表的 Java 应用程序。该列表需要能够经常动态地调整大小,对此的明显答案是通用的Linkedlist。不幸的是,它还需要像通过调用索引添加/删除值一样频繁地获取/设置值。 A
我的 Mysql 语句遇到了真正的问题,我需要将几个表连接在一起,查询它们并按另一个表中值的平均值进行排序。这就是我所拥有的... SELECT ROUND(avg(re.rating
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Is there a difference between i==0 and 0==i? 以下编码风格有什么
我是一名优秀的程序员,十分优秀!