• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which would be better performance, better memory-wise?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to have thousands of collections (or arrays) of objects, but each object will only have anywhere from 1 to 5 or so objects. So each is small, but there are a LOT of them.

Is it better to have thousands of small arrays (in which case I have to create each array and populate it) where they might change in size and contents about 20% of the time.

Or is it better to have thousands of Collections/Lists with just a few objects (in which case I have to create each collection and populate it) where they'll also change in size/contents about 20% of the time.

Which is better in memory consumption?
Which is better in performance?
Is there an even better solution?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an even better solution?


What makes the most sense to the structure and clarity of the program?
 
Pratik Lohia
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best thing to do is to club all similar objects in a list... and with a unique idendifier of the similar objects as the key, have a hashtable of all the lists.
This is excellent, to manage and for better performance.

Hope this helps
 
Virag Saksena
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three factors which will affect your performance
1. Is access to your objects faster with one approach vs other
Are there cases where you'd have to do linear scans to get to the desired object, and if so, how long will the scan be ?

2. How good is your initial approximation of the final size ?
Most of the collections grow by creating a new collection double the prev collection and discarding the prev collection. So if you specify an initial size of the large collection, you might be close.

3. How long do these objects live. If you have lots of small objects, and they come and go frequently, they will live and die before reaching an old generation and garbage collection would not be a problem.

Considering all of these, it might be better as Pratik suggested to have a large hash table/hash map to access your lots of small objects. Be sure to use an appropriate large initial size on the Hash.

Regards,

Virag
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!