C# - Arrays vs Arraylist
-
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results; -
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results;Why dont you write your own IList object like to your queries
-
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results; -
snorkie wrote:
We had a senior developer
I hope for your part, the tense is correct ;P
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - coming soon
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) -
I believe he didn't learn any programming languages coverd Array before he jumped into Senior position of .NET
-
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results;snorkie wrote:
string[] results = new string[arraylist.count]; arraylist.CopyTo(results, 0); return results;
Of course this is utterly inefficient. Everybody knows that a recursive function is always faster than a non-recursive one. So CopyTo() needs to be replaced by a recursive function! And by the way, there is nothing to stop you from writing recursive subrounties in TRS-80 BASIC! ;P
_____________________________________ Action without thought is not action Action without emotion is not life
-
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results;Did you say senior :~ !!! my GOD X|
Redwan Al-Bougha
-
Did you say senior :~ !!! my GOD X|
Redwan Al-Bougha
Redwan Al-Bougha wrote:
say senior
Yes, he did, and in a reply he mentions the senior developer is no longer with them.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results;I don't know what he was smoking, an
ArrayList
or genericList<>
simply manage a single array under the covers, resizing it as required. You need to be able to allocate a contiguous array to hold all the elements. Maybe he was confused with C++'sdeque
, which does generally manage multiple array segments. .NET has no concept of near or far heaps. It does have a Large Object Heap which is used for large allocations; the belief is that large objects will generally be kept around so the Large Object Heap is swept at the same time as a Generation 2 garbage collection. This can keep otherwise short-lifed objects around. The choice of regular or Large Object heaps is only down to the size of the objects themselves: a 1MB array or a 1MB capacity for an ArrayList makes no difference. One distinction betweenArrayList
and, say, anint[]
is thatArrayList
manages anobject[]
, which causes value types to be boxed when added to the collection and unboxed when they're used. In contrast the genericList<int>
manages anint[]
, but that means an actual copy of the implementation is required, and generated by the JIT, for every different value type that is used as a type parameter. The same implementation is used for all reference types.string
is, of course, a reference type. Arrays themselves are reference types so there should be no difference in passing an array or an ArrayList between methods. He probably managed to measure JIT time and got an incorrect answer, or simply got a page fault or a reschedule in the middle of a measurement. It can be convenient to build something in anArrayList
then copy to an array when you don't know how big the array needs to be at the outset, and you can't change the rest of the code to use anArrayList
instead. If you wrote your own, you'd probably end up mirroring the code inArrayList
itself. Many .NET controls and methods support binding to anIList
to give you some flexibility in the container you use."Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu
-
Yeah, he is gone. But on the lighter side, we find all sorts of gems in our code that make us shake our heads and laugh. Hogan
-
I don't know what he was smoking, an
ArrayList
or genericList<>
simply manage a single array under the covers, resizing it as required. You need to be able to allocate a contiguous array to hold all the elements. Maybe he was confused with C++'sdeque
, which does generally manage multiple array segments. .NET has no concept of near or far heaps. It does have a Large Object Heap which is used for large allocations; the belief is that large objects will generally be kept around so the Large Object Heap is swept at the same time as a Generation 2 garbage collection. This can keep otherwise short-lifed objects around. The choice of regular or Large Object heaps is only down to the size of the objects themselves: a 1MB array or a 1MB capacity for an ArrayList makes no difference. One distinction betweenArrayList
and, say, anint[]
is thatArrayList
manages anobject[]
, which causes value types to be boxed when added to the collection and unboxed when they're used. In contrast the genericList<int>
manages anint[]
, but that means an actual copy of the implementation is required, and generated by the JIT, for every different value type that is used as a type parameter. The same implementation is used for all reference types.string
is, of course, a reference type. Arrays themselves are reference types so there should be no difference in passing an array or an ArrayList between methods. He probably managed to measure JIT time and got an incorrect answer, or simply got a page fault or a reschedule in the middle of a measurement. It can be convenient to build something in anArrayList
then copy to an array when you don't know how big the array needs to be at the outset, and you can't change the rest of the code to use anArrayList
instead. If you wrote your own, you'd probably end up mirroring the code inArrayList
itself. Many .NET controls and methods support binding to anIList
to give you some flexibility in the container you use."Multithreading is just one damn thing after, before, or simultaneous with another." - Andrei Alexandrescu
IList obviously keeped back an HTable and under the sorting algorithim it has used the QuicSort. So it can be use an ArrayList of generic or any enumeratic variations that hold the algorithm. There many ways to develop for any special queries by an arraylist. There have some question;;; 1- Why? 2- What is our aim 3- What is our strategy it is linking to the platform and technology 4- which is cheaper for our credentials 5- Prefere
-
I was going through our documentation and noticed this gem. We had a senior developer who created our development standards. This was part of the document he wrote for us. We had the .NET 2.0 framework available to us (hint: Generics) when this was written. I hope you find this as entertaining as I did. On a side note, when the efficiencies were brought to his attention and refuted with a sample app, he was indignant and would not even consider any other way. He no longer works here!
There are times that information needs to be collected and distributed around the program. To aid in the collection of data and
passing it, Microsoft added collections. The two most popular are arrays and arraylists.An array takes its space out of the near heap. It is very possible to run this out by adding to the array. Arraylist takes its
space out of the far heap and in 64KB chunks. When building the collection, arraylists are the best thing to use for speed.
HOWEVER, when passing the information from method to method, an arraylist is not the most efficient taking up to 60,000 flops to pass
the information therefore arrays are faster.q: How do we strike a happy medium on this?
a: Use both. Since most of the data that we collect are strings, we can do the following. Build the arraylist when collecting
data, the pass the data to an array when collecting is complete. The following code is a great example on this:string[] results = new string[arraylist.count];
arraylist.CopyTo(results, 0);
return results;I don't get why people cling to arrays in C#, they are less easy to use than egneric Lists and hvae a similary large overhead (the amount of methods available for an array is pretty much the same as for a List). From what heard there is also no speed improvement. And Arraylist should be used as rarely as possible, if all elements have the same type, it's easier to work ith them instead of first having to cast each object. Besides worker threads, that only have 1 object as possible parameter, I can't think of the nedd to hae differnt bkect types in the same collection.
modified on Tuesday, February 24, 2009 9:16 AM