473,836 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Framework 2.0 array redim unsatisfactory performance

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(foo s(1000))
End Sub
Nov 21 '05 #1
19 3160
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.Collecti ons.Generic.Lis t(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******** *******@newssvr 33.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(foo s(1000))
| End Sub
|
|
Nov 21 '05 #2
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
Nov 21 '05 #3
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(foo s(i).ToString)
End sub
///

However with a lot of alternatives, that is why Jay wrote (as I assume) to
look for System.Collecti ons.Generic.Lis t(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

Nov 21 '05 #4
Tom,
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.


No, List(Of T) is backed by a simple array, it's not a linked list.
There's no per-item overhead, not sure how your tests could indicate
that.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #5
Doh!
| NOTE: Long in VB. NET is a 640Bit value, I suspect you mean Integer which
is
That should be 64-bit value.

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:us******** ******@tk2msftn gp13.phx.gbl...
| 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.Collecti ons.Generic.Lis t(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******** *******@newssvr 33.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(foo s(1000))
|| End Sub
||
||
|
|
Nov 21 '05 #6
Tom,
| 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.

List(Of T) is implemented as an Array not a single-linked list, per:

http://msdn2.microsoft.com/library/6...us,vs.80).aspx

<quote>
Implements the System.Collecti ons.Generic.ILi st<> interface using an array
whose size is dynamically increased as required.

</quote>

Note it states "using an array whose size is dynamically increased", this is
the double action I was referring to.

How are you determining "at least 5 additional bytes for each entry"?

You are testing against a List(Of Integer) & not List(Of Object) where each
element is being boxed?

Are you looking at the Capacity compared to Count? Capacity needs to be
larger as this is where the performance comes in. Rather then alloc a new
array & copy all the elements for each element added.

Yes there is some overhead as compared to an array, however I would expect
this to be 4 to 12 bytes per instance of List(Of T), not per element.

FWIW: List(Of T) is the generic version of ArrayList. A couple of advantages
of List(Of T) are that it avoids the "boxing penalty" that ArrayList has,
plus it ensures type safety.

Hope this helps
Jay
"Tom Jastrzebski" <to*@tom.com> wrote in message
news:rN******** ******@newssvr2 3.news.prodigy. net...
| 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
|
|
Nov 21 '05 #7
Hi,

I simply created a List(Of Integer) with one milion Integers (4byte) and
check process memory utilization before and after that.
Yes, I agree that this is a very aproximate method, but I just wanted to
have a general idea.

Tomasz
Nov 21 '05 #8
> No, List(Of T) is backed by a simple array, it's not a linked list.
There's no per-item overhead, not sure how your tests could indicate
that.


Thanks, I just did more research, and figured out that the underlying data
structure is actually more complicated, and that this structure is design
for performance rather then memory usage.

Tomasz
Nov 21 '05 #9
> 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


Och, yes, I understand this by now.
I just wanted to bring this to the attention, since this array usage pattern
produces way better results in VB 6.0, which means that this probably could
be done better. That is really it.

Probably down the road I am not going to use .Net data structures available
out of the box, since memory usage is at stake.
For prototyping and initial testing, however, they are good enough.

Tomasz
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1927
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I store the data of the session in a new multiple dimension array. All data are saved correctly in the array of the 1st page. But in the new array of the 2nd page there is only one entry. Does anybody knows
11
39480
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not rst.EOF i = i + 1 If rst!Something = Then astrMyArray(i) = rst!Something
2
9848
by: farmer | last post by:
I need to populate a listbox of other listview with selected listview items.although the following code works perfecty I want to use an Array (for various reasons) for Each ListItem In listview1.ListItems If ListItem.Checked = True Then listbox1.AddItem ListItem.Text next
11
17658
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
4
2829
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
8
1406
by: Dennis | last post by:
I have two computers both running Windows XP Professional. The computer on which I have Vs.Net 2003 has Framework 1.1 installed which is listed as 1.050.00mb. On the other computer, Framework 1.1 is installed and listed as only 36.14mb. Why the difference. I do note that some applications do not work on the one with 36.14mb sized Framework 1.1. -- Dennis in Houston
5
28404
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
10
12223
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
8
71620
by: Pim75 | last post by:
Hello, I'm defining a string array like: Dim strArray() As String = {"1", "2"} Can I add some values to this string array later in the code? It's not clear to me how to do this. I hope someone can help me. Thanks in advance!
0
10852
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10255
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9382
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4459
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4021
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.