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

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

Nick Chapsas

1 год назад

155,736 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

SHIVANAND YADAV
SHIVANAND YADAV - 24.10.2023 09:03

Which IDE are you using?

Ответить
Jason Olivarez
Jason Olivarez - 03.10.2023 05:28

I see what you did in your random seed… 80085!

Ответить
Anish Jain
Anish Jain - 14.08.2023 02:15

Magic

Ответить
Программист на колесе
Программист на колесе - 04.08.2023 20:50

I wonder, can you mutate the list when other iteration methods are used? No side effects expected?

Ответить
Sahuagin
Sahuagin - 19.07.2023 22:30

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

Ответить
Miguel Fajardo
Miguel Fajardo - 23.05.2023 22:53

What a great video and information for iterating a List !! I think that this works with a List of object that has plenty of properties...

Ответить
Francisco Eugênio Romanini Nabas
Francisco Eugênio Romanini Nabas - 03.05.2023 03:53

Imma steal this to my PowerShell

Ответить
Suraj Rohira
Suraj Rohira - 21.04.2023 20:32

Love these kind of videos, and so well explained!

Ответить
M P
M P - 21.04.2023 09:24

Hey Nick, where is your telegram channel?

Ответить
Cameron Reuschel
Cameron Reuschel - 14.04.2023 00:53

Why isn't this all optimized away? I'd expect all non-parallel versions to perform exactly the same with a properly optimizing compiler/JIT. So what's up here?

Ответить
Rajesh singh
Rajesh singh - 28.03.2023 10:37

Hi Nick, when simple iterate over the items it has performance boost in span but when I'm adding additional functionality into it than its performance reduced and 'for' loop has giving slightly better performance over the Span i.e. CollectionMarshal.AsSpan . Please check.

Ответить
Thick_propheT
Thick_propheT - 07.03.2023 11:24

Windows firewall was blocking my benchmark that used `Unsafe.Add(...)`. I was able to get it to stop doing that, but now i'm seeing `AccessViolationExceptions`. can't figure out why that would be the case. shouldn't the for loops `i < span.Length` check prevent an overrun?

Ответить
MF
MF - 27.02.2023 18:35

Great content ♥!

Ответить
Иван Иванов
Иван Иванов - 16.02.2023 12:15

Hello! At my job, we had an ague with colleagues what is better if you need to represent a collection in your DTO: List or Array. I think array is better. I had my own bunch of arguments about this, but would love to listen to your opinion about this question. Is there any chance you will make a video with close topic?

Ответить
jansenzxc
jansenzxc - 09.02.2023 22:19

help , why cant i use collectionsmarshal, i already added 'using System.Runtime.InteropServices'

Ответить
big mazi
big mazi - 07.02.2023 21:57

Extra way: walk via unconditional (!) "for" loop, exit via catching "OutOfBounds" exception. Removes double-checking of bounds, but introduces overhead from exception handling. May outperform if the list is extremely huge (throw cost is constant and doesn't scale with items count).

Ответить
ZerikScythe
ZerikScythe - 06.02.2023 03:46

dat seed doe...😏

Ответить
Eric Tarelli
Eric Tarelli - 05.02.2023 06:00

Hi, great video! its so usefull. Thanks. I have a question, how can i implement it in async method? what is the best practices in async?

Ответить
Filipe Leite
Filipe Leite - 21.01.2023 01:23

Hi Nick. I tried this using Rider in macOS with the arm64 version of .NET 7 and I get very different results. The regular foreach is much slower than For in every test for 100, 100K and 1M. More than double for 1M. Any reasons come to mind?

Ответить
Pegie L. Gallenero
Pegie L. Gallenero - 20.12.2022 13:33

There's another way to iterate the span in a async?

Ответить