• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java reflection performance

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java reflection performance

Use java reflection to create an object , like Class.forName("fff").newInstance(), compared to regularly create an instance, how much it will downgrade the performance ? do we have a statistic chart ?

Thanks.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't you just write a little piece of code that creates a million regular instances and then a milltion instances using reflections ?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Chen wrote:how much it will downgrade the performance ? do we have a statistic chart ?

by simple google, I find this. http://java.sun.com/docs/books/tutorial/reflect/index.html
from the same URL,
Performance Overhead Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
 
Paul Clapham
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Khojaye wrote:
Edward Chen wrote:how much it will downgrade the performance ? do we have a statistic chart ?

by simple google, I find this. http://java.sun.com/docs/books/tutorial/reflect/index.html
from the same URL,
Performance Overhead Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Yes, that article does exist and can be found on the web. But it totally fails to answer Edward Chen's question about how much. Not to mention that the advice posted is unhelpful; if you have the choice to use either reflection or non-reflection, you should use the non-reflection version even if your application is performance-sensitive. Not only is it faster, but the code is easier to understand. And if you don't have the choice, you can't avoid using reflection, no matter whether the application is performance-sensitive or not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic