我讀到初始化具有初始容量的字典可能會導致更好的性能,如果可以估計大小。具有初始容量的字典
Dim MyDataTable As New DataTable
'Fill MyDataTable from Database
Dim CapacityEstimate As integer = MyDataTable.Rows.Count
Dim MyDictionary As New Dictionary(Of String, MyObjectType)(CapacityEstimate)
'Fill the Dictionary independent of data table
的CapacityEstimate變量只是鍵/值對的數量的估計值(通常爲2500〜7000的範圍內),該字典應該保持。因此,如果我估計它是4000,最後是4010個對象(我可能會超出或低於,不確定),字典是否會使用大量內存來調整其中已有的許多鍵/值對。這是一個好的解決方案,還是我最好不用初始容量進行初始化。謝謝。
我在哪裏暗示實際尺寸和容量是相同的 – swiftgp 2012-08-10 19:21:08
@ user1556110 - 我的答案並非只針對您自己 - 而是針對讀取問題的每個人。 – Oded 2012-08-10 19:22:26