Framework 2.0数组redim表现不尽如人意 [英] Framework 2.0 array redim unsatisfactory performance

查看:56
本文介绍了Framework 2.0数组redim表现不尽如人意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


当我遇到这个问题时,我只是在测试VB.Net上的Framework.Net 2.0性能。

下面附带的这些简单的代码在VB 6.0中比在.Net环境下,在VS 2005 Beta 2下执行了数百次,如果不是千次,那么
有没有人

不知道这是否会在最终版本中得到解决?


谢谢,

Tomasz


Public Sub Main()

Dim foos()As Long


Dim n As Long

n = 100000

Dim i As Long

For i = 1 to n

ReDim Preserve foos(i)

foos(i)= i

下一个我

Debug.Print(foos(1000))

End Sub

解决方案

Tom,

|有没有人

|不知道这是否会在最终版本中得到解决?

不,它本身不会被解决。每次你使用ReDim Preserve

时,都会定义一个全新的阵列。每个元素都从现有的

数组复制到新数组中!


注意:VB长。 NET是一个640Bit的值,我怀疑你的意思是整数,现在是32位(与VB6的Long为32位相比)。如果您正在使用

Long认为您有32位值,那么您实际上是在复制&使用两次

尽可能多的内存!


我建议使用而不是ReDim Preserve,你考虑使用

System.Collections.Generic.List(Of T)代替,因为它会动态地根据需要为b
分配内存(它以16

元素的缓冲区开头,每次需要更多空间时再加倍。列表(

T).Count反映当前集合中元素的数量,而

列表(Of T) .Capacity反映了

集合中的元素数量(换句话说就是缓冲区的大小)。当Count达到

容量缓冲区增加了.. 。

有关List(Of T)的详细信息,请参阅:
http://msdn2.microsoft.com/library/6...us,vs.80).aspx


类似的东西(来自内存):


Dim foos作为列表(整数)


Dim n As Integer = 100000


索引As Integer = 1 To n

foos.Add(index)

Next


注意:在.NET数组&集合使用基数0索引而不是基数1

索引。这意味着foos(0)= 1.


注意:缓冲区大小加倍是.NET

1.0& 1.1使用,.NET 2.0可能会也可能不会改变算法来增加缓冲区的大小...


希望这会有所帮助

Jay


" Tom Jastrzebski" < to*@tom.com>在消息中写道

新闻:EO *************** @ newssvr33.news.prodigy.com。 ..

|您好,

|

|当我遇到

|时,我只是在Framework.Net 2.0上测试VB.Net这个问题。

|下面附带的这个简单的代码执行了数百次,如果不是千次,那么
|在VB 6.0中比在.Net环境下更快,在VS 2005 Beta 2下。

任何人

|不知道这是否会在最终版本中得到解决?

|

|谢谢,

| Tomasz

|

| Public Sub Main()

| Dim foos()As Long

|

| Dim n As Long

| n = 100000

| Dim i As Long

|对于i = 1到n

| ReDim Preserve foos(i)

| foos(i)= i

|接下来我

| Debug.Print(foos(1000))

|结束子

|

|


Jay,


是否正在使用Integer或Long类型当然会有所不同。

但是,我的测试显示这种差异并不那么显着,并且

仍旧旧VB 6.0执行方式更好。


我测试Array的原因是因为我需要一个带有

的动态数据结构作为尽可能小的内存开销。


我的测试显示基于通用的List<>每个条目使用至少5个额外字节

。毫不奇怪,很可能它被实现为单链接的

列表。在大多数应用程序中它不是一个问题,但在我的情况下它是。


由于动态数组在C#中不可用,我只是想知道

如何有效这个VB结构在开始考虑使用C ++替代

方法之前。


谢谢,


Tomasz


Tom,


你读过杰伊写的吗?

每次执行ReDim Preserve时都会定义一个全新的数组,并且每个元素都会从现有数组复制到新数组中!


我测试的原因数组是因为我需要一个动态数据结构,并且内存开销尽可能小。



你在内存中创建了100000个数组,这些数组一直在增长。在

中,因为它是在你的情况下在一个例程中,可能与

没有多少来自GC的交互,因为这个过程正在进行(Garbage

收藏家)因此使用的内存会增长和增长


通常情况下这可能会在VB.Net中完成,因为


\\\

Public Sub Main()

dim Foos as new arraylist''或任何其他Ilist

for n as integer = i to 100000

Foos.add(I)

next

Debut.Print(foos(i).ToString)

结束子

///


然而有很多选择,这就是杰伊写的(我假设)

寻找System.Collections.Generic.List(Of T)帮助您快速查找

实例此链接

http:// msdn .microsoft.com /天秤座ry / de ... ctiontypes.asp


我希望这会有所帮助,


Cor


Hello,

I was just testing VB.Net on Framework.Net 2.0 performance when I run into
the this problem.
This trivial code attached below executed hundreds, if not thousand times
faster in VB 6.0 than in .Net environment, under VS 2005 Beta 2. Does anyone
have any idea whether this will be addressed in the final release?

Thanks,
Tomasz

Public Sub Main()
Dim foos() As Long

Dim n As Long
n = 100000
Dim i As Long
For i = 1 To n
ReDim Preserve foos(i)
foos(i) = i
Next i
Debug.Print(foos(1000))
End Sub

解决方案

Tom,
| Does anyone
| have any idea whether this will be addressed in the final release?
No, it will not be addressed per se. As each time you do the ReDim Preserve
a brand new array is defined & every element is copied from the existing
array into the new array!

NOTE: Long in VB. NET is a 640Bit value, I suspect you mean Integer which is
now 32-bit (as compared to VB6''s Long which was 32-bit). If you are using
Long thinking you have 32-bit values you are actually copying & using twice
as much memory as you need to!

I would recommend instead of ReDim Preserve, that you consider using
System.Collections.Generic.List(Of T) instead, as it will dynamically
allocate memory on a as needed bases (it starts with a buffer of 16
elements, then doubles it each time it needs more space. The List(Of
T).Count reflects the number of elements currently in the collection, while
List(Of T).Capacity reflects the number of elements that can be in the
collection (in other words the size of the buffer). When Count reaches
Capacity the buffer is increased...
For details on List(Of T) see:
http://msdn2.microsoft.com/library/6...us,vs.80).aspx

Something like (from memory):

Dim foos As List(Of Integer)

Dim n As Integer = 100000

For index As Integer = 1 To n
foos.Add(index)
Next

NOTE: In .NET arrays & collections use base 0 indexing instead of base 1
indexing. Which means that foos(0) = 1.

NOTE: The "double the size of the buffer" is the general algorithm that .NET
1.0 & 1.1 use, .NET 2.0 may or may not change the algorithm to increase the
size of the buffer...

Hope this helps
Jay

"Tom Jastrzebski" <to*@tom.com> wrote in message
news:EO***************@newssvr33.news.prodigy.com. ..
| Hello,
|
| I was just testing VB.Net on Framework.Net 2.0 performance when I run into
| the this problem.
| This trivial code attached below executed hundreds, if not thousand times
| faster in VB 6.0 than in .Net environment, under VS 2005 Beta 2. Does
anyone
| have any idea whether this will be addressed in the final release?
|
| Thanks,
| Tomasz
|
| Public Sub Main()
| Dim foos() As Long
|
| Dim n As Long
| n = 100000
| Dim i As Long
| For i = 1 To n
| ReDim Preserve foos(i)
| foos(i) = i
| Next i
| Debug.Print(foos(1000))
| End Sub
|
|


Jay,

Whether Integer or Long type is being used of course makes a difference.
However, my tests show that this difference is not that significant, and
still old good VB 6.0 performs way better.

The reason why I test Array is because I need a dynamic data structure with
as small memory overhead as possible.

My tests show that generic based List<> uses at least 5 additional bytes for
each entry. Not a surprise, most likely it is implemented as single-linked
list. In most applications it is not a concern, but in my cases it is.

Since dynamic array is not available in C# I was just wondering how
efficient this VB structure was before starting thinking about alternative
approaches using C++.

Thanks,

Tomasz


Tom,

Did you read what Jay wrote?

As each time you do the ReDim Preserve a brand new array is defined &
every element is copied from the existing array into the new array!

The reason why I test Array is because I need a dynamic data structure
with as small memory overhead as possible.


You are creating 100000 arrays in memory which are growing all the time. In
addition, because that it is in your case inside a routine, probably with
not much interaction from the GC because the process is going on (Garbage
Collector) and therefore the used memory will grow and grow

Normally this would (could) be done in VB.Net as

\\\
Public Sub Main()
dim Foos as new arraylist ''or any other Ilist
for n as integer = i to 100000
Foos.add(I)
next
Debut.Print(foos(i).ToString)
End sub
///

However with a lot of alternatives, that is why Jay wrote (as I assume) to
look for System.Collections.Generic.List(Of T) to help you quick to find by
instance this link

http://msdn.microsoft.com/library/de...ctiontypes.asp

I hope this helps,

Cor


这篇关于Framework 2.0数组redim表现不尽如人意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆