Consider that you r in a situation to use some collection object. What type you will prefer Generic.list or Array
Advantage of generic is, it is easy to handle. use the add, remove and new extension methods in .net2008 but which is efficient.
(i) for( int i = 0; i < 5000000; i++ )
{
for( int j = 0; j < Array.Length; j++ )
{
Array[j] = j;
}
}
(ii)for( int i = 0; i < 5000000; i++ )
{
for( int j = 0; j < List.Count; j++ )
{
List[j] = j;
}
}
the time for the execution is The results where 2620 for the Array and 13200 for the List in terms of nanoseconds.
The reasons are
1. it has to do with the memory not necessarily being contiguous, and so the lookup is slightly more complex
2. List generic version is actually making function calls in the generated code this will understand if u saw the IL code for the above two cases but not in case of array.
3. The List<T> class uses an array internally, and will do effectively the same operation as Array.Resize(). The only real difference between the two is that List<T> always doubles the size of the storage, so that you need to resize fewer and fewer times as the data gets larger. Of course, you could always use that strategy when using Array.Resize() as well,
0 comments:
Post a Comment