0

I have a list of maps and in a for-loop I want to add a Map to the list. I heard that using map.clear() has a better performance than creating a new Map but my problem is that List.add() works with the reference of the object and by using Map.clear() the reference is not ceared.

Is there a possibility to force List.add() to use the value or to build an other workaround?

NotMyFaultSir
  • 173
  • 11
  • If you call `clear()` on the map, it should wipe everything. Is this what you want, or do you want a copy of the original map? – Tim Biegeleisen Nov 17 '16 at 13:36
  • 3
    " I heard that using map.clear() has a better performance than creating a new Map" -- This sounds like the kind of micro-optimisation that you should not be worrying about unless there is a proven reason. – bradimus Nov 17 '16 at 13:40
  • Relevant: http://stackoverflow.com/questions/6757868/map-clear-vs-new-map-which-one-will-be-better – px06 Nov 17 '16 at 13:41
  • @TimBiegeleisen I want the reference in my created map-object to be cleared but i want to keep the reference in the list. – NotMyFaultSir Nov 17 '16 at 13:48
  • @bradimus In the given case i have a list with up to maybe 100 objects but i´d like to know it for future usage. Because think i will encounter this problem in the future when working with much more data – NotMyFaultSir Nov 17 '16 at 13:48
  • So, now, instead of doing the obvious: just create a new `Map` each time and add it to the list, you want to do something strange / impossible (somehow add the `Map` by value) so that you can use `Map.clear()` which will be "more efficient". This isn't going to work and it is not going to make your code "more efficient". If there were a way to add the map "by value" (or make a copy of the map), the whole thing would not be any more efficient. You're just moving the copying / making of a new `Map` object to a different place. – Jesper Nov 17 '16 at 13:53
  • For future usage, you should know that you should not worry about this unless you have profiled the code and found a specific bottleneck. When, where, and which optimisations to make will depend on the analysis. Deciding whether to create a new map or reuse on will depend on the data gathered by the profiling among other considerations. – bradimus Nov 17 '16 at 13:53

2 Answers2

3

It is possible to insert "by value" by simply creating a copy of the Map and inserting the copy. The problem with this is that it's not an optimization at all.

Instead of simply creating a new empty Map, you create a copy of a filled Map and clear the original Map. This means: you don't avoid the overhead of creating a new Object, but also introduce the extra work of copying and clearing a filled Map.

And a little note on optimization in general:
It's ~10% of your code that'll be doing 90% of the work (yes, these numbers are made up, but it's the usual way to think about optimization and at least close to reality). Don't over-optimize your code in the first run. Just do what can be done easily and without making the code less readable, run a profiler and look for the bottlenecks of your code and optimize those. This is by far more efficient and easier than optimizing the entire code.

1

If you are constantly reusing the same Map, it makes sense to clear it instead of throwing the old Map away to build a new one.

But here, you do not want to reuse the same Map, because you want to store it in a List. If you want to clear it in order to reuse it, you will have to add a copy of the Map to the List in order to have independant maps. Which one has better performance is a low level optimisation. You should never worry of it unless:

  • you have found that you got unacceptable performances
  • you have found by profiling, that most of the time was spent there

You should just use what is more clear/coherent with you project coding rules. I would create a brand new map on each iteration here, but this is only my opinion. But please do not do low level optimizations in early developpement stages.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252