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

Home Posts Topics Members FAQ

Alternative to ArrayList.Add()

Hi,

My code reads through a file of shapes adding each
one to an ArrayList as it does so.

The problem is that for files with large numbers of
shapes, calling ArrayList.Add(M yShape) is
exceptionally slow.

There is no way of knowing how many shapes need to
be read in so I can't set the ArrayList capacity
and use array indexing to add the shape objects.

I am prepared to use another collection if it
will be quicker and allow quick addition and
retrieval of objects (not so worried about
insertions and deletions), would SortedList.Add( )
be any quicker?

Is there any way around this problem?

Any help will be much appreciated.

Tristan.
Nov 15 '05 #1
2 6217
SortedList would be slower since it is a specialization of ArrayList in a way.
A Hashtable would be faster to insert, but possibly slower to use later.

I don't see where pre-initing a 200 element ArrayList and then using that would
be very slow though. Once the ArrayList gets a good number of items, it grows
itself pretty darn fast.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Tristan" <tr************ @yahoo.co.uk> wrote in message
news:7f******** *************** ***@posting.goo gle.com...
Hi,

My code reads through a file of shapes adding each
one to an ArrayList as it does so.

The problem is that for files with large numbers of
shapes, calling ArrayList.Add(M yShape) is
exceptionally slow.

There is no way of knowing how many shapes need to
be read in so I can't set the ArrayList capacity
and use array indexing to add the shape objects.

I am prepared to use another collection if it
will be quicker and allow quick addition and
retrieval of objects (not so worried about
insertions and deletions), would SortedList.Add( )
be any quicker?

Is there any way around this problem?

Any help will be much appreciated.

Tristan.

Nov 15 '05 #2
Define "Shape"?;

It might not be the collection that's the problem, but the amount and the
size of objects that you're shoving into it...

Also, it would be a good idea to write some code that tries to guess what
the initial capacity capacity of the ArrayList should be...

Remember:
"Capacity is always greater than or equal to Count. If Count exceeds
Capacity while adding elements, the capacity of the list is doubled by
automatically reallocating the internal array."

Reallocation of Large arrays can be slow - if you have an idea of how many
elements you're gonna have then add 10%, you'll probably see a significant
gain in perf.

And don't forget to TrimToSize().

Hope it helps;
Josh.

"Tristan" <tr************ @yahoo.co.uk> wrote in message
news:7f******** *************** ***@posting.goo gle.com...
Hi,

My code reads through a file of shapes adding each
one to an ArrayList as it does so.

The problem is that for files with large numbers of
shapes, calling ArrayList.Add(M yShape) is
exceptionally slow.

There is no way of knowing how many shapes need to
be read in so I can't set the ArrayList capacity
and use array indexing to add the shape objects.

I am prepared to use another collection if it
will be quicker and allow quick addition and
retrieval of objects (not so worried about
insertions and deletions), would SortedList.Add( )
be any quicker?

Is there any way around this problem?

Any help will be much appreciated.

Tristan.

Nov 15 '05 #3

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

Similar topics

43
6926
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app performance. In the hopes of improving performance, we have tried to find a way to avoid the...
4
1722
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties per type. I have written some code that does the trick now, but it involves looping through the private ArrayList and creating a new Array with just the objects of the type corresponding to the property. Problem is, this hapens every time you...
3
2865
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
3
9702
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
4
2475
by: tma | last post by:
I'm trying to save code to a session object like the following: dim oAppList as arraylist dim oApp as someclass Code to manipulate oApp... .... .... oApplist.Add(oApp) session.add("AppList", oApplist)
6
5392
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow = dtLCPack.NewRow(); int intColCount = dtLCPack.Columns.Count; ArrayList arrlRow = (ArrayList)alLCPlist; <== Specified cast
6
1469
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB example I found on the web. I would be most appreciative if someone could explain why this statement is necessary and what does it do: MyArt = New Art
9
5608
by: Matt Bailey | last post by:
I am writing an ASP page with a C# backend. This page uses a StreamReader to input a file and pull out a specific section of the text based on markers inside. While the code I have works, I get a 404 error on files larger than 10 mbs, which will be a common occurence. I understand this might be a question better suited for an ASP forum, but I was wondering if anyone out there knows of an alternative to the StreamReader which will help...
1
11528
by: Greg Larsen | last post by:
How to I populate and reference multi-dimensional ArrayList. I would like to populate an Array list with the Add method something like so: ArrayList a = new ArrayList; ArrayList b = new ArrayList; a.Add = "A"; a.Add = "B"; a.Add = "C"; b.Add = a;
0
9961
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
10778
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
10885
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
9597
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
7990
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
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5817
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
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4244
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.