为什么反思慢? [英] Why is reflection slow?

查看:82
本文介绍了为什么反思慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是因为我们应该加载类(例如 string ),创建实例,然后搜索适当的方法,打包参数,然后只调用方法?所以大部分时间花在这些操作上而不是对象上的显式方法调用,对吗?

解决方案

你采取的每一步都需要每次使用反射时都要验证。例如,当你调用一个方法时,它需要检查目标是否实际上是方法的声明者的实例,你是否有正确数量的参数,每个参数是否是正确的类型等等。 / p>

绝对没有内联或其他表演技巧的可能性。



如果您按名称查找类型或方法,这最多只涉及一个简单的地图查找 - 每次执行时都会执行,而不是在JIT时执行一次。



基本上还有很多东西要做做。然而,反射已经变得比以前快得多......如果你觉得它太慢了,你可能会过度使用它。


Is it because we should load class (by string for example), create instance, then search for appropriate method, pack parameters, and then just invoke method? So most time is spent on these operations instead of explicit method invocation on an object, right?

解决方案

Every step you take needs to be validated every time you take it when you use reflection. For example, when you invoke a method, it needs to check whether the target is actually an instance of the declarer of the method, whether you've got the right number of arguments, whether each argument is of the right type, etc.

There's absolutely no possibility of inlining or other performance tricks.

If you're finding types or methods by name, that's at best going to involve a simple map lookup - which will be performed every time you execute it, rather than once at JIT time.

Basically there's a lot more to do. However, reflection has become a lot faster than it used to be... if you're finding it much too slow, you may well be overusing it.

这篇关于为什么反思慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆