Click here to Skip to main content
Licence CPOL
First Posted 15 Jun 2010
Views 14,334
Bookmarked 2 times

Performance comparison of using a List as opposed to an Array

By | 17 Jun 2010 | Article
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.

//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!!

//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>:

//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)

About the Author

ricrodrigues

Software Developer (Senior)
Truphone
Portugal Portugal

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberkrishnabhargav13:50 16 Jun '10  
GeneralRe: My vote of 1 Pinmemberricrodrigues23:33 16 Jun '10  
AnswerAbout accuracy PinmemberNiklas Lindquist2:48 17 Jun '10  
GeneralRe: About accuracy Pinmemberricrodrigues3:21 17 Jun '10  
GeneralRe: About accuracy Pinmemberricrodrigues3:33 17 Jun '10  
Generalhad similar results ... BUt Pinmemberkrishnabhargav13:29 16 Jun '10  
GeneralRe: had similar results ... BUt Pinmemberricrodrigues23:33 16 Jun '10  
GeneralRe: had similar results ... BUt Pinmemberricrodrigues3:34 17 Jun '10  
GeneralMy vote of 1 PinmemberYaroslav Tatarenko10:26 16 Jun '10  
GeneralRe: My vote of 1 Pinmemberricrodrigues23:31 16 Jun '10  
GeneralRe: My vote of 1 Pinmemberricrodrigues3:36 17 Jun '10  
GeneralRe: My vote of 1 PinmemberYaroslav Tatarenko7:18 17 Jun '10  
GeneralRe: My vote of 1 Pinmemberricrodrigues7:29 17 Jun '10  
GeneralMy vote of 1 PinmemberJason McBurney6:17 16 Jun '10  
GeneralRe: My vote of 1 Pinmemberricrodrigues23:31 16 Jun '10  
GeneralRe: My vote of 1 Pinmemberricrodrigues3:35 17 Jun '10  
GeneralThoughts PinmvpPIEBALDconsult18:19 15 Jun '10  
GeneralRe: Thoughts Pinmemberricrodrigues22:28 15 Jun '10  
GeneralRe: Thoughts Pinmemberricrodrigues3:35 17 Jun '10  
GeneralRe: Thoughts PinmvpPIEBALDconsult14:38 17 Jun '10  
GeneralRe: Thoughts Pinmemberricrodrigues22:29 17 Jun '10  
GeneralRe: Thoughts PinmvpPIEBALDconsult3:38 18 Jun '10  
GeneralIllegitimate comparison PinPopularmembertonyt10:14 15 Jun '10  
GeneralRe: Illegitimate comparison Pinmemberricrodrigues22:26 15 Jun '10  
GeneralRe: Illegitimate comparison Pinmemberricrodrigues3:35 17 Jun '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Jun 2010
Article Copyright 2010 by ricrodrigues
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid