When the collection class only stores objects of an indefinite number of character types, the differences between system. Collections. arraylist and system. Collections. Specialized. stringcollection

Source: Internet
Author: User

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

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.