vb.net 将项目添加到数组的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18097756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest way to add an Item to an Array
提问by jor
What is the fastest way to add a new item to an existing array?
将新项目添加到现有数组的最快方法是什么?
Dim arr As Integer() = {1, 2, 3}
Dim newItem As Integer = 4
(I already know that when working with dynamic list of items you should rather use a List
, ArrayList
or similar IEnumerables
. But what to do if you're stuck to legacy code that uses arrays?)
(我已经知道,随着项目的动态列表工作时,你应该宁可使用List
,ArrayList
或类似的IEnumerables
,但是做什么,如果你坚持到遗留代码,使用数组?)
What I've tried so far:
到目前为止我尝试过的:
' A) converting to List, add item and convert back
Dim list As List(Of Integer)(arr)
list.Add(newItem)
arr = list.ToArray()
' --> duration for adding 100.000 items: 33270 msec
' B) redim array and add item
ReDim Preserve arr(arr.Length)
arr(arr.Length - 1) = newItem
' --> duration for adding 100.000 items: 9237 msec
' C) using Array.Resize
Array.Resize(arr, arr.Length + 1)
arr(arr.Length - 1) = newItem
' --> duration for adding 100.000 items: 1 msec
' --> duration for adding 100.000.000 items: 1168 msec
A) seems very slow since every time an item is added two conversions of the whole array are done. B) seems faster but still the array is copied once during the ReDim Preserve
. C) seems to be the fastest at this point. Is there anything better?
A) 看起来很慢,因为每次添加一个项目时,都会对整个数组进行两次转换。B) 看起来更快,但在ReDim Preserve
. C) 在这一点上似乎是最快的。有更好的吗?
回答by jor
Case C) is the fastest. Having this as an extension:
情况 C) 是最快的。将此作为扩展:
Public Module MyExtensions
<Extension()> _
Public Sub Add(Of T)(ByRef arr As T(), item As T)
Array.Resize(arr, arr.Length + 1)
arr(arr.Length - 1) = item
End Sub
End Module
Usage:
用法:
Dim arr As Integer() = {1, 2, 3}
Dim newItem As Integer = 4
arr.Add(newItem)
' --> duration for adding 100.000 items: 1 msec
' --> duration for adding 100.000.000 items: 1168 msec
回答by Massimiliano Peluso
回答by Eric F.
For those who didn't know what next, just add new module file and put @jor code (with my little hacked, supporting 'nothing' array) below.
对于那些不知道接下来要做什么的人,只需添加新的模块文件并在下面放置@jor 代码(我的小黑,支持“无”数组)。
Module ArrayExtension
<Extension()> _
Public Sub Add(Of T)(ByRef arr As T(), item As T)
If arr IsNot Nothing Then
Array.Resize(arr, arr.Length + 1)
arr(arr.Length - 1) = item
Else
ReDim arr(0)
arr(0) = item
End If
End Sub
End Module
回答by user6015865
Not very clean but it works :)
不是很干净,但它有效:)
Dim arr As Integer() = {1, 2, 3}
Dim newItem As Integer = 4
arr = arr.Concat({newItem}).ToArray
回答by the_lotus
It depends on how often you insert or read. You can increase the array by more than one if needed.
这取决于您插入或阅读的频率。如果需要,您可以将数组增加一个以上。
numberOfItems = ??
' ...
If numberOfItems+1 >= arr.Length Then
Array.Resize(arr, arr.Length + 10)
End If
arr(numberOfItems) = newItem
numberOfItems += 1
Also for A, you only need to get the array if needed.
同样对于 A,您只需要在需要时获取数组。
Dim list As List(Of Integer)(arr) ' Do this only once, keep a reference to the list
' If you create a new List everything you add an item then this will never be fast
'...
list.Add(newItem)
arrayWasModified = True
' ...
Function GetArray()
If arrayWasModified Then
arr = list.ToArray()
End If
Return Arr
End Function
If you have the time, I suggest you convert it all to List and remove arrays.
如果您有时间,我建议您将其全部转换为 List 并删除数组。
* My code might not compile
* 我的代码可能无法编译