This morning I suddenly rememberedStringcollection, ItString set
When we only store objects of an indefinite number of character types, is it true?Is stringcollection more efficient than arraylist?
First write a consoleProgram
Using System;
Using System. collections;
Using System. Collections. Specialized;
Namespace Leleapplication1
{
Class Class1
{
[Stathread]
Static Void Main ( String [] ARGs)
{
String TEM;
Arraylist = New Arraylist ();
Stringcollection s = New Stringcollection ();
For ( Int I = 1 ; I < 3000000 ; I ++ )
{
StringStr=I. tostring ();
A. Add (STR );
S. Add (STR );
}
// Returns the stringenumerator that can access stringcollection cyclically.
Stringenumerator se = S. getenumerator ();
// The explicit interface implementation returns ienumerator, which can access stringcollection cyclically.
Ienumerator ies = (Ienumerable) s). getenumerator ();
// Returns the number of enumerations that can access arraylist cyclically.
Ienumerator IEA = A. getenumerator ();
Long Start;
Long End;
Long Result;
Start = Datetime. Now. ticks;
While (SE. movenext ())
{
TEM=Se. Current;
}
End = Datetime. Now. ticks;
Result = End - Start;
Console. writeline ( " Stringcollection run ticks: " + Result. tostring ());
Start = Datetime. Now. ticks;
While (Ies. movenext ())
{
TEM=(String) Ies. Current;
}
End = Datetime. Now. ticks;
Result = End - Start;
Console. writeline ( " Stringcollection 2 run ticks: " + Result. tostring ());
Start = Datetime. Now. ticks;
While (IEA. movenext ())
{
TEM=(String) IEA. Current;
}
End = Datetime. Now. ticks;
Result = End - Start;
Console. writeline ( " Arraylist run ticks: " + Result. tostring ());
Console. Readline ();
}
}
}
The result of one operation is as follows::
Retried several timesThe arraylist uses the shortest time to traverse elements in the set.
Why is such a result?.StringcollectionWhere exactly is it used??
I hope the Knowledge mentioned above will prompt you.
Of course, you are welcome to contact us and correct us.
Author: aierong
Blog: http://www.cnblogs.com/aierong
Email: aierong@126.com