动态调用方法时的性能差异
这两个示例方法调用之间是否存在很大的性能差异?
SampleClass sc = new SampleLib.SampleClass();
sc.DoSomething("Hello");
dynamic dyn = someAssembly.CreateInstance("SampleLib.SampleClass")
dyn.DoSomething("Hello");
让我们假设
动态动态 = someAssembly.CreateInstance("SampleLib.SampleClass")
不经常调用。创建实例后,它将仅继续集中处理已创建的实例。
Is there a big performace difference between these 2 sample method calls ?
SampleClass sc = new SampleLib.SampleClass();
sc.DoSomething("Hello");
dynamic dyn = someAssembly.CreateInstance("SampleLib.SampleClass")
dyn.DoSomething("Hello");
Lets assume that
dynamic dyn =
someAssembly.CreateInstance("SampleLib.SampleClass")
is not frequent call. Once it creates the instance it will continue intensivly work only with created instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一确定的方法是在您的上下文中进行分析。为了设置期望,
dynamic
非常聪明,并且缓存(每个模式)实际的代码路径。因此,它比原始反射快得多,但是接口应该稍微快一些,并且具有静态检查的优点。就我个人而言,我会从当前代码和 SampleLib 都可以引用的库 dll 中编写接口,即
The only way to know for sure is to profile in your context. To set expectation,
dynamic
is pretty smart, and caches (per pattern) the actual code-path. As such, it is much faster than raw reflection, however an interface should be slightly faster and has the advantage of static checking.Personally, I'd code to an interface from a library dll that both the current code and SampleLib can reference, i.e.