The trap of ReDim Preserve performance in VB any person using the array must be very familiar with using ReDim to change the array size, today, I will explain in detail why it is best not to use such a statement, or at least to be careful about using it.
possible use of ReDim
Before we start
ReDimBefore the sentence, I want to go through the program example (
redimsamples.aspx) to explain
ReDimThe most common way to use it. Use to
ReDimAnd
ReDim Preserve:
<% @Page language= "VB"%><%dim arrstrings (1) as Stringresponse.write (UBound (arrstrings) & "<br>") ReDim arrstrings Response.Write (UBound (arrstrings) & "<br>") ReDim Preserve arrstrings (25) Response.Write (UBound (arrstrings) & "<br>")%>
ReDimAllow for larger and smaller array sizes. Therefore, a new array is produced in each instance, on the grounds that the vb.net array is inherited from. NET Runtime
System.Array, and at birth, the justification will have a fixed size. This is obvious in C #, as shown in the following code
ReDim:
string[] Arrtest = new string[1];//and now we want to change the Size:redim arrtest = new Arrtest];
In itself, this is not a problem, the problem is
Preserve(Today's topic). When using
RedimContains
Preservekeyword, the previous element is retained-as in a new array.
ReDim Preserve to perform the killing on the performance
In principle, we've been destroying the whole thing-
ReDim PreserveThe statement produces a new array-and the elements of the previous array are copied into the array. This happens secretly in the vb.net environment (as is the case with VB in the past). In addition to the loss of performance, it is not observed. To be able to manifest this phenomenon, I set a loop to Redim the array 5,000 times (
redimloop.aspx)。
<% @Page language= "VB" trace= "True"%><%dim arrstrings () as StringDim I as Integertrace.write ("Redim", "Start") For i = 1 to 5000 ReDim Preserve arrstrings (i) arrstrings (i-1) = Inexttrace.write ("ReDim", "End")%>
When I'm tracking it, it's easy to get the performance data:
Of course, every call (and the machine you use) will be different each time, but at least you already know the right performance.
But when the call
ReDim PreserveWhat happens on the surface? To explain this, in C # I follow
ReDimA statement, as if used in vb.net.
ReDimOn the surface, it must be expressed (
redimloopexplicit.aspx)。
<% @Page language= "C #" trace= "True"%><%string[] arrstrings = new String[1];int i; Trace.Write ("Redim", "Start"); for (i=1;i<=5000;i++) {string[] arrhelper = new string; Arrstrings.copyto (arrhelper, 0); Arrhelper[i-1] = i.tostring (); Arrstrings = Arrhelper;} Trace.Write ("Redim", "End");%>
From this we can see that-first a new array size is produced, and the previous array will be copied to the new array. I assign a repeat variable to a String in C #, and in order to be complete, I exchange the array variable.
If you think that vb.net seems a bit different, then it's wrong-performance is exactly the same as ReDim Preserve (My code might be a little bit better because it doesn't need to be factored into the list). Performance damage can only be done with better techniques- ArrayList class. best way to solve-arraylists
The best way to resolve an array is to use ArrayList class in the System.Collections namespace. ArrayList can be grown or scaled small and easy to use ( arraylistloop.aspx ): <% @Page language= "VB" trace= "True"%><% @Import Namespace= "System.Collections"%><%dim arrlist as New ArrayList (MB) Dim I as Longtrace.write ("ArrayList", "Start" For i = 1 to 50000 arrlist.add (i) nexttrace.write ("ArrayList", "End")%>
If you pay attention, you will notice that the loop is not running to 5000 is 50000, the reason is: use to 5000, and can not test performance. But using 50000 is very obvious:
is performed from 0.02 to 0.6-and is a large, sequential loop. I'm trying to get ReDim Preserve to repeat the number of times, but I reset the Server's time when I performed it. concludes
from the topic of this article we learned that in the vb.net environment ReDim Preserve best to avoid using. When you need to use an active array, the advice is to use [I]arraylist Because it looks more like a formal array.