The fastest way to iterate a List in C# is NOT what you think

preview_player
Показать описание

Hello everybody I'm Nick and in this video I will show you all the way you can iterate a List in C# and then show you what is by far the fastest and most memory efficient way. You might have guessed where this is going :)

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

I love the humor of using 80085 as your seed and I'm not sure if anyone else caught this old calculator dirty humor

ooples
Автор

Nick should get a shirt with "I Stan the Span" printed on it

capsey_
Автор

.ForEach is not a LINQ method it is actually defined on List<T>

Sahuagin
Автор

Great benchmark.
Good that you explained that the parallel versions are most likely faster when you actually do work inside the iterations.

It always distracts me when you say "half the speed" when you mean "half the time" (ie. double the speed).
I know it might be a language thingy, but it is really confusing at times.

ristopaasivirta
Автор

I've been a C# software engineer for a few years now and until this video I've never heard of a Span (outside the context of html lol). Going to be looking that up, great vid!

Zindawg
Автор

I like how this channel gives a clear statement on what will be answered in a moment. Example: (1) At 1:12 Nick says, "I'm going to put all the ways to iterate over a list here" (2) This allows the user to pause the video and try and think of ways. (3) Then click play and view the 7 different ways. Its great that something like "Feel free to pause the video and try...." as most viewers know this and would not do it anyway. Side note - I was able to only think of three ways. (for loop, and forearch loop, ToArray().Select(x=>x) )

FunWithBits
Автор

Your videos are the best and I have learned so much from watching your videos. Keep up the fantastic work! I've been coding for over 30years and I'm still learning new tricks, thanks!

urbanguest
Автор

Can't express enough how amazing and educational your videos are. Keep doing what you do!!

Tal__Shachar
Автор

Nick is a legend. Integrating "easter eggs" like 69, 1337 and 80085 always makes me smile

zhelyrh
Автор

A nice extension method is in order :D
public static void FastForEach<T>(this List<T> source, Action<T> action)
{
var span =
foreach (var t in span)
{
action(t);
}
}

LoKSET
Автор

This was a great video. I love your little deep dives into the C# language.

GarethDoherty
Автор

That final console output is really good resume to keep in mind the use of iterations in c#. Very useful 👏👏

sergiom.
Автор

Great video! I certainly could have used this on previous projects but will make sure it gives me the benefits I need for my current ones.

spacetravelnerd
Автор

Very detailed and helpful showing you the results of the typical options Andrew ones I had not seen before

itaylorm
Автор

Great video as always! I also did some collection benchmarks back in April 2022, and i found something different than your benchmarks: for loops were considerably faster than foreach loops. The problem in your benchmark code might be that you access the private field instead of using a variable. foreach will automatically inline the field access to a variable, but for does not do that. In my benchmarks, foreach loops on List<T> were 50% slower than for loops.

IAmFeOx
Автор

Didn’t know about Span(), never would have thought to look, foreach was already heaven, thank you.
I was taught never add or remove items during a for loop but dd it anyway, then fast forward to writing multithreaded applications and foreach and Span() throwing an exception is a useful indicator of faulty design.

paulembleton
Автор

As an old guy, I want to add that, if you don't care about the order of iteration (and all the parallel examples illustrate this is the case here), you can run the index backwards, which avoids calling Count, Size, or Length on each iteration:
var asSpan =
for (int i = asSpan.Length; --i >= 0; )
{
var item = asSpan[i];
}
Note that I explicitly declared i as a signed type so the loop termination condition can be satisfied.

Many of the other examples (not using Span) also fail if the collection is changed during the iteration. The difference is that with the cases that use an enumerator you deterministically get a specific exception, whereas with the Span you just get mysterious behaviour (which is also true for the direct indexing loop).

kevinmartin
Автор

It was as expected. Nothing beats the standard WHILE loop except doing things in parallel - which comes with a lot of downsides.

redguard
Автор

Interesting. I could use this to speed some things up. Seems pretty niche for most of what I do tho, but definitely an improvement where improvements can be made

hipihypnoctice
Автор

LINQ doesn't have a ForEach extension method. What's being used in the video looks like the ForEach method defined by List<T>.

jondoty