Java Reflection: Why is it so slow?

后端 未结 6 1141
独厮守ぢ
独厮守ぢ 2020-11-29 17:30

I\'ve always avoided Java reflection soley based on its reputation for slowness. I reached a point in the design of my current project where being able to use it would make

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 18:16

    • Reflection was very slow when first introduced, but has been sped up considerably in newer JREs
    • Still, it might not be a good idea to use reflection in an inner loop
    • Reflection-based code has low potential for JIT-based optimizations
    • Reflection is mostly used in connecting loosely-coupled components, i.e. in looking up concrete classes and methods, where only interfaces are known: dependeny-injection frameworks, instantiating JDBC implementation classes or XML parsers. These uses can often be done once at system startup, so the small inefficiency don't matter anyway!

提交回复
热议问题