• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cost of Using Reflection

 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a bean which has 40-50 attributes and i have to update it through a hashmap,(the key of the map and the name of the attributes are same) so i am thinking to use reflection for this, and assign all the variables in one for loop rather than calling 40 setters (and 40 hashmap.get())
My friend has told me that using reflection has lots of performance overhead, how true it is.
thanx
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implement it the clean way and only switch to a more optimal and harder to maintain method if the performance proves unacceptable. Reflection does come at a cost, but not an absurdly high cost.
[ July 22, 2003: Message edited by: David Weitzman ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. It was pretty bad in 1.2, but is much better these days.
Calling a method via reflection in 1.2 took about 200x longer than direct method invocation. Think it's more like 10x now, but I haven't really checked.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on how often the bean changes. If it's going to stay the same, use the direct method of doing it. This doesn't sound like you need reflection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!