I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 161, comments - 1491, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

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!

Print | posted on Saturday, September 01, 2007 9:37 PM

Feedback

# 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]);
}
}




9/2/2007 10:57 AM | sirrocco

# 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.
9/3/2007 11:01 AM | David

# 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 :)
9/3/2007 11:07 AM | Mladen

# 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.
9/3/2007 5:32 PM | Jeff

# 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 :))
9/3/2007 5:36 PM | Mladen

# 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 :-)
9/3/2007 6:22 PM | marc

# 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.
9/4/2007 9:47 AM | Thomas Danecker

# 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

9/4/2007 7:32 PM | Matt Berther

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

thanx matt
9/4/2007 8:13 PM | Mladen

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 5 and type the answer here:

Powered by: