Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasons to use array.resize instead of redim

I know that "redim" is older than "Array.Resize" but do not understand the reasons that make better to use the latter to the former.

like image 569
genespos Avatar asked Nov 11 '15 07:11

genespos


People also ask

What does array resize do?

Changes the number of elements of a one-dimensional array to the specified new size.

Why we use ReDim in VB net?

The ReDim statement is used to size or resize a dynamic array that has already been formally declared by using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts). Use the ReDim statement repeatedly to change the number of elements and dimensions in an array.

Which method is used to resize an array?

Resize(T[], Int32) Method is used to resize the number of elements present in the array.

What are the use of preserve and ReDim statements in arrays?

Preserve is an optional keyword used to preserve the data in an existing array when you change the size of the last dimension. The ReDim statement is used to resize an array that has already been explicitly declared using the Dim, Private, or Public statement.


1 Answers

As explained in MSDN, ReDim and Array.Resize seems to work equal by allocationg a new Array with fixed size to copy the contents of the old awwat, however there are some internal differences (that I ignore) that makes ReDim faster, and ReDim Preserve a performance killer when compared.

By the result of the next test, which performs a simple iteration to redimensionate an array 100.000 times, you can proof that ReDim is much faster than Array.Resize, however, both are slower than a custom generic solution using Array.Copy method.


Results

ReDim Elapsed Time: 00:00:07.3918219

Array.Resize Elapsed Time: 00:00:28.9773317

Custom Resize Elapsed Time: 00:00:03.2659880

As you see, there is a difference of +20 seconds between ReDim and Array.Resize


Test source-code

Dim myArray As String()
Dim sw As New Stopwatch

myArray = New String() {"Q", "W", "E", "R", "T", "Y"}
sw.Start()
For x As Integer = 1 To 100000 : ReDim myArray(x) : Next
sw.Stop()
Console.WriteLine("ReDim Elapsed Time: " & sw.Elapsed.ToString)

myArray = New String() {"Q", "W", "E", "R", "T", "Y"}
sw.Restart()
For x As Integer = 1 To 100000 : Array.Resize(myArray, x) : Next
sw.Stop()
Console.WriteLine("Array.Resize Elapsed Time: " & sw.Elapsed.ToString)

myArray = New String() {"Q", "W", "E", "R", "T", "Y"}
sw.Restart()
For x As Integer = 1 To 100000 : ResizeArray(myArray, x) : Next
sw.Stop()
Console.WriteLine("Custom Resize Elapsed Time: " & sw.Elapsed.ToString)

Custom Array-Resize solution:

Very useful to add it as a extension method.

' <Extension>
<DebuggerStepThrough>
Public Function ResizeArray(Of T)(ByVal sender As T(),
                                  ByVal newSize As Integer) As T()

    If (newSize <= 0) Then
        Throw New ArgumentOutOfRangeException(paramName:="newSize",
                                              message:="Value bigger than 0 is required.")
    End If

    Dim preserveLength As Integer = Math.Min(sender.Length, newSize)

    If (preserveLength > 0) Then
        Dim newArray As Array = Array.CreateInstance(sender.GetType.GetElementType, newSize)
        Array.Copy(sender, newArray, preserveLength)
        Return DirectCast(newArray, T())

    Else
        Return sender

    End If

End Function

Disclaimer: I'm not the author of the function, it is a custom Vb.Net adapttaion from a C# code that I found time ago in a random StackOverflow answer about Array.Resize performance.

like image 194
ElektroStudios Avatar answered Oct 05 '22 23:10

ElektroStudios