473,403 Members | 2,338 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,403 software developers and data experts.

Know how ArrayList Works?

Anyone know the internal working of this thing. I expected it to be
kinda slow at enumerating large lists of objects but it was actually
pretty good. I was able to write a quick linked list class that beat
it at enumerating but was not as fast as the arrayList at actually
adding the items into the list.

This makes me think it is using something like linked arrays. Where
instead of allocating a new node each time an object is added it
allocates an array of say 10 objects and when all 10 are filled it
allocates a new array of 10 and links it w/ the previous array.

This would account for the speed difference. I wonder how it is so
fast at this though:

for(int i = 0; i < myArrayList.count; i++)
{
myArrayList[i];
}

How can this be fast? I'm not telling it I'm going in order so how
does it get such good speed at seemingly random access to the linked
list?

Thanks I'd appreciate any insite anyone may have.
Nov 16 '05 #1
8 5061

"Phill" wrote...
Anyone know the internal working of this thing.
I'm not 100% sure this is the *actual* implementation, but I believe it is:

http://www.dotnet247.com/247referenc...ayList/__rotor

....so why not look into it by yourself? :-)
This makes me think it is using something like linked arrays. Where
instead of allocating a new node each time an object is added it
allocates an array of say 10 objects and when all 10 are filled it
allocates a new array of 10 and links it w/ the previous array.


The list starts with a "capacity" of 16, and then *doubles* the capacity as
required.

// Bjorn A

Nov 16 '05 #2
If you really want to know how it is implemented you should download
Lutz Roeder's Reflector tool from:

http://www.aisto.com/roeder/dotnet/

With it you can decompile the source into C#, VB.Net, or Delphi. This
is about as awesome as it gets for quickly finding out how something in
..Net is implemented. Hope this helps for this and in the future.

Have A Better One!

John M Deal, MCP
Necessity Software

Phill wrote:
Anyone know the internal working of this thing. I expected it to be
kinda slow at enumerating large lists of objects but it was actually
pretty good. I was able to write a quick linked list class that beat
it at enumerating but was not as fast as the arrayList at actually
adding the items into the list.

This makes me think it is using something like linked arrays. Where
instead of allocating a new node each time an object is added it
allocates an array of say 10 objects and when all 10 are filled it
allocates a new array of 10 and links it w/ the previous array.

This would account for the speed difference. I wonder how it is so
fast at this though:

for(int i = 0; i < myArrayList.count; i++)
{
myArrayList[i];
}

How can this be fast? I'm not telling it I'm going in order so how
does it get such good speed at seemingly random access to the linked
list?

Thanks I'd appreciate any insite anyone may have.

Nov 16 '05 #3
Thanks, that is an exceptional tool.

What it shows is that ArrayList in fact uses only a simple object
array.
When it runs out of room it creates a new array 2x the size of the
original. Then it copies the contents of the old array to the new
array and replaces the old array w/ the new one.

I'm perplexed how this is faster than a linked list. Copying all the
values in an array to a new array should be slower than simply
creating a new node.

I guess I have tons to learn about how to write fast .Net programs.
Nov 16 '05 #4
Remember that you can set the initial capacity using one of the ArrayList
constructors. Often you either know how big it needs to be, or you can
estimate or know a typical size. If you allocate enough initial capacity,
often you can avoid some or all of the copying at runtime.

Even when you haven't a clue as to the initial size you may know that the
default size is going to be too small for a starting point.

This same often-overlooked optimization applies to other things too, such as
the initial size of a StringBuilder, which defaults to a miserly 16 bytes.

--Bob

"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Thanks, that is an exceptional tool.

What it shows is that ArrayList in fact uses only a simple object
array.
When it runs out of room it creates a new array 2x the size of the
original. Then it copies the contents of the old array to the new
array and replaces the old array w/ the new one.

I'm perplexed how this is faster than a linked list. Copying all the
values in an array to a new array should be slower than simply
creating a new node.

I guess I have tons to learn about how to write fast .Net programs.

Nov 16 '05 #5
Phill <wa********@yahoo.com> wrote:
Thanks, that is an exceptional tool.

What it shows is that ArrayList in fact uses only a simple object
array.
When it runs out of room it creates a new array 2x the size of the
original. Then it copies the contents of the old array to the new
array and replaces the old array w/ the new one.

I'm perplexed how this is faster than a linked list. Copying all the
values in an array to a new array should be slower than simply
creating a new node.


Yes, but it only needs to happen relatively rarely.

Suppose you have an ArrayList with an initial capacity of 500, and you
add 10000 entries to it. It needs to copy 500+1000+2000+4000+8000
entries - a total of 15500 items copied. However, each of those copies
can be very fast - a direct copy in memory of the kind that processors
often have specific instructions for.

Using a linked list, you need to create an extra 10000 objects. This
will quite possibly involve a few gen0 garbage collections and object
promotion/compaction - all of which involves memory copying too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Yes, setting a good size to begin w/ should improve performance quite
a bit if you know something about the eventual size.

The memory copy indeed must be quite fast.

Jon, Could you explain to me what a gen0 garbage collection is, and
what object
promotion/compactions are? I'm not familiar w/ those terms.
Nov 16 '05 #7
Phill <wa********@yahoo.com> wrote:
Yes, setting a good size to begin w/ should improve performance quite
a bit if you know something about the eventual size.
Yup.
The memory copy indeed must be quite fast.
Sure.
Jon, Could you explain to me what a gen0 garbage collection is, and
what object promotion/compactions are? I'm not familiar w/ those terms.


That's a bit too big for a newsgroup post (while I'm watching The West
Wing, anyway :) Fortunately, Jeffrey Richter has spent time writing it
all nicely!

http://msdn.microsoft.com/msdnmag/is...I/default.aspx

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Thanks, Jon.
Nov 16 '05 #9

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

Similar topics

4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
2
by: Chuck Bowling | last post by:
I'm really stumped on this. Serializing an ArrayList to XML works fine when i use built in classes but when i try storing my own classes in ArrayLists and serializing them the CLR throwns an...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
2
by: Z D | last post by:
Hello, I'm currently using Remoting (HTTP/Binary) to remote a simple object. Everything is working fine except for one function that returns an arraylist of datatables. When I call this...
6
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 =...
2
by: Russ Green | last post by:
I am currently working on a small VB.NET utility that accesses the Microstation VBA object model via COM. I have this code which I am trying to get to work... The key bits of information are ...
5
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
tifoso
by: tifoso | last post by:
I searched here and only found somebody with a round-robin arraylist issue here but what they suggest I tried already and it doesn't work for me I hope somebody can shed some light Scenario: I'm...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
I''ve been working with a CSharp web-site (and am new to it). I have a javascript function that accepts variables from my CSharp code. It all works fine, with the exception of one variable type. I...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.