Click here to Skip to main content
15,121,564 members
Articles / Programming Languages / C#
Article
Posted 15 Jun 2010

Tagged as

Stats

45.5K views
3 bookmarked

Performance comparison of using a List as opposed to an Array

Rate me:
Please Sign up or sign in to vote.
3.06/5 (13 votes)
17 Jun 2010CPOL
Benchmark both List and string[].

Introduction

The objective of this article is to elucidate you, fellow developers, to really evaluate the need of using a generic List<T> instead of an old string[], since usually the cost is overlooked.

Using the Code

I've built a simple example to compare the performance for reading and writing to a List<string> and a string[]. Concerning the writing time in milliseconds, for 100000 randomly generated items, the string[] outperformed the List<string> by approximately 8ms.

C#
//Write
const int size = 100000;
Stopwatch startList = new Stopwatch();
startList.Start();
List<string> listString = new List<string>(size);
for (int i = 0; i <= size; i++)
{
    string toAdd = Guid.NewGuid().ToString();
    listString.Add(toAdd);
}
startList.Stop();
TimeSpan durationList = startList.Elapsed;

Stopwatch startArray = new Stopwatch();
startArray.Start();
string[] arrayString = new string[size];
for (int i = 0; i < size; i++)
{
    string toAdd = Guid.NewGuid().ToString();
    arrayString[i] = toAdd;
}
startArray.Stop();
TimeSpan durationArray= startArray.Elapsed;

When reading, the string[] outperforms the List<string> by approximately 2.5 times faster!!

C#
//Read
Stopwatch startListRead = new Stopwatch();
startListRead.Start();
for (int i = 0; i <= listString.Count - 1; i++)
{
    string str = listString[i];
}
startListRead.Stop();
TimeSpan durationListRead = startListRead.Elapsed;

Stopwatch startArrayRead = new Stopwatch();
startArrayRead.Start();
for (int i = 0; i <= arrayString.Length - 1; i++)
{
    string str = arrayString[i];
}
startArrayRead.Stop();
TimeSpan durationArrayRead = startArrayRead.Elapsed;

And you can still do this with LINQ to Objects with an array, as you do with your List<string>:

C#
//Perform our LINQ query on an Array just like a List<T>
var result = from i in arrayString
         where i.StartsWith("a")
         select i;

Results:

benchmarkres.png

So, bottom line, you should consider if you really need the List<T> as opposed to an array because there are serious performance costs that will affect your application.

Points of Interest

Creating concerns on performance of instructions being used in your applications.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ricmrodrigues
Software Developer (Senior) Truphone
Portugal Portugal
No Biography provided

Comments and Discussions

 
Questionlist<>.Count Kills Reading Performance Pin
MountainGhost30-Aug-12 7:41
MemberMountainGhost30-Aug-12 7:41 
GeneralMy vote of 5 Pin
Dan Randolph8-Jul-12 7:17
MemberDan Randolph8-Jul-12 7:17 
GeneralRe: My vote of 5 Pin
Dan Randolph8-Jul-12 8:00
MemberDan Randolph8-Jul-12 8:00 
More good news for generic Lists. It is actually slightly faster to fill a list than an array one element at a time once you get over 200000 elements. You can also use the List.ToArray() method to copy the list contents back to an array. This operation is so fast that you can ignore the time spent (consumes 3.5% of the time that the full array read takes). So if you need to access all elements in a List (or a large number of them), just convert List to array first.

A big advantage of List is that you don't need to know the size ahead of time, and most apps use less than 1000 elements per List, so there is no measurable time difference between arrays and lists at these small sizes.
GeneralMy vote of 1 Pin
krishnabhargav16-Jun-10 14:50
Memberkrishnabhargav16-Jun-10 14:50 
GeneralRe: My vote of 1 Pin
ricmrodrigues17-Jun-10 0:33
Memberricmrodrigues17-Jun-10 0:33 
AnswerAbout accuracy Pin
Niklas L17-Jun-10 3:48
MemberNiklas L17-Jun-10 3:48 
GeneralRe: About accuracy Pin
ricmrodrigues17-Jun-10 4:21
Memberricmrodrigues17-Jun-10 4:21 
GeneralRe: About accuracy Pin
ricmrodrigues17-Jun-10 4:33
Memberricmrodrigues17-Jun-10 4:33 
Generalhad similar results ... BUt Pin
krishnabhargav16-Jun-10 14:29
Memberkrishnabhargav16-Jun-10 14:29 
GeneralRe: had similar results ... BUt Pin
ricmrodrigues17-Jun-10 0:33
Memberricmrodrigues17-Jun-10 0:33 
GeneralRe: had similar results ... BUt Pin
ricmrodrigues17-Jun-10 4:34
Memberricmrodrigues17-Jun-10 4:34 
GeneralMy vote of 1 Pin
Yaroslav Tatarenko16-Jun-10 11:26
MemberYaroslav Tatarenko16-Jun-10 11:26 
GeneralRe: My vote of 1 Pin
ricmrodrigues17-Jun-10 0:31
Memberricmrodrigues17-Jun-10 0:31 
GeneralRe: My vote of 1 Pin
ricmrodrigues17-Jun-10 4:36
Memberricmrodrigues17-Jun-10 4:36 
GeneralRe: My vote of 1 Pin
Yaroslav Tatarenko17-Jun-10 8:18
MemberYaroslav Tatarenko17-Jun-10 8:18 
GeneralRe: My vote of 1 Pin
ricmrodrigues17-Jun-10 8:29
Memberricmrodrigues17-Jun-10 8:29 
GeneralMy vote of 1 Pin
Jason McBurney16-Jun-10 7:17
MemberJason McBurney16-Jun-10 7:17 
GeneralRe: My vote of 1 Pin
ricmrodrigues17-Jun-10 0:31
Memberricmrodrigues17-Jun-10 0:31 
GeneralRe: My vote of 1 Pin
ricmrodrigues17-Jun-10 4:35
Memberricmrodrigues17-Jun-10 4:35 
GeneralThoughts Pin
PIEBALDconsult15-Jun-10 19:19
professionalPIEBALDconsult15-Jun-10 19:19 
GeneralRe: Thoughts Pin
ricmrodrigues15-Jun-10 23:28
Memberricmrodrigues15-Jun-10 23:28 
GeneralRe: Thoughts Pin
ricmrodrigues17-Jun-10 4:35
Memberricmrodrigues17-Jun-10 4:35 
GeneralRe: Thoughts Pin
PIEBALDconsult17-Jun-10 15:38
professionalPIEBALDconsult17-Jun-10 15:38 
GeneralRe: Thoughts Pin
ricmrodrigues17-Jun-10 23:29
Memberricmrodrigues17-Jun-10 23:29 
GeneralRe: Thoughts Pin
PIEBALDconsult18-Jun-10 4:38
professionalPIEBALDconsult18-Jun-10 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.