为什么反射会减慢Android手机的速度
我多次读到反射会降低手机性能。 这有多真实?
例如,在我的例子中,我从 Web 服务获取一些参数,这些参数与我在 Android 应用程序中的类的参数同名。所以我只是使用java字段和反射设置这些参数的值...它似乎并没有降低性能...
任何人都可以向我解释反射降低性能这一想法背后的事实吗?
I read many times that reflection will slow down the phone performance.
How true is this?
For example in my case, I get some parameters from a web service which have the same name as the parameters of a class I have in my android app. so I just set the values of these parameters using java fields and reflection... it doesn't seem to slow down the performance..
can anybody explain to me the fact behind this idea about reflection slowing down performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个问题。基本上,您超出了编译器可以执行的优化范围,因为反射是动态发生的。
如果您没有进行大量反射调用(例如,在 ListView 的 getView 内部执行操作会很糟糕),您可能可以摆脱它。它是用来使用的,只是要谨慎使用。
Take a look at this question. Basically, you are getting outside of the optimizations that the compiler can perform because reflection happens dynamically.
If you're not making a lot of reflection calls (e.g., it would be bad to do inside the getView of a ListView), you can probably get away with it. It's there to be used, just be judicious about it.
它比不使用反射慢。这绝对是您在循环或快速 UI 处理(例如,滚动 ListView)期间要避免的事情。
它确实如此,尽管在这种情况下用户可能不会注意到。
请参阅@Brian Cooley 在其答案中提供的链接。请记住,Dalvik(Android 中的虚拟机)上的反射可能比 Java VM 上的反射慢——无论如何,我确实相当确定它不会更快。
It is slower than not using reflection. It is definitely something you want to avoid in loops or during rapid UI processing (e.g., scrolling a ListView).
It does, though it may not be noticeable to the user in this case.
See the link provided by @Brian Cooley in his answer. Bear in mind that reflection on Dalvik (the virtual machine in Android) may be slower than reflection on the Java VM -- I am really rather certain it is not faster, at any rate.