The Fastest Way to Modify a List in C# | Coding Demo

  Рет қаралды 20,528

Zoran Horvat

Zoran Horvat

Күн бұрын

Become a patron and get access to source code and exclusive live streams: / fastest-way-to-c-81380759
LINQ is too slow to be of use when modifying lists/arrays, and for loop is not applicable to both, nor is it the fastest either. Watch this demo and learn how to modify contents of an array and a list uniformly, and still with running time that is way below other methods.
In this demo, we will measure performance of iterating through an array and through a list, using common for loop, a LINQ expression, and a span. The rest of the demo is dedicated to measuring performance of operations that mutate content of an array or a list.
Each experiment is followed by a benchmark and explanation of the results gathered by running the benchmark. Eventually, spans will be a clean winner in all respects: execution speed, minimum memory footprint, random access to memory locations, universal support for arrays and lists.
And there will be more! You will learn the dangers that hide behind using a span created from a list, and how to avoid making a bug.
Chapters:
00:00 Intro
00:46 Improving iteration performance
04:02 Improving performance of modifying a list
06:14 Using a span for fast list modification
09:27 Analysis and comment on performance data
Learn more from video courses:
Beginning Object-oriented Programming with C# ► codinghelmet.com/go/beginning...
Collections and Generics in C# ► codinghelmet.com/go/collectio...
Making Your C# Code More Object-oriented ► codinghelmet.com/go/making-yo...
Other courses at Pluralsight ► codinghelmet.com/go/pluralsight
Other courses at Udemy ► codinghelmet.com/go/udemy
Other videos on this channel you may be interested in watching:
Using GitHub Copilot to Write Complex Code | Step-by-step Tutorial ► • Using GitHub Copilot t...
Coding with GitHub Copilot - Beginner to Master | VS Code Demo ► • A Comprehensive Guide ...
What is Covariance and Contravariance in C# ► • What is Covariance and...
How to Initialize a Clean ASP.NET Core Project with Entity Framework Core and Identity ► • How to Initialize a Cl...
The Null Conundrum: A Guide to Optional Objects in C# ► • How to Avoid Null Refe...

Пікірлер: 27
@zoran-horvat
@zoran-horvat Жыл бұрын
Become a patron and get access to source code and exclusive live streams: www.patreon.com/posts/fastest-way-to-c-81380759
@leethompson1216
@leethompson1216 2 күн бұрын
Thanks a lot - Great explanation!
@michaltomorowicz596
@michaltomorowicz596 Жыл бұрын
Great video, many thanks. Really appreciate mentions of not only the perf gains but also the potential dangers of this approach... Looking forward to the future content already!
@10199able
@10199able Жыл бұрын
I find spans very helpful during string manipulation, because I dont have to create new strings on the heap :)
@zoran-horvat
@zoran-horvat Жыл бұрын
True, string class is imposing needless overhead, and it is often paid in bulks. String manipulation was a recurring topic at the time when spans first appeared.
@muhammedemin6842
@muhammedemin6842 Жыл бұрын
I really like your teaching syle and examples. I used spans on a project yesterday and I've seen the difference. Thanks a lot, you are great! 🙌🏻👏🏻
@zoran-horvat
@zoran-horvat Жыл бұрын
I am just about to apply spans to a large code base. I was postponing that process, because it will not be easy to complete - but with expected 50%+ reduced execution time, the decision is clear...
@Sp1tfire100
@Sp1tfire100 7 ай бұрын
Great and easy to understand explanation. Thanks a lot👍
@Wil_Bloodworth
@Wil_Bloodworth 27 күн бұрын
You might want to read the Liskov Substitution Principle again as it only applies to derived/child classes not just any general substitution. Span does not inherit/derive from List so this has nothing to do with the LSP principle and you're not violating LSP with your example. Maybe Barbara watches your channel and she can comment. LOL
@zoran-horvat
@zoran-horvat 27 күн бұрын
LSP is not about classes but about types. So much about who should read the principle again. Regarding my video, I am not sure which timestamp in the video you are referring to. Before clarifying, be aware that assigning a list to a span without recreating the object makes the list a subtype of a span, quite formally. It is the same effect as what you would accomplish by defining a custom implicit cast operator without a direct subclass relationship. Any cast operation defines a subtype relationship, and implicit cast does that with full formality. Now with a lowered cynicism on your side and more seriousness, I would like to hear which timestamp in the video should cause concern.
@timur2887
@timur2887 2 ай бұрын
spans are ref structs, containing a pointer to data and a length for its slice, so they allocated on stack only
@julianocs87
@julianocs87 6 ай бұрын
Great video. Interesting insights on Spans. I'm always looking for opportunities to use them.
@_NguyenManhToan_
@_NguyenManhToan_ Жыл бұрын
Thank you very much!
@quantume7143
@quantume7143 Жыл бұрын
Great video! :)
@muhammedalikhan7559
@muhammedalikhan7559 3 ай бұрын
Legend! ❤
@mumk
@mumk 2 ай бұрын
interesting video, thank you sir
@_NguyenManhToan_
@_NguyenManhToan_ Жыл бұрын
Great video ❤❤❤❤❤❤
@kmsskyquake7330
@kmsskyquake7330 7 ай бұрын
very helpfull video , but other than that i am genuinely curious about your typing speed beacuse ,it looked extremly high in the video !!😄😄
@matheosmattsson2811
@matheosmattsson2811 7 ай бұрын
Good video! This may be a stupid question, but how does creating a Span from a list not allocate any new memory if the List is originally on the Heap but a Span is always on the Stack? I may have misunderstood something (probably), but could you briefly explain what happens during that "conversion"? Is the List copied from the Heap to the Stack or is the Span on the Stack just referencing memory on the Heap?
@zoran-horvat
@zoran-horvat 7 ай бұрын
You have some good questions here, and I will try to give a short answer. Nevertheless, I would suggest that you read the documentation for spans very carefully. Span is a struct on the stack which only references (inside!) An array on heap. That prevents collection of the array and lets you perform optimized operations using that span, faster than using regular access. There are many consequences. One is that changing the list may cause the list object to reference a new array in memory while span still holds the old one. You may think you are modifying the list, but you are not. As soon as the span goes out of scope, the old array will be subject to collection.
@matheosmattsson2811
@matheosmattsson2811 7 ай бұрын
@@zoran-horvat Thank you for the quick answer :) So a Span is simply a struct containing pointers and basic logic for reading (and modifying) already allocated memory through references? If I understand correctly :D I will read up on spans!
@srkidd12
@srkidd12 7 ай бұрын
What about altering properties in the items that are within the Span? That likely ok? Not removing/adding items themselves.
@zoran-horvat
@zoran-horvat 7 ай бұрын
Span is just a reference to an array. It doesn't cause reallocation of the array, but only gives you access to the elements. In that light, only getting or setting the values through the span does not cause any issues.
@user-yv9ff5mn2o
@user-yv9ff5mn2o 7 ай бұрын
i don't understand listofspan transform.. and whre is the actual implementation?
@icaroamorim3123
@icaroamorim3123 3 ай бұрын
cant use CollectionsMarshall
@dotvkab
@dotvkab 7 күн бұрын
очередной маг выдает спаны за чудо божие 😂
Chain of Responsibility to the Rescue!
9:15
Zoran Horvat
Рет қаралды 10 М.
A Complete .NET Developer's Guide to Span with Stephen Toub
1:02:48
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 35 МЛН
Please be kind🙏
00:34
ISSEI / いっせい
Рет қаралды 56 МЛН
New Gadgets! Bycycle 4.0 🚲 #shorts
00:14
BongBee Family
Рет қаралды 18 МЛН
Which one is the best? #katebrush #shorts
00:12
Kate Brush
Рет қаралды 21 МЛН
Explain generics in C# ?
0:59
Interview Happy
Рет қаралды 16 М.
Build Your Own Option Type in C# and Use It Like a Pro
18:31
Zoran Horvat
Рет қаралды 12 М.
Stackalloc and Spans
30:17
Coding Tutorials
Рет қаралды 9 М.
C# List
11:35
codaza
Рет қаралды 43 М.
17 Pieces of C# Syntax That Make Your Code Short
12:41
Zoran Horvat
Рет қаралды 19 М.
Stop using LINQ to order your primitive collections in C#
14:57
Nick Chapsas
Рет қаралды 96 М.
How to Avoid Null Reference Exceptions: Optional Objects in C#
18:13
I Lied! The Fastest C# Loop Is Even Weirder
11:50
Nick Chapsas
Рет қаралды 45 М.
Mem VPN - в Apple Store
0:30
AndroHack
Рет қаралды 106 М.
📦Он вам не медведь! Обзор FlyingBear S1
18:26
TOP-18 ФИШЕК iOS 18
17:09
Wylsacom
Рет қаралды 728 М.