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

Tagged as

Stats

47.7K 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)


Written By
Software Developer (Senior) Truphone
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Thoughts Pin
PIEBALDconsult17-Jun-10 14:38
professionalPIEBALDconsult17-Jun-10 14:38 
GeneralRe: Thoughts Pin
ricmrodrigues17-Jun-10 22:29
Memberricmrodrigues17-Jun-10 22:29 
GeneralRe: Thoughts Pin
PIEBALDconsult18-Jun-10 3:38
professionalPIEBALDconsult18-Jun-10 3:38 
GeneralIllegitimate comparison Pin
tonyt15-Jun-10 10:14
Membertonyt15-Jun-10 10:14 
GeneralRe: Illegitimate comparison Pin
ricmrodrigues15-Jun-10 22:26
Memberricmrodrigues15-Jun-10 22:26 
GeneralRe: Illegitimate comparison Pin
ricmrodrigues17-Jun-10 3:35
Memberricmrodrigues17-Jun-10 3:35 
GeneralRe: Illegitimate comparison Pin
tonyt19-Jun-10 20:09
Membertonyt19-Jun-10 20:09 
GeneralRe: Illegitimate comparison Pin
ricmrodrigues20-Jun-10 20:55
Memberricmrodrigues20-Jun-10 20:55 
tonyt wrote:
I don't believe the 2.5x faster time to populate a List<t>
verses an array is correct. It can't be, and why are you
comparing calls to Add() rather than calls to the List<t>'s
default indexer ?


If you actually tried it, you'd realize why. It's because it can't be done that way.


tonyt wrote:
The only overhead that List<t> has in that regards, is the
internal version that is incremented every time the list
is changed, to ensure that any enumerators are invalidated
in that case. That should definitely not have that much of
an effect on use of a list verses an array.


The overhead exists, and what prove them, is the results, please post your results, if they're different.
GeneralA concern Pin
Niklas L15-Jun-10 7:10
MemberNiklas L15-Jun-10 7:10 
GeneralRe: A concern Pin
ricmrodrigues15-Jun-10 22:30
Memberricmrodrigues15-Jun-10 22:30 
QuestionKewl Aritcle, Question Pin
supernova566615-Jun-10 3:07
Membersupernova566615-Jun-10 3:07 
AnswerRe: Kewl Aritcle, Question Pin
ricmrodrigues17-Jun-10 3:34
Memberricmrodrigues17-Jun-10 3:34 
GeneralNot the timings I get Pin
Magnus_15-Jun-10 3:05
MemberMagnus_15-Jun-10 3:05 
GeneralRe: Not the timings I get Pin
supernova566615-Jun-10 3:09
Membersupernova566615-Jun-10 3:09 
GeneralRe: Not the timings I get Pin
Magnus_15-Jun-10 3:32
MemberMagnus_15-Jun-10 3:32 
GeneralRe: Not the timings I get Pin
ricmrodrigues15-Jun-10 3:40
Memberricmrodrigues15-Jun-10 3:40 
GeneralRe: Not the timings I get Pin
ricmrodrigues15-Jun-10 3:52
Memberricmrodrigues15-Jun-10 3:52 
GeneralRe: Not the timings I get Pin
Dominic Pettifer15-Jun-10 7:04
MemberDominic Pettifer15-Jun-10 7:04 
GeneralRe: Not the timings I get Pin
ricmrodrigues15-Jun-10 22:27
Memberricmrodrigues15-Jun-10 22:27 
GeneralRe: Not the timings I get Pin
ricmrodrigues17-Jun-10 3:33
Memberricmrodrigues17-Jun-10 3:33 
GeneralTypo in code Pin
Dominic Pettifer17-Jun-10 3:42
MemberDominic Pettifer17-Jun-10 3:42 
GeneralRe: Typo in code Pin
ricmrodrigues17-Jun-10 4:09
Memberricmrodrigues17-Jun-10 4:09 
GeneralStopWatch Pin
giammin15-Jun-10 3:02
Membergiammin15-Jun-10 3:02 
GeneralRe: StopWatch Pin
ricmrodrigues15-Jun-10 3:39
Memberricmrodrigues15-Jun-10 3:39 
GeneralRe: StopWatch Pin
ricmrodrigues17-Jun-10 3:34
Memberricmrodrigues17-Jun-10 3:34 

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.