Fastest method to check if an array is empty
Solution 1
After some intensive testing and analyzing I've found what appears to be the quickest method (so far at least). Making this small change has sped up my program 500-600%.
When there is an item added to the object arrays I also add the index of the second dimension of the cubes to a list IF the index of the second dimension is not already in the list. Any other suggest would be welcome though.
Solution 2
Length
is about 2x faster than GetLength
on my system (calling Length
136M times takes 0.650 seconds, while calling GetLength (0)
takes 1.480 seconds).
I also do not understand why you ReDim your array, you've already created a new one.
I believe this will be the fastest code if cubes is a multi-dimensional array:
If cubes(threadnumber)(i).objects.Length > 0 Then
cubes(threadnumber)(i).objects = New Double() {}
End If
If cubes is not a multi-dimensional array (like List for instance), you should take the cubes(threadnumber)
code out of the loop.
Update
Length
is 6x faster than GetLength
when running in Release mode without the debugger, in which case Length
takes 0.181s and GetLength
1.175s on my system. This is likely because the JIT will inline the call to Length
, but not the call to GetLength
.
This is the test code I used.
Solution 3
if myarray is nothing then...
or
if myarray isnot nothing then...
Solution 4
GetLength is the fastest way I know of to see if an array has elements in it. I don't think you will speed up this piece of code.
However, the code that is calling this 136 million times could probably be optimised.
Looking at your code:
If Not (cubes(threadnumber)(i).objects.GetLength(0) = 0) Then
cubes(threadnumber)(i).objects = New Double() {}
ReDim cubes(threadnumber)(i).objects(-1)
End If
I'm guessing that the reason you are testing if it has elements is so you can redim the array to free up memory. A better way to free up memory might be to clear the cubes object instead and allow the arrays to fall out of scope.
FraserOfSmeg
Updated on December 12, 2020Comments
-
FraserOfSmeg over 3 years
I want to know the fastest way to check if an array is empty in VB.NET. The array is already initialized so I can't use any of the checks that look at that. This is the current code below:
If Not (cubes(threadnumber)(i).objects.GetLength(0) = 0) Then cubes(threadnumber)(i).objects = New Double() {} ReDim cubes(threadnumber)(i).objects(-1) End If
I've done some testing and I know that using
.GetUpperBound
is a little faster, but I'm not sure if this will work because I think.GetUpperBound
returns a 0 if the array length is 1.Any/all methods to speed this up (even fractionally) will be tremendously helpful. This program takes ages to compleate and the first line of the above code is a big portion of the time, it's called 136 million times.
Also if anyone knows how to speed up For...Next loops that'd be great too!
-
sj1900 over 11 yearsHmm, I just read you question again and this might not be the info you are after. Sorry.
-
FraserOfSmeg over 11 yearsThanks for the reply, but yes the main problem with this is that everywhere suggest checking if it's not been initialized as opposed to being empty. Thanks for trying though :)
-
sj1900 over 11 yearsMy understanding is that if it hasn't been initialized it is equivalent to VB.NET "nothing". But to be honest, I'm not sure exactly what you're trying to accomplish.
-
FraserOfSmeg over 11 yearsThat's my understanding as well - not initialized = nothing. Because of the way I've had to code all of the arrays are initialized but only some of them have a length > 0.
-
sj1900 over 11 yearsAhh, I get it now. I'll shutup and let people who know what they are talking about (like Derek) provide useful info.