473,806 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

performance of arraylist.add versus redim preserve

Off the cuff, does anyone know if arraylist is more efficeint at adding
items to an array than redim preserve?

Paul

<begin loop>
Dim c As Integer = SomeArray.GetUp perBound(0) + 1
ReDim Preserve SomeArray(c)
SomeArray(c) = SomeObject
<end loop>

Dim al As ArrayList = ArrayList.Adapt er(SomeArray)
<begin loop>
al.Add(SomeObje ct)
<end loop>
Nov 21 '05 #1
5 6910
Totally. Not even worth asking the question ;)

(google for performance of this and you'll see some benchies somewhere -
it's a no brainer).....

"Paul" <no***@executes pammers.org> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Off the cuff, does anyone know if arraylist is more efficeint at adding
items to an array than redim preserve?

Paul

<begin loop>
Dim c As Integer = SomeArray.GetUp perBound(0) + 1
ReDim Preserve SomeArray(c)
SomeArray(c) = SomeObject
<end loop>

Dim al As ArrayList = ArrayList.Adapt er(SomeArray)
<begin loop>
al.Add(SomeObje ct)
<end loop>

Nov 21 '05 #2
"Paul" <no***@executes pammers.org> schrieb:
Off the cuff, does anyone know if arraylist is more efficeint at adding
items to an array than redim preserve?


Yes, it is, especially if the size of the list changes often.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #3
Paul,
In addition to the other comments:

| Off the cuff, does anyone know if arraylist is more efficeint at adding
| items to an array than redim preserve?

If you ReDim Preserve a single element at a time, then ArrayList is far more
efficient. Remember that the ArrayList allocates an internal array with 16
elements, each time it needs to expand this internal array it doubles the
internal array's size.

However if you over allocate your ReDim Preserve by doubling the size of the
array as ArrayList does internally, then performance should be about the
same.

Ideally in both cases you should allocate the number of expected elements in
both cases to prevent needing to resize the array itself or the array
internal to the ArrayList. As this expansion of the array itself or the
ArrayList's internal array is what is hurting performance, as a new array
needs to be allocated & the old array is copied to the new array.

If you over allocate your ReDim Preserve, you might be able to simple do a
final ReDim Preserve when you exit the loop to "return" the array to its
true size.

Also remember if your array handles value types, then using an ArrayList may
hurt performance as the elements will need to be boxed when put into the
array & unboxed when taking out. This boxing of the elements may also cause
extra pressure on the GC.

Hope this helps
Jay

"Paul" <no***@executes pammers.org> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
| Off the cuff, does anyone know if arraylist is more efficeint at adding
| items to an array than redim preserve?
|
| Paul
|
| <begin loop>
| Dim c As Integer = SomeArray.GetUp perBound(0) + 1
| ReDim Preserve SomeArray(c)
| SomeArray(c) = SomeObject
| <end loop>
|
| Dim al As ArrayList = ArrayList.Adapt er(SomeArray)
| <begin loop>
| al.Add(SomeObje ct)
| <end loop>
|
|
Nov 21 '05 #4
Thanks, all. Good info.

Paul
Nov 21 '05 #5
If I know the maximum number of elements that might be added, I dimension an
array first to that size, set the elements of the array to the values then
redim preserve to the size of the actual array needed. This, I think is the
most efficient way if you don't want to change the no. of elements later.
Also, you don't have to cast the element each time you use it later.
--
Dennis in Houston
"Paul" wrote:
Thanks, all. Good info.

Paul

Nov 21 '05 #6

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

Similar topics

2
4756
by: | last post by:
Is it correct to think that after reducing the populated array's size from say, 10 to 5 with redim preserve myArray(i) an attempt to access an element above the fifth does not cause a compillation error "array out of script", but returns whatever heppened to be written in that memory address (in particular it might return the correct values of those elements before re-dimentioning)? This seems to be the case in my code, yet I wanted to...
4
4328
by: Matt Garman | last post by:
Is there any difference, performance-wise, accessing elements of a vector using iterators or the subscript operator? In other words, say I have a vector of strings: vector<string> strvec; After some processing, the vector has a very large (>50,000) number of elements. Is there any performance difference in the following methods of vector access?
6
6626
by: LP | last post by:
Hello, What is C# equivalent of rediming an array and preserving exciting elements. VB.NET syntax looks something like this: ReDim Preserve myArray(5) Thank you
6
33123
by: John Grandy | last post by:
Does C# have an equivalent for VB.NET's Redim Preserve ? ReDim Preserve increases the final dimension of any array while preserving the array's contents (however, the type of the array may not be changed).
2
2583
by: D. Shane Fowlkes | last post by:
I've been reading up on Arrays in ASP.NET. I'm going to create an two dimensional array of some type to contain 5 columns but a variable amount of rows. I read up on the ArrayList function and the HashTable as well. In the two books I have, there's no mention of the limitations of an ArrayList and it can only contain a single column array. But from the examples, I've seen, this seems to be true. The HashTable, as far as I can tell, can...
1
2297
by: keyser soze | last post by:
hi REDIM Preserve reports an "out of range" i first create an array, store it into a session var then, in other page, i load restore the session var into a local array but, after this, i can't REDIM Preserve thanks ks
8
869
by: =?Utf-8?B?UmljaEc=?= | last post by:
I have an application written in VB.NET (Visual Studio 2003 and Framework 1.1 SP1). When it runs on an XP machine it takes a few seconds. Running the same code with the same data on a Windows 2003 Server machine it takes over 3 times longer. The spec of the machines is identical and both are not running anything else. The code does nothing special and uses standard VB. It does manipulate collections of class objects but I don't...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10624
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
10371
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
7650
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
5546
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...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.