Reputation: 46601
When using objects that have a capacity, what are some guidelines that you can use to ensure the best effeciency when using to collections? It also seems like .NET framework has set some of these capacities low. For example, I think StringBuilder has an intial capacity of 16. Does this mean that after 16 strings are inserted into the StringBuilder, the StringBuilder object is reallocated and doubled in size?
Upvotes: 0
Views: 335
Reputation: 14084
Speaking of StringBuilder
, I'd dare to use the worst-case size. StringBuilder
requires contigous memory block, which is hard to allocate on a highly fragmented heap.
I'd go with an estimation for other collections, though.
Upvotes: 0
Reputation: 754893
There are only two circumstances where I ever explicitly set the capacity of a collection
Interestingly, for #1 it is almost always done when I am copying data returned from a COM interface into a BCL collection class. So I guess you could say I only ever do this in interop scenarios :).
Upvotes: 2
Reputation: 9855
If you know how large a collection or StringBuilder will be up front, it is good practice to pass that as the capacity to the constructor. That way, only one allocation will take place. If you don't know the precise number, even an approximation can be helpful.
Upvotes: 4
Reputation: 1062975
With StringBuilder
, it isn't the number of strings, but the number of characters. In general; if you can predict the length, go ahead and tell it - but since it uses doubling, there isn't a huge overhead in reallocating occasionally if you need to juts use Add
etc.
In most cases, the difference will be trivial and a micro-optimisation. The biggest problem with not telling it the size is that unless the collection has a "trim" method, you might have nearly double the size you really needed (if you are very unlucky).
Upvotes: 2