在 vb.net 中检查数组是否为空的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13506257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest Method to check is an array is empty in vb.net
提问by FraserOfSmeg
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:
我想知道在 vb.net 中检查数组是否为空的最快方法。数组已经初始化,所以我不能使用任何查看它的检查。这是下面的当前代码:
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.
我做了一些测试,我知道使用 '.getupperbound' 会快一点,但我不确定这是否有效,因为我认为如果数组长度为 1,'.getupperbound' 会返回 0。
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.
任何/所有加速(甚至部分)的方法都将非常有帮助。这个程序需要很长时间才能完成,上面代码的第一行占了很大一部分时间,它被调用了 1.36 亿次。
Also if anyone knows how to speed up "for next" loops that'd be great too!
此外,如果有人知道如何加速“for next”循环,那也太棒了!
Thanks :)
谢谢 :)
Fraser
弗雷泽
采纳答案by FraserOfSmeg
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%.
经过一些深入的测试和分析,我发现了似乎是最快的方法(至少到目前为止)。做出这个小小的改变让我的程序加速了 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.
当有一个项目添加到对象数组时,我还将多维数据集的第二个维度的索引添加到列表中,如果第二个维度的索引不在列表中。不过,任何其他建议都会受到欢迎。
回答by Rolf Bjarne Kvinge
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).
Length
大约比GetLength
我的系统快 2Length
倍(调用136M 次需要 0.650 秒,而调用GetLength (0)
需要 1.480 秒)。
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.
如果cubes 不是多维数组(例如List),您应该将cubes(threadnumber)
代码移出循环。
Update
更新
Length
is 6x fasterthan 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
.
Length
是6倍快比GetLength
没有调试器,在这种情况下,在发行模式下运行时Length
需要0.181s和GetLength
我的系统上1.175s。这可能是因为 JIT 将内联对 的调用Length
,而不是对 的调用GetLength
。
Thisis the test code I used.
这是我使用的测试代码。
回答by sj1900
if myarray is nothing then...
or
或者
if myarray isnot nothing then...
回答by Derek Tomes
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.
GetLength 是我所知道的查看数组中是否包含元素的最快方法。我不认为你会加速这段代码。
However, the code that is calling this 136 million times could probably be optimised.
但是,可能会优化调用此 1.36 亿次的代码。
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.
我猜你测试它是否有元素的原因是你可以重新调整数组以释放内存。释放内存的更好方法可能是清除多维数据集对象并允许数组超出范围。