The weirdest way to loop in C# is also the fastest

The weirdest way to loop in C# is also the fastest

Nick Chapsas

1 год назад

252,350 Просмотров

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


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

@nickchapsas
@nickchapsas - 24.11.2022 17:19

For the avoidance of doubt: The technique shown in the video is still considered unsafe (like I mentioned in the video's conclusion) and should not be used unless you really know what you're doing. The reason why it is included in the video is because it doesn't require the use of the unsafe keyword hanse why I said "unsafe code territory" in the beginning. This doesn't make it safe. It just makes it a candidate to compare with other things that don't require the unsafe keyword to be compiled. If we were to use the unsafe keyword then I'd include pointers too and that would be a different story.

Ответить
@TheIvanDM
@TheIvanDM - 08.10.2024 08:26

Will there be an increase in speed if you assign the "length" of the collection to a variable (before the for loop) and use this variable in for expression, instead of getting the length of the collection every time

Ответить
@rmcgraw7943
@rmcgraw7943 - 01.08.2024 10:38

GetPinnableReference()

Ответить
@minnedanhieux1040
@minnedanhieux1040 - 30.05.2024 08:32

Nice video! How does the performance compare to optimized unsafe context code? I landed up in unsafe coming from c++ and that boosted my algorithms. But I'm worried people after me will not be familiar with unsafe context. This is a little nicer syntactic wise(although it still is not for everybody) and so I'm all into span and co. when performance stays maximal.

Ответить
@jimh3595
@jimh3595 - 23.05.2024 18:29

How can i see the Viewer for the low level code? Thanks

Ответить
@lateefkareem
@lateefkareem - 01.05.2024 17:16

since the span does not take into account new changes to the list, it is a natural cool way to perform Gauss Jordan iterative method for solving linear system. I mean Gauss Jordan, not Gauss Siedel.

Ответить
@deus_nsf
@deus_nsf - 14.02.2024 01:30

I really appreciate all your tips and I don't mean to disrespect, but doesn't all this defies the point of .NET? Unless time is a big constraint on optimizing a specific part of the code, a foreach loop will probably get optimized so much in the next iterations of .NET that writing all this will become unecessary, and I had this actually happened to me at multiple occasions (with reflexion for example).
I took the time to write super efficient and elaborate code (ableit very hard to read and to port), all of this to be obliterated by the next version of .NET that made the much simpler version of what I was trying to do not only as fast but also safer with thread concurrency etc... It feels like you're approaching C# and .NET like it's C++ with the STL, I dig it don't get me wrong! But I find it all the more surprising!

Ответить
@thygrrr
@thygrrr - 03.02.2024 21:55

Why not Memory<T>?

Ответить
@racmanov
@racmanov - 29.11.2023 19:08

Why do i need it? Game development and because i want to :) Ty for info

Ответить
@akirakosaintjust
@akirakosaintjust - 27.10.2023 01:14

Wouldn't it be cleaner to just declare an unsafe block and do pointer arithmetic? It'd be the same right?

Ответить
@whatdoyouthink4968
@whatdoyouthink4968 - 27.09.2023 19:10

It still makes sense to use C# for me and not C++ because I only need this in a handful of methods out of 1000s.

Ответить
@JohnDemetriou
@JohnDemetriou - 22.09.2023 11:13

It's basically how we traverse stuff in old fashioned C. Moving the pointer to the next node, moving the array pointer one step further etc

Ответить
@stoic2454
@stoic2454 - 02.09.2023 15:27

If the goal is to ultimately using unsafe method why convert list to listofspans, can we not fetch the address of first element of list also just like we did for array ?

Ответить
@DummyFace123
@DummyFace123 - 22.04.2023 06:22

My question is if you aren’t dealing with an array, but ToArray lose the performance advantage?

Ответить
@HikingUtah
@HikingUtah - 15.04.2023 18:56

I'm really curious why a for loop on a span would be faster than a for loop on an array. Seems like Microsoft needs to do some optimizing on their array code.

Ответить
@jaytee283
@jaytee283 - 29.03.2023 02:53

Love it. Thanks

Ответить
@chadsquad5050
@chadsquad5050 - 24.03.2023 06:37

use nint for indexing with Unsafe.Add for better code gen than int, it will be faster than span that way. So instead of using var i = 0, do nint i = 0

Ответить
@theman3282
@theman3282 - 12.03.2023 06:53

this is just employing pointer arithmetic over contiguous memory locations right?

Ответить
@briumphbimbles
@briumphbimbles - 01.03.2023 15:55

Isn't that just pointer arithmetic (with a whole bunch of abstraction)?

Ответить
@_iPilot
@_iPilot - 17.02.2023 12:10

And this is how we going back to raw C pointers.

Ответить
@TheGerm24
@TheGerm24 - 25.01.2023 16:37

This technique reminds me of writing C and C++ in college.

Ответить
@robertbrzheintzbrz147
@robertbrzheintzbrz147 - 23.01.2023 23:21

OMG. Firstly I would just go back to simple pointers if I go unsafe anyway. Secondly it only works with an int array as you just add 1 to the address. What about data that uses more space?

Ответить
@_zapara_
@_zapara_ - 18.01.2023 14:14

Thank you!!! All your videos are great!

Ответить
@LogicException
@LogicException - 09.01.2023 19:12

Another video title would have been: Youngster meets Pointer, introduces her and doesn't even begin to point out how quickly the car can hit the wall.

Ответить
@dmitrykim3096
@dmitrykim3096 - 06.01.2023 12:29

C++ at it's best

Ответить
@josephl
@josephl - 01.01.2023 03:42

I’d love to see how will this compare with the Parallel.ForEachAsync from .NET 6.0 henceforth…

Ответить
@michaeldurrant8446
@michaeldurrant8446 - 20.12.2022 22:41

Real quick - you are rarely writing code where this actually matters. For sure, don't burn cycles uselessly, but compute cycles are cheaper than brain cycles.

Ответить
@es68951
@es68951 - 17.12.2022 16:17

This is just C code written as C#. Of course it's the fastest, all you need to do is add and deref. The same translates natively to assembly.

Ответить
@BGFutureBG
@BGFutureBG - 13.12.2022 21:35

How to write C++ without writing C++

Ответить
@odytrice
@odytrice - 11.12.2022 20:04

I think it's good to put the knowledge out there and I appreciate the video. However, I feel like there needs to be a strong caveat because beginners and even intermediate devs will want to use these techniques because they are "cool".

My advice is that you should always write correct clear code first. That means, always use foreach loops and only after profiling do you then optimize. Because like the benchmark is showing, you don't really get any tangible benefits unless that code is called 100,000+ times which is true for Framework libraries but rarely true for everyday Line of Business applications. Not to mention the fact that compilers and the JIT are pretty sophisticated and in some cases can automatically do these optimizations like in the case of int[] so in those cases you have uglier code for no benefit whatsoever

I'm sure you already agree with this but you need to make it clear for beginners and intermediate devs who stumble across this

Ответить
@KojiKazama
@KojiKazama - 10.12.2022 23:59

Thank you sir. This may actually prove useful in some of my current projects and I guess Unity game devs might find it useful in certain cases as games often need to be optimized to the millisecond (curious to know hoe it works with Unity gameobjects).

Ответить
@lorenzo42p
@lorenzo42p - 08.12.2022 21:29

seems like the compiler should've been improved, rather than writing hack code.

Ответить
@FrozenKnight21
@FrozenKnight21 - 07.12.2022 10:50

All that work to get back to c style pointers...

Ответить
@garry_personal_brand
@garry_personal_brand - 07.12.2022 10:00

Why do you have to random 420 :)
Its 420 Time

Ответить
@MrBelleblaas
@MrBelleblaas - 06.12.2022 06:43

Duhhh?

Ответить
@portusdelphini
@portusdelphini - 05.12.2022 21:05

Why do you try to achieve c++ in c#?

Ответить
@paulpinecone2464
@paulpinecone2464 - 05.12.2022 12:28

Did you just discover pointer arithmetic? Perhaps MS could invent a new language named D to take advantage of it?

Also, why are you adding the offset every time when you could just be incrementing by the constant sizeof()?

Ответить
@robertbrzheintzbrz147
@robertbrzheintzbrz147 - 04.12.2022 18:26

And where do you benchmark the conversions from whatever you have the data loaded into, to a span?

Ответить
@aaronmurgatroyd5810
@aaronmurgatroyd5810 - 04.12.2022 10:06

I love how we have to write all this code in these new languages now to do something which lower level languages do by default, Assembly, C, and Pascal have used direct memory pointer operations to iterate through arrays since the dawn of time, but to get c# to do it at the same level you have to write a bunch of code… why can t the language simply be smart enough to use pointers internally automatically… Great video though, its good to know this stuff…

Ответить
@EMILE12345678901
@EMILE12345678901 - 04.12.2022 04:51

sounds like C pointers with extra steps

Ответить
@eizeoosting843
@eizeoosting843 - 03.12.2022 23:29

But … converting the Array to the Span using the Marshal also takes time being a loop internally iterating the data in the array copying the data into the span. Surely the whole solution will only be more efficient if you have to iterate over the data multiple times 🤷‍♂️

Ответить
@AlanDias17
@AlanDias17 - 03.12.2022 22:31

Man I tried this span method to copy array to another array and it took 5 times longer than normal for loop. Hm?

Ответить
@christianermann8816
@christianermann8816 - 03.12.2022 22:12

lol I’ll

Ответить
@reillydasilva4218
@reillydasilva4218 - 03.12.2022 20:53

Proof that C# users will do anything at all to avoid C++

Ответить
@ideegeniali
@ideegeniali - 03.12.2022 18:28

use C++ not C# for this!

Ответить
@LukasChodo
@LukasChodo - 03.12.2022 16:50

I like how it starts with lets make fast loop without unsafe code and proceeds with API's that does unsafe operations. Nonetheless it is informative.

Ответить
@RobTheQuant
@RobTheQuant - 03.12.2022 07:16

Great video. I usually do fixed and use pointers if I need performance in C#. It performs as fast as C. UNSAFE is only really unsafe for NOOBS who don't know what they are doing, for us in the know unsafe is totally 100% safe :)

Ответить
@FabioMaulo
@FabioMaulo - 03.12.2022 05:50

IMO all this stuff should be a matter of the compiler. For example the compiler, in many cases, knows which is the real underlying type so, if I use foreach, just because a "fashion" matter, with an array of value types, the compiler should "translates" it with a "for" if it is more efficient.

Ответить
@splugereport
@splugereport - 02.12.2022 22:48

I like many of the comments are like "pointers seem like details". 😂

Ответить