This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance Loss of Class.forName(String classname)  RSS feed

 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am considering to use static forName method for instantiating a class by choosing from a bunch of classes.
But I am not sure about its performance drawback. Do you guess the performance loss after this operation by considering same operation with new operator ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will typically have to do it very often (for example in a for loop) to notice any performance difference.

I'd advice to simply try it and *measure* whether it's too slow for you.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Class.forName(...) loads a class, if it is not already loaded. Whereas the "new" keyword creates an instance of a class, and first it will load the class, if it is not already loaded. So the two are only partially comparable.
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to find the system bottlenecks. So maybe throw in a few time checks through out the code path. Let's say A->B->C->D->E->elapsed time.
If you never experience any poor timing prior to D, then leave it alone and focus on the trouble spots. I think this is also just a more detailed explaination of IP's reply.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class.forName() also initializes the class after loading it. This means initializers and static blocks are executed. To keep the overhead low, you should minimize the amount of initialization code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!