I’m working on a .net app and I’m using an object that is composed of 5 Lists and 1 Hashtable. This object is used within a loop that iterates at least 500 times to run some analysis. On each loop this object should start empty, so I was wondering if it’s more efficient to call clear on all Lists and Hashtable or should I just re-initialize the object?
I know I could write code to benchmark this, but I’m wondering if someone has already been down this path?
Thanks.
While I agree with the other answer that this is a micro optimization, in the interests of answering the question, I have found that new List is slightly faster than Clear when using a List. Here’s my benchmark code:
Over five runs, new List averaged 340.8 ms, and Clear averaged 354.8 ms.
However, this results are so close that it’s clear that: