C # list and Array Performance Comparison

Source: Internet
Author: User

In. before Net Framework 2.0, array representation can be represented by array and arraylist of collection classes. After 2.0 ,. NET introduces the generic concept list <>, So we choose one more.

2.0 the intention of introducing generic models is to reduce the performance consumption caused by packing and unpacking of types.

For example;

Private void compareto <t> (list <t> List)

{

....

}

The list <t> here is a generic type. I don't know what type to input during the call. It may be int, string, or class.

Today, we mainly discuss what kind of performance is higher when the type is determined. arraylist does not need to be considered. It must be the slowest. It can only add object type. here we mainly discuss the performance of list <> and array:

First look at the string situation:

Static void main (string [] ARGs)
{
Const int COUNT = 1000;
String [] array = new string [count];
List <string> List = new list <string> ();
Console. writeline ("the count is: {0}", count );

Console. Write ("the value of total for array :");
Stopwatch stopwatch = new stopwatch ();
Stopwatch. Start ();

For (INT I = 0; I <count; I ++)
{
Array [I] = I. tostring ();;
}

Int thearraytotal = 0;
For (INT I = 0; I <count; I ++)
{
// Thearraytotal + = array [I];

}
Stopwatch. Stop ();
Console. Write (thearraytotal );
Console. writeline ();
Console. Write ("array init time :");

 
Console. Write (stopwatch. elapsedmilliseconds. tostring ());
Stopwatch. Reset ();
Stopwatch. Start ();
Console. writeline ();

For (INT I = 0; I <count; I ++)
{
List. Add (I. tostring ());
}
Int thelisttotal = 0;
Foreach (string V in List)
{
// Thelisttotal + = V;
}
Stopwatch. Stop ();
Console. writeline ("the value of total for list: {0}", thelisttotal );
Console. Write ("list init time :");
Console. Write (stopwatch. elapsedmilliseconds. tostring ());
Stopwatch. Reset ();
Console. Read ();

}

When Count = 1000, there is no difference between the two. The time used is 0.

When Count = 10000, there is no difference

When Count = 100000,

 

The count is: 100000
The value of total for array: 0
Array init time: 16
The value of total for list: 0
List init time: 21

Transport ry is 5 faster than list

 

When Count = 1000000

 

The count is: 1000000
The value of total for array: 0
Array init time: 296
The value of total for list: 0
List init time: 320

 

Arry is 24 faster than list

 

Arry is faster than list in string mode.

 

When int type is used:

Static void main (string [] ARGs)
{
Const int COUNT = 100000;
Int [] array = new int [count];
List <int> List = new list <int> ();
Console. writeline ("the count is: {0}", count );

Console. Write ("the value of total for array :");
Stopwatch stopwatch = new stopwatch ();
Stopwatch. Start ();

For (INT I = 0; I <count; I ++)
{
Array [I] = I;
}

Int thearraytotal = 0;
For (INT I = 0; I <count; I ++)
{
// Thearraytotal + = array [I];

}
Stopwatch. Stop ();
Console. Write (thearraytotal );
Console. writeline ();
Console. Write ("array init time :");

 
Console. Write (stopwatch. elapsedmilliseconds. tostring ());
Stopwatch. Reset ();
Stopwatch. Start ();
Console. writeline ();

For (INT I = 0; I <count; I ++)
{
List. Add (I );
}
Int thelisttotal = 0;
Foreach (int v in List)
{
// Thelisttotal + = V;
}
Stopwatch. Stop ();
Console. writeline ("the value of total for list: {0}", thelisttotal );
Console. Write ("list init time :");
Console. Write (stopwatch. elapsedmilliseconds. tostring ());
Stopwatch. Reset ();
Console. Read ();
}

Modify COUNT = 1000,10000, 100000,000000 and input them as follows:

The count is: 1000
The value of total for array: 0
Array init time: 0
The value of total for list: 0
List init time: 0

 

The count is: 10000
The value of total for array: 0
Array init time: 0
The value of total for list: 0
List init time: 0

 

The count is: 100000
The value of total for array: 0
Array init time: 0
The value of total for list: 0
List init time: 1

 

The count is: 1000000
The value of total for array: 0
Array init time: 7
The value of total for list: 0
List init time: 17

 

The count is: 10000000
The value of total for array: 0
Array init time: 77
The value of total for list: 0
List init time: 218

(Different hardware configurations have different results)

From the above results, the efficiency of array is higher than that of list. When the array length is not very large, there is no difference between the two. We recommend that you use list <>. After all, it is a variable length, can be added. We recommend that you use array for special applications,

Arraylist is not recommended.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.