473,382 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.GetUpperBound(0) + 1
ReDim Preserve SomeArray(c)
SomeArray(c) = SomeObject
<end loop>

Dim al As ArrayList = ArrayList.Adapter(SomeArray)
<begin loop>
al.Add(SomeObject)
<end loop>
Nov 21 '05 #1
5 6882
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***@executespammers.org> wrote in message
news:%2****************@TK2MSFTNGP15.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.GetUpperBound(0) + 1
ReDim Preserve SomeArray(c)
SomeArray(c) = SomeObject
<end loop>

Dim al As ArrayList = ArrayList.Adapter(SomeArray)
<begin loop>
al.Add(SomeObject)
<end loop>

Nov 21 '05 #2
"Paul" <no***@executespammers.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***@executespammers.org> wrote in message
news:%2****************@TK2MSFTNGP15.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.GetUpperBound(0) + 1
| ReDim Preserve SomeArray(c)
| SomeArray(c) = SomeObject
| <end loop>
|
| Dim al As ArrayList = ArrayList.Adapter(SomeArray)
| <begin loop>
| al.Add(SomeObject)
| <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
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...
4
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; ...
6
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
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...
2
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...
1
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...
8
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.