Performance comparisons for List and ArrayList
In the process of using non-generic collections such as ArrayList, boxing and unboxing operations can have significant performance losses, and there is no such problem with generic collections. List is generic, and ArrayList is non-generic. Save data island ArrayList All need special object, read and convert to the corresponding data type, list is not required.
Used to record the start and end times
DateTime starttime = new DateTime ();
DateTime endtime = new DateTime ();
Defines an instance of a collection type ArrayList
ArrayList list = new ArrayList ();
Get current time
StartTime = DateTime.Now;
★★★★★★★★① use ArrayList class ★★★★★★★★
The parameter of the Add method of ArrayList is object type,
When we pass int as a parameter, we need to do a boxing operation.
Boxing operation converts a value type to type Object
for (int i = 0; i < 1000000; i++)
{
List. ADD (i);
}
int icount = 0;
We need to do a unboxing operation when we use INT type
The unboxing operation converts the application type to object type, and the unpacking process does a lot of work
foreach (int i in list)
{
Icount + 1;
}
Console.WriteLine ("The result of using ArrayList: {0}", icount.tostring ());
Get end time and calculate difference
Endtime = DateTime.Now;
TimeSpan ts = endtime-starttime;
Console.WriteLine ("Time Consuming to use ArrayList:" + TS. TotalMilliseconds);
★★★★★★★★② using generic class ★★★★★★★★
Use the generic definition list<t>,int type of list to replace this with an int in a compiler-generated class
The boxing unboxing operation is no longer performed during execution
list<int> list2 = new list<int> ();
StartTime = DateTime.Now;
for (int i = 0; i < 1000000; i++)
{
List2. ADD (i);
}
icount = 0;
foreach (int i in List2)
{
Icount + 1;
}
Console.WriteLine (Result of "using generics: {0}", icount.tostring ());
Endtime = DateTime.Now;
ts = endtime-starttime;
Console.WriteLine ("Time consuming to use generics:" + TS.) TotalMilliseconds);
Show three results as follows
/* First time
* Time-consuming use of ArrayList: 92
* Time-consuming use of generics: 25
*
* Second time
* Time-consuming use of ArrayList: 96
* Time-consuming use of generics: 22
*
* Third time
* Time-consuming use of ArrayList: 90
* Time-consuming use of generics: 22
*
* The difference between the two can be clearly seen
* Here is only time, does not include the statistics on memory consumption
*
*※ but also notice that the difference in the unit is milliseconds,
* I just want to show that generics are good,
* But do not excessively pursue the merits of the procedure,
* In short, to grasp this degree, for their own is the best
* O (∩_∩) o~
*/
Console.read ();
Advantages and disadvantages of ArrayList and LinkedList
Java programming We use the most of the several classes can be string,arraylist,hashmap. Especially ArrayList we almost never know, There's even a suspicion of random use. Let's look at the difference between ArrayList and LinkedList. So the name thinking ArrayList is an array table, LinkedList is a linked table. All the data for ArrayList is on the same address, Each of the LinkedList's data has its own address.
Let's compare the insertion of commonly used data, the deletion of data, the updating of data, and the query of data.
Data insertion: For example, inserting a new data in the I node
ArrayList: Loops to the I node, inserts a new data, and then adds 1 to the index of all the data behind the I node. -> More operations
LinkedList: Loop to the I node, link the successor of the previous node to the new data, and then link the new data to the latter data. -> less operation
Conclusion: The average efficiency linkedlist is better.
Data deletion: For example, delete I node data
ArrayList: Loop to the I node, then subtract the index of all the data behind the I node by 1. -> More operations
LinkedList: Loop to the I node, then link the previous node to the last data on the I node. -> less operation
Conclusion: The average efficiency linkedlist is better.
Data query: For example, query I node data
ArrayList: Loop to i node. -> less operation
LinkedList: Loop to i node. -> operations are small, but because each data address is not the same, the query such as ArrayList slow.
Conclusion: The average efficiency ArrayList is better.
Data update: For example, Update I node data
ArrayList: Loop to the I node to update the data. -> less operation
LinkedList: Loop to i node, update the front data. -> less operation, but because the query speed is not ArrayList good, all the efficiency is not ArrayList good.
Conclusion: The average efficiency ArrayList is better.
So we can see that each has its merits.
If the query operation more ArrayList the effect is better. If deleted, inserting more linkedlist effect is better. Concrete how to use also see specific needs.