Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

Little .Net tip: Start using List<T>.ForEach()

I haven't been using this function at all. Who knows why... maybe because i didn't know about it :)

But in my latest pet project i had to do something really quickly on a lot of items in a list.

After a bit of research into foreach and for loops, i saw the List<T>.ForEach method.

 

Did some testing and sure thing List<T>.ForEach proved to be faster than others.

 

I thought about posting the whole setup and how i've tested it, but then i saw that Dustin Russell Campbell

did exactly the same thing a bit more thoroughly, so why double it up, right?

Read it, like it, USE it!

 

Performance of foreach vs. List.ForEach

 

Simply rocks!

Legacy Comments


sirrocco
2007-09-02
re: Little .Net tip: Start using List<T>.ForEach()
List.ForEach - is faster than foreach because underneath List.ForEach uses a for loop :D
Here's how ForEach does it :

public void ForEach(Action<T> action)
{
if (action == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
}
for (int i = 0; i < this._size; i++)
{
action(this._items[i]);
}
}





David
2007-09-03
re: Little .Net tip: Start using List<T>.ForEach()
But it's but ugly. I can't remember I ever need my (empty) for loop to be just a little bit faster. Plus you loose refactoring capabilities (foreach <-> for), intellisense and so on.

Mladen
2007-09-03
re: Little .Net tip: Start using List<T>.ForEach()
you do? i haven't noticed that at all...
plus somehow i think it's acctually prettier :)

Jeff
2007-09-03
re: Little .Net tip: Start using List<T>.ForEach()
I agree with David, I don't think it's worth it unless you really, really need to do it. I think it is much easier to read and write using a standard for-each or for loop.

Mladen
2007-09-03
re: Little .Net tip: Start using List<T>.ForEach()
i agree with him too... for not so large list lengths.

but when you come to million item range and exec time becomes important this is usefull to know.

I never said you have to use it exclusively :))

marc
2007-09-03
re: Little .Net tip: Start using List<T>.ForEach()
I find the standard "foreach(object o in list)" way easier to read and way more maintainable, than the List.ForEach(.....) constructs - but that's just me.

Performance isn't everything - maintenance is :-)

Thomas Danecker
2007-09-04
re: Little .Net tip: Start using List<T>.ForEach()
sirrocco, you're right, it's using a for loop but the difference is, that the for loop directly indexes the array, which is much faster than using the List<T>'s indexer in a for loop (indexing an array may be optimized to some simple pointer arithmetics). And it's also much faster than using foreach, because foreach would use an enumerator to iterate through the list.

Matt Berther
2007-09-04
re: Little .Net tip: Start using List<T>.ForEach()
Mladen asked if I woud post a link to my thoughts on the subject since he has trackbacks disabled.

http://www.mattberther.com/?p=912


Mladen
2007-09-04
re: Little .Net tip: Start using List<T>.ForEach()
thanx matt