הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, December 2, 2011 9:25 PM
I've got a 2-dimensional array that I need to re-initialize (clear) and re-use many times.
Does this clear all the contents of both dimensions?
Dim S_Array(99, 40) As String Array.Clear(S_Array, 0, S_Array.Length)
All replies (20)
Friday, December 2, 2011 10:11 PM ✅Answered | 2 votes
I've got a 2-dimensional array that I need to re-initialize (clear) and re-use many times.
Does this clear all the contents of both dimensions?
Dim S_Array(99, 40) As String
Array.Clear(S_Array, 0, S_Array.Length)
Yes it will set all items in the array to Nothing, same as Redim, just a lot faster.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Friday, December 2, 2011 9:44 PM
I'd bet that it didn't, but then I've never really used System.Array.Clear myself. If you call Clear then it has to iterate through the whole array and set it to NULL or NOTHING or 0 or whatever, and then when you go to re-use it you iterate through the whole array again to set it to the new values.
What I'd do is just ReDim the array every time - same effect as System.Array.Clear without the iteration. Either that or (if the size of the array never changes when you re-use it) just change the values in it without clearing it first.
It never hurts to try. In a worst case scenario, you'll learn from it.
Thanks, I'll try Redim instead. I'm not using preserve, so, I guess that should do the trick...
Friday, December 2, 2011 9:48 PM | 1 vote
Hi,
Yes you are correct. :-D
Use this line.>>
Array.Clear(S_Array, 0, S_Array.Length)
For proof this works try this code.
Add one Button and one RichTextBox to a Form then run this please.
Option Strict On
Option Explicit On
Option Infer On
Public Class Form1
Dim S_Array(99, 40) As String
Dim myRand As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 0 To 99
For y As Integer = 0 To 40
S_Array(x, y) = myRand.Next(0, 100).ToString
Next
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sb As New System.Text.StringBuilder
For x As Integer = 0 To 99
For y As Integer = 0 To 40
sb.Append(S_Array(x, y) & ",")
Next
Next
RichTextBox1.Text = sb.ToString
MessageBox.Show("Click OK when you are ready please.")
Array.Clear(S_Array, 0, S_Array.Length)
sb.Remove(0, sb.Length)
For x As Integer = 0 To 99
For y As Integer = 0 To 40
sb.Append(S_Array(x, y))
Next
Next
RichTextBox1.Text = sb.ToString
MessageBox.Show("StringBuilder length = " & sb.ToString.Length.ToString)
End Sub
End Class
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Friday, December 2, 2011 9:52 PM | 1 vote
Hi,
I would not use ReDim
Microsoft did not create the Array.Clear method for no reason whatsoever.
ReDim creates a new array in memory, whether you use Preserve or not.
Please read the remarks under ReDim here.>>
http://msdn.microsoft.com/en-us/library/w8k3cys2(VS.71).aspx
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Friday, December 2, 2011 10:07 PM | 1 vote
I agree with John. This test shows the speed difference between the legacy method and the .Net version.
Public Class Form1
Const sizeA As Integer = 99
Const tries As Integer = 1000
Dim testA(sizeA, sizeA) As String
Dim stpw As New Stopwatch
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
initA()
stpw.Restart()
For x As Integer = 1 To tries
Array.Clear(testA, 0, testA.Length)
Next
stpw.Stop()
Debug.WriteLine(stpw.Elapsed)
initA()
stpw.Restart()
For x As Integer = 1 To tries
ReDim testA(sizeA, sizeA)
Next
stpw.Stop()
Debug.WriteLine(stpw.Elapsed)
End Sub
Private Sub initA()
For x As Integer = 0 To testA.GetUpperBound(0)
For y As Integer = 0 To testA.GetUpperBound(1)
testA(x, y) = x.ToString
Next
Next
End Sub
End Class
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Friday, December 2, 2011 10:16 PM
I've got a 2-dimensional array that I need to re-initialize (clear) and re-use many times.
Does this clear all the contents of both dimensions?
Dim S_Array(99, 40) As String
Array.Clear(S_Array, 0, S_Array.Length)
Yes it will set all items in the array to Nothing, same as Redim, just a lot faster.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Great. I'll change it back then. I'm all for speed! Thanks a lot guy's :)
- Red
Friday, December 2, 2011 10:18 PM | 1 vote
I've got a 2-dimensional array that I need to re-initialize (clear) and re-use many times.
Does this clear all the contents of both dimensions?
Dim S_Array(99, 40) As String
Array.Clear(S_Array, 0, S_Array.Length)
Yes it will set all items in the array to Nothing, same as Redim, just a lot faster.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Great. I'll change it back then. I'm all for speed! Thanks a lot guy's :)
- Red
You should credit John with the answer so that future readers see hist post first.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Friday, December 2, 2011 10:23 PM
Great. I'll change it back then. I'm all for speed! Thanks a lot guy's :)
- Red
Hi again,
Then please mark the replies as answer which better answer your question as well.
The suggestion from Andrew B. Painter is okay but not as fast so you can leave those marked as answer if you wish.
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Friday, December 2, 2011 10:28 PM
Okay, you guy's, now don't let's be fighting over who all gets credit! :)
Friday, December 2, 2011 10:50 PM
You should credit John with the answer so that future readers see hist post first.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Hi dbasnett,
Thank you for your suggestion to the OP
:-D
I will leave it to the OP which post(s) to leave marked as answer.
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Friday, December 2, 2011 11:32 PM
For Benchmark testing, you need to clear main-memory on your machine before each test, otherwise you won't get accurate results...
Friday, December 2, 2011 11:39 PM
Changing the data type(string to int) and the dimensions(2 to 1) I found that it depended on the size of the array. When I used 99,999 as the size Clear was faster. When the array was 999,999 redim was faster, in both test methods. edit: Where did Andrews post go?
Public Class Form1
Const sizeA As Integer = 99999 '999999
Const tries As Integer = 1000
Dim testA(sizeA) As Integer
Dim stpw As New Stopwatch
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
Array.Clear(testA, 0, testA.Length)
Debug.WriteLine(testA.Length.ToString)
initA()
stpw.Restart()
For x As Integer = 1 To tries
Array.Clear(testA, 0, testA.Length)
Next
stpw.Stop()
Debug.WriteLine(stpw.Elapsed)
Array.Clear(testA, 0, testA.Length)
initA()
stpw.Restart()
For x As Integer = 1 To tries
ReDim testA(sizeA)
Next
stpw.Stop()
Debug.WriteLine(stpw.Elapsed)
End Sub
Private Sub initA()
For x As Integer = 0 To testA.GetUpperBound(0)
testA(x) = x
Next
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim HugeCount(sizeA) As System.Int32
Dim Timer As New System.Diagnostics.Stopwatch
'For a proper test, We'll iterate through the whole
'array and set each indice's value to the index it
'occupies.
For hc_idx As System.Int32 = 0 To (HugeCount.Length - 1)
HugeCount(hc_idx) = hc_idx
Next hc_idx
'Now we'll test the time to clear.
Timer.Reset()
Timer.Start()
System.Array.Clear(HugeCount, 0, HugeCount.Length)
Timer.Stop()
Dim ClearTime As System.Int64 = Timer.ElapsedTicks
'We don't actually have to reset every value to look into ReDim
For hc_idx As System.Int32 = 0 To (HugeCount.Length - 1)
HugeCount(hc_idx) = hc_idx
Next hc_idx
Timer.Reset()
Timer.Start()
ReDim HugeCount(sizeA)
Timer.Stop()
Dim RedimTime As System.Int64 = Timer.ElapsedTicks
Debug.WriteLine("Time to clear: " & ClearTime.ToString & System.Environment.NewLine & "Time to ReDim: " & RedimTime)
'Turns out ReDim is more than twice as fast as Array.Clear.
End Sub
End Class
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Friday, December 2, 2011 11:50 PM
Hi Andrew,
Very good point I feel. :-D
Sorry, I did not do a StopWatch test like the one you have just shown on the code from dbasnett.
However if you do not want VB.Net to create a duplicate array and
dispose of the old one then you could use the "sometimes" faster Array.Clear method.
<edit> I have just tried the code from dbasnett on a two dimensional array of STRING
ReDim on that type of array is 8 times slower for the number of tries.
So I guess it depends on the TYPE of the array elements or / and the number of array dimensions.
:-S
</edit>
To dbasnett,
I am just going to check if the .Restart method is new to FrameWork 4.0 or not.
The Public Shared method
Stopwatch.StartNew()
does not appear to be working for me. :-(
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Friday, December 2, 2011 11:52 PM
@JAO/red - very curious. A quick search of the web shed no light. Where did Andrews post go?
.Restart is new in 4.0 (yea!).
I see Andrew has found another of my post abusive. You have to stop Andrew.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Saturday, December 3, 2011 12:11 AM
@JAO/red - very curious. A quick search of the web shed no light. Where did Andrews post go?
.Restart is new in 4.0 (yea!).
I see Andrew has found another of my post abusive. You have to stop Andrew.
Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Hi dbasnett,
I've just looked up .Restart
:-D
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.restart.aspx
I guess that Andrew no longer wishes to participate in this thread and has deleted His posts.
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Saturday, December 3, 2011 1:11 PM
This morning I took another look at this using a one dimensional array. Sometimes Array.Clear is faster and sometimes ReDim is faster. For clearing I think I will stay with .Clear. Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Saturday, December 3, 2011 1:48 PM
This morning I took another look at this using a one dimensional array. Sometimes Array.Clear is faster and sometimes ReDim is faster. For clearing I think I will stay with .Clear. Serial Port Random Check Internet Connection Microsoft® Community Contributor 2011
Hi dbasnett,
I think that I will as well. :)
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Saturday, December 3, 2011 2:55 PM
Has anyone played around with "Erase"? Speed? Effectiveness?
Saturday, December 3, 2011 4:33 PM
Has anyone played around with "Erase"? Speed? Effectiveness?
Hi again,
According to Intellisense ERASE deallocates any memory used as well. You might not want that to happen?
'Try this code snippet within the event for a Button Click or similar.
'It will throw an EXCEPTION error on the MessageBox line.>>
Dim numbers(100) As Integer
For index As Integer = 0 To 100
numbers(index) = index * 2
Next
Erase numbers
MessageBox.Show(numbers(77).ToString)
That is because the entire numbers array is ERASEd from memory.
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Saturday, December 3, 2011 4:42 PM
Yes. Looks like you have to Redim after the erase in order to use the array again, which would probably be slower than just a Redim or .Clear...