• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects in loops - what is more efficient?

 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a small task to loop over an array of xml elements and assign their attribute values to a simple class containing two strings and a boolean. What is the most efficient way to create new objects in this loop and add them to an array? I am using method 3 below, but is this the most efficient and why? Thanks.

Method 1:


or

Method 2:


or

Method 3:


Thanks
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between the three? I can see that in No 3 you are creating the new element as an anonymous object rather than giving it a name. I am not sure I can see a difference between 1 and 2. But you are not adding anything to an array; you have an object which you are adding the mappings to. Is that a List?
I suggest you compile all those three methods and print their bytecode with javap -c. Please show us all three outputs. Can you actually see a difference?

You should not worry about such tiny bits of optimisation but I suspect you will find no difference in speed between the three.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic