Redim boolean Array vs lists and sets

In case you want to reset an array of booleans, which is faster, reassigning an array or listing and resetting values?

I did some tests and they seem to suggest that redim be much faster, but I'm not sure if this is not the result of how Im runs the tests.

My tests show that redim is almost twice as fast.

So can anyone take care of which is faster and why? Also would you expect the same result in different languages?

Enum test:

Dim booleanArray(200) As Boolean Dim startTime As Date = Date.Now For i As Integer = 0 To 9999999 For l As Integer = 0 To 200 booleanArray(l) = True Next Next Dim endTime As Date = Date.Now Dim timeTaken As TimeSpan = endTime - startTime 

Redial Test:

 Dim booleanArray(200) As Boolean Dim startTime As Date = Date.Now For i As Integer = 0 To 9999999 ReDim booleanArray(200) Next Dim endTime As Date = Date.Now Dim timeTaken As TimeSpan = endTime - startTime 
+4
source share
4 answers

This shows that allocating a new array is fast. This is to be expected when a lot of available memory is available - basically it increases the pointer and a small part of the household.

However, note that this will create a new array with all elements as False, not True.

A more suitable test might be to call Array.Clear in an existing array, in the first case, which will destroy the content quite quickly.

Please note that your second form will create a lot more garbage - in this case it will always remain in gen0 and will be easily collected, but in real applications with more realistic memory usage you may end up with garbage collection performance problems creating new arrays instead of cleaning old ones.

Here is a quick C # test that tests three strategies:

 using System; using System.Diagnostics; public class Test { const int Iterations = 100000000; static void Main() { TestStrategy(Clear); TestStrategy(ManualWipe); TestStrategy(CreateNew); } static void TestStrategy(Func<bool[], bool[]> strategy) { bool[] array = new bool[200]; GC.Collect(); GC.WaitForPendingFinalizers(); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < Iterations; i++) { array = strategy(array); } sw.Stop(); Console.WriteLine("{0}: {1}ms", strategy.Method.Name, (long) sw.ElapsedMilliseconds); } static bool[] Clear(bool[] original) { Array.Clear(original, 0, original.Length); return original; } static bool[] ManualWipe(bool[] original) { for (int i = 0; i < original.Length; i++) { original[i] = false; } return original; } static bool[] CreateNew(bool[] original) { return new bool[original.Length]; } } 

Results:

 Clear: 4910ms ManualWipe: 19185ms CreateNew: 2802ms 

However, that still just uses 0 generation - I would personally expect Clear be better for overall application performance. Note that they behave differently if any other code has references to the original array - the β€œcreate new” ( ReDim ) strategy does not modify the existing array at all.

+2
source

Tests are not comparable. The first test sets each element to true, while Redim does not.

Redim helps you to increase / decrease borders and clear the content (and set it by default). for example, Redim will help set the contents of the boolean array to false.

Do you Redim set all elements to true ?

 Dim booleanArray(200) As Boolean For l As Integer = 0 To 200 booleanArray(l) = True Next Redim booleanArray(200) 

This will reset the contents of each booleanArray to false .

If you want to save content and increase size - Redim Preserve booleanArray(300) (instead of Redim booleanArray(200) ). This will keep the first 200 elements to true , and the new 100 elements will have a default value ( false ).

+1
source

I tested this in C # 3.5

Time spent on the test Enum: 00: 00: 06.2656 Time spent on the second test: 00: 00: 00.0625000

As you can see, Redim is much faster since you are not setting any values ​​for it.

+1
source

I would expect ReDim be faster since you are not assigning a value to each element of the array.

The micro test will look normal.

0
source

Source: https://habr.com/ru/post/1316621/


All Articles