As the title says really. Does the virtual keyword cause a performance hit?
正如标题所说的那样。虚拟关键字是否会导致性能下降?
2 个解决方案
#1
3
First off, in Actionscript you don't need to explicitly mark a method to be overridden as virtual as you do in C#, the compiler will do this for you. This is why the keyword doesn't show up in any docs because it is irrelevant from the developers perspective.
首先,在Actionscript中,您不需要像在C#中那样显式地标记要被重写为虚拟的方法,编译器将为您执行此操作。这就是关键字没有出现在任何文档中的原因,因为它与开发人员的观点无关。
To answer your question though, which is whether overriding a method makes it slower, the answer is no as long as your class is sealed, meaning you don't use the 'dynamic' keyword when defining the class.
要回答你的问题,即重写一个方法是否会使它变慢,只要你的类被密封,答案就没有了,这意味着你在定义类时不使用'dynamic'关键字。
The reason why is that when constructing a sealed class, you're going to have explicit markers to every method for that object, when overriding a method you get a marker directly to the new function, the class does not have to look it up at runtime like in a dynamic class.
原因在于,当构造一个密封类时,你将为该对象的每个方法都有显式标记,当覆盖一个方法直接获得一个标记到新函数时,该类不必查找它运行时就像在动态类中一样。
#2
1
All methods are virtual in AS3 right now. But my guess is 'virtual' will become enforced in a future version of AS poss. via some compiler option setting.
所有方法现在都在AS3中是虚拟的。但我的猜测是“虚拟”将在AS poss的未来版本中强制执行。通过一些编译器选项设置。
#1
3
First off, in Actionscript you don't need to explicitly mark a method to be overridden as virtual as you do in C#, the compiler will do this for you. This is why the keyword doesn't show up in any docs because it is irrelevant from the developers perspective.
首先,在Actionscript中,您不需要像在C#中那样显式地标记要被重写为虚拟的方法,编译器将为您执行此操作。这就是关键字没有出现在任何文档中的原因,因为它与开发人员的观点无关。
To answer your question though, which is whether overriding a method makes it slower, the answer is no as long as your class is sealed, meaning you don't use the 'dynamic' keyword when defining the class.
要回答你的问题,即重写一个方法是否会使它变慢,只要你的类被密封,答案就没有了,这意味着你在定义类时不使用'dynamic'关键字。
The reason why is that when constructing a sealed class, you're going to have explicit markers to every method for that object, when overriding a method you get a marker directly to the new function, the class does not have to look it up at runtime like in a dynamic class.
原因在于,当构造一个密封类时,你将为该对象的每个方法都有显式标记,当覆盖一个方法直接获得一个标记到新函数时,该类不必查找它运行时就像在动态类中一样。
#2
1
All methods are virtual in AS3 right now. But my guess is 'virtual' will become enforced in a future version of AS poss. via some compiler option setting.
所有方法现在都在AS3中是虚拟的。但我的猜测是“虚拟”将在AS poss的未来版本中强制执行。通过一些编译器选项设置。