Click here to Skip to main content
11,570,352 members (55,165 online)
Click here to Skip to main content

Tagged as

Performance comparison of using a List as opposed to an Array

, 17 Jun 2010 CPOL 27.7K 3
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
Questionlist<>.Count Kills Reading Performance Pin
MountainGhost30-Aug-12 6:41
memberMountainGhost30-Aug-12 6:41 
GeneralMy vote of 5 Pin
Dan Randolph8-Jul-12 6:17
memberDan Randolph8-Jul-12 6:17 
GeneralRe: My vote of 5 Pin
Dan Randolph8-Jul-12 7:00
memberDan Randolph8-Jul-12 7:00 
GeneralMy vote of 1 Pin
krishnabhargav16-Jun-10 13:50
memberkrishnabhargav16-Jun-10 13:50 
GeneralRe: My vote of 1 Pin
ricrodrigues16-Jun-10 23:33
memberricrodrigues16-Jun-10 23:33 
AnswerAbout accuracy Pin
Niklas Lindquist17-Jun-10 2:48
memberNiklas Lindquist17-Jun-10 2:48 
GeneralRe: About accuracy Pin
ricrodrigues17-Jun-10 3:21
memberricrodrigues17-Jun-10 3:21 
GeneralRe: About accuracy Pin
ricrodrigues17-Jun-10 3:33
memberricrodrigues17-Jun-10 3:33 
Generalhad similar results ... BUt Pin
krishnabhargav16-Jun-10 13:29
memberkrishnabhargav16-Jun-10 13:29 
GeneralRe: had similar results ... BUt Pin
ricrodrigues16-Jun-10 23:33
memberricrodrigues16-Jun-10 23:33 
GeneralRe: had similar results ... BUt Pin
ricrodrigues17-Jun-10 3:34
memberricrodrigues17-Jun-10 3:34 
GeneralMy vote of 1 Pin
Yaroslav Tatarenko16-Jun-10 10:26
memberYaroslav Tatarenko16-Jun-10 10:26 
GeneralRe: My vote of 1 Pin
ricrodrigues16-Jun-10 23:31
memberricrodrigues16-Jun-10 23:31 
GeneralRe: My vote of 1 Pin
ricrodrigues17-Jun-10 3:36
memberricrodrigues17-Jun-10 3:36 
GeneralRe: My vote of 1 Pin
Yaroslav Tatarenko17-Jun-10 7:18
memberYaroslav Tatarenko17-Jun-10 7:18 
GeneralRe: My vote of 1 Pin
ricrodrigues17-Jun-10 7:29
memberricrodrigues17-Jun-10 7:29 
GeneralMy vote of 1 Pin
Jason McBurney16-Jun-10 6:17
memberJason McBurney16-Jun-10 6:17 
GeneralRe: My vote of 1 Pin
ricrodrigues16-Jun-10 23:31
memberricrodrigues16-Jun-10 23:31 
GeneralRe: My vote of 1 Pin
ricrodrigues17-Jun-10 3:35
memberricrodrigues17-Jun-10 3:35 
GeneralThoughts Pin
PIEBALDconsult15-Jun-10 18:19
mvpPIEBALDconsult15-Jun-10 18:19 
GeneralRe: Thoughts Pin
ricrodrigues15-Jun-10 22:28
memberricrodrigues15-Jun-10 22:28 
GeneralRe: Thoughts Pin
ricrodrigues17-Jun-10 3:35
memberricrodrigues17-Jun-10 3:35 
GeneralRe: Thoughts Pin
PIEBALDconsult17-Jun-10 14:38
mvpPIEBALDconsult17-Jun-10 14:38 
GeneralRe: Thoughts Pin
ricrodrigues17-Jun-10 22:29
memberricrodrigues17-Jun-10 22:29 
GeneralRe: Thoughts Pin
PIEBALDconsult18-Jun-10 3:38
mvpPIEBALDconsult18-Jun-10 3:38 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 17 Jun 2010
Article Copyright 2010 by ricrodrigues
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid