Common array performance tests in. Net 3.5 Environments

Source: Internet
Author: User

It seems boring, but no one is doing it. Let me work hard.

Below are some simple tests. The Unit is ms. Note that the number of loops used here is 100, and the number of loops is.

These records can be used as a reference for programming.

Test Platform:

Pentium 1.6g dual-core CPU
1 GB memory
Test the debugging environment of vs2008.

1. arraylist (100 W, 1 W)

Stopwatch Timer =   New Stopwatch ();
Timer. Start ();

system. collections. arraylist Al = New system. collections. arraylist ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> Al. add (I);
}

Timer. Stop ();

Console. Write (timer. elapsedmilliseconds. tostring ()+ "\ T");

Stopwatch timer1= NewStopwatch ();
Timer1.start ();

for ( int I = 0 ; I 10000 ; I + )
{< br> Al. contains (I);
}

Timer1.stop ();

Console. writeline (timer1.elapsedmilliseconds. tostring ());

98 775
160 891
107 773
193 769

Ii. hashtable (100 W, 1 W)

Stopwatch Timer =   New Stopwatch ();
Timer. Start ();

system. collections. hashtable HT = New system. collections. hashtable ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. add (I, I);
}

Timer. Stop ();

Console. Write (timer. elapsedmilliseconds. tostring ()+ "\ T");

Stopwatch timer1= NewStopwatch ();
Timer1.start ();

for ( int I = 0 ; I 10000 ; I + )
{< br> ht. containskey (I);
}

Timer1.stop ();

Console. writeline (timer1.elapsedmilliseconds. tostring ());
}

375 0
673 0
540 0
495 0

Increase timer1 to 1 million (hashtable (100 W, 100 W ))

389 139
616 277
516 140
610 277

Iii. hashset (100 W, 100 W)

Stopwatch Timer =   New Stopwatch ();
Timer. Start ();

system. collections. generic. hashset int > HT = New system. collections. generic. hashset int > ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. add (I);
}

Timer. Stop ();

Console. Write (timer. elapsedmilliseconds. tostring ()+ "\ T");

Stopwatch timer1= NewStopwatch ();
Timer1.start ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. contains (I);
}

Timer1.stop ();

Console. writeline (timer1.elapsedmilliseconds. tostring ());

89 32
79 32
79 32
117 31

Iv. List (100 W, 1 W)

Stopwatch Timer =   New Stopwatch ();
Timer. Start ();

system. collections. generic. list int > HT = New system. collections. generic. list int > ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. add (I);
}

Timer. Stop ();

Console. Write (timer. elapsedmilliseconds. tostring ()+ "\ T");

Stopwatch timer1= NewStopwatch ();
Timer1.start ();

for ( int I = 0 ; I 10000 ; I + )
{< br> ht. contains (I);
}

Timer1.stop ();

Console. writeline (timer1.elapsedmilliseconds. tostring ());

16 379
19 392
18 403
18 392

Replace list <int> with list <Object>.

96 945
157 1033
106 909
193 910

Change to string, I. tostring ()

496 1238
531 1190
572 1246
536 1258

V. Dictionary (100 W, 100 W)

Stopwatch Timer =   New Stopwatch ();
Timer. Start ();

System. Collections. Generic. Dictionary<Int,Int>HT= NewSystem. Collections. Generic. Dictionary<Int,Int>();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. add (I, I);
}

Timer. Stop ();

Console. Write (timer. elapsedmilliseconds. tostring ()+ "\ T");

Stopwatch timer1= NewStopwatch ();
Timer1.start ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. containskey (I);
}

Timer1.stop ();

Console. writeline (timer1.elapsedmilliseconds. tostring ());

113 35
125 34
124 34
126 34

6. dictionary LINQ query (100 W, 100 W)

Try using a piece of LINQ.Stopwatch Timer= NewStopwatch ();
Timer. Start ();

system. collections. generic. dictionary int , int > HT = New system. collections. generic. dictionary int , int > ();

for ( int I = 0 ; I 1000000 ; I + )
{< br> ht. add (I, I);
}

Timer. Stop ();

Console. Write (timer. elapsedmilliseconds. tostring ()+ "\ T");

Stopwatch timer1= NewStopwatch ();
Timer1.start ();

For ( Int I =   0 ; I <   1000000 ; I ++ )
{
Ht. Where (C => C. Key = I );
}

Timer1.stop ();

Console. writeline (timer1.elapsedmilliseconds. tostring ());

112 177
107 78
125 70
107 82

Conclusion:
1. If cache is used, the hashset of a single generic set brought by 3.5 can replace list. Although the loading speed is a little slower, the query speed is much faster than the list generic type. It should be noted that the list query in the above test is tens of thousands, and the hashset is millions.

2. The dictionary generic type can replace hashtable, although it may not be so obvious under the character or object type. However, in digital matching, dictionary is about twice faster than hashtable, and this overhead is estimated to be caused by hashtable packing.

3. The speed of LINQ is slower.

By yurow. http://www.cnblogs.com/birdshover/

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.