How to INCREASE C# Performance using SPAN

  Рет қаралды 6,324

Nick Proud

Nick Proud

Күн бұрын

Span is a relatively new value type in C# that allows you to target a contiguous region of arbitrary memory. What does that mean?
Well, it means a lot of things, but perhaps mostly notably for .NET developers, it means improved performance when it comes to manipulating or viewing objects.
In this video, I give a simple introduction to Span as a .NET type, I talk about the potential for increased performance for string manipulation in particular, and then I use Benchmark.NET to get some benchmarking results for code that manipulates strings using 'StartsWith' and a Span of string.
My Blog: automationmission.com
LinkedIn: / nickproud
Twitter: / nickproud
#dotnet #csharp #softwareengineer

Пікірлер: 22
@manojambhore2772
@manojambhore2772 Жыл бұрын
Really good one Nick, waiting for your benchmark. Net video
@nickproudprogrammer
@nickproudprogrammer Жыл бұрын
Thanks! Glad you enjoyed it. Yes Benchmark.NET is on my list for upcoming videos. Stay tuned! :)
@ar_xiv
@ar_xiv 4 ай бұрын
Any tips for using span in an Update loop of a game engine? The only way I can figure out how to do it is to make local spans, but this is obviously wrong because they are constantly going out of scope, and converting back and forth from an array to a span every frame is obviously wrong (and actually tanked my framerate). Basically, how can I have a span that I can access like a field?
@DuncanSmart
@DuncanSmart 6 ай бұрын
The main benefit of spans is reducing allocations (that you mentioned at the start) when doing stuff like string manipulation, but you didn’t enable the memory diagnoser. Not sure your example would have been very enlightening for that anyway, but maybe a follow up video on this subject would be interesting.
@nickproudprogrammer
@nickproudprogrammer 5 ай бұрын
Thank you for your feedback! I appreciate your insight on the main benefit of spans and the suggestion for a follow-up video. I'll definitely take that into consideration for future content.
@mattmckinstry
@mattmckinstry 8 ай бұрын
What were the Allocations like for the example?
@nickproudprogrammer
@nickproudprogrammer 8 ай бұрын
Good question. Would need to profile the example to find out
@abdulj1283
@abdulj1283 Жыл бұрын
Nice video Nick. Can you make your next video on Dependency Injection using C# as this concept is sometimes confusing to me. Thanks...
@nickproudprogrammer
@nickproudprogrammer Жыл бұрын
Thanks Abdul! Sounds good. I'll add Dependency Injection to my list. Thanks for the input :)
@TexasJoe1985
@TexasJoe1985 6 ай бұрын
String.StartsWith by default uses a culture-sensitive comparison rule using the current culture, where as you are using an ordinal comparison in your span example. The ordinal comparison is going to be faster because you're just looking at the binary value. It would be interesting to see the benchmark again but where you use StringComparison.Ordinal as the secondary parameter in String.StartsWith.
@nickproudprogrammer
@nickproudprogrammer 5 ай бұрын
Interesting idea. Thanks!
@SkullSkill912
@SkullSkill912 Жыл бұрын
can you make an httplistener that intercepts a POST, you can edit it, and forward it?
@nickproudprogrammer
@nickproudprogrammer Жыл бұрын
Like a relay? Would you just be forwarding the request onto another API?
@SkullSkill912
@SkullSkill912 Жыл бұрын
​@@nickproudprogrammer You send a POST, intercept it to get the data but it wasnt sent to the server yet, then forward it once you got the data.
@nickproudprogrammer
@nickproudprogrammer Жыл бұрын
@@SkullSkill912 Sounds like you want to implement a packet sniffer! Can you explain the use case a little more? It's just that if what I think you want to do is correct, it falls within the realms of ethical (or not so ethical) hacking
@sebnig7270
@sebnig7270 Жыл бұрын
Is it only faster for the alternative for startswith or also for contains helper function?
@iam8401
@iam8401 10 ай бұрын
I wonder the same...
@Lammot
@Lammot 5 ай бұрын
From bad to worse: 0. You should generally try to always return the result of the bench to avoid compiler optimizations for dead code. Not going to pretend that I know what cases will it optimize away, (it obviously didn't optimize entire methods away), but point is - you don't need to guess if you just return. 1. Should've slapped [MemoryDiagnoser] attribute on the tests. 0 extra allocations is half the point of the struct. Maybe you would've seen next issue: 3. You are allocating an array for each method call at line 29. Defeats the whole purpose of having a span there in the first place. 4. You are comparing apples to oranges here. StartsWith compares via a culture specific comparer and span op_equals... 5. The span op equals will ALWAYS return false, because operator will first check the length of spans and then check if they point to the same memory. Which they obviously don't. Were you really ready to advise others on the subject?
@nickproudprogrammer
@nickproudprogrammer 5 ай бұрын
Thanks for your detailed critique. I value constructive feedback but noticed a somewhat pedantic tone. Let's keep the conversation positive and collaborative. I'll certainly consider your technical points in my future content.
@Lammot
@Lammot 5 ай бұрын
@@nickproudprogrammer not trying to get to you personally, jfyi. I do, however, wonder how well did you understand the subject you before you posted a guide for other folks. Just plain curiosity, even if it sounds cruel. ps: future content is future content, but this video demonstrates examples that are plain wrong, which then sticks to people who are new to the subject. Are you planning to do anything about that?
@nickproudprogrammer
@nickproudprogrammer 5 ай бұрын
Fair points, although I think you could've approached it with a bit more grace. The purpose of the video was to demonstrate the differences in processing time when using span compared to similar operations. 1. MemoryDiagnoser - I've acknowledged feedback from viewers that this would be also nice to see. I simply chose to show execution time in this high level intro video. 3? (Did 2 get missed?) You're correct, this use case doesn't necessarily reflect the point of using span in the real world. Thanks for this. 4. This is your best point, and it's kindly pointed out by other viewers also. I'll put a note in the description for this and it will be central to a version 2 video when I can get to film it. 5. Same as above. Thanks for this Thanks again for helping me improve my content. I am very careful to ensure that I'm clear when I'm dispensing advice and when I'm demonstrating an example. I think there's a lot in this video which can be improved, thanks to your feedback 😊
@dr.angerous
@dr.angerous 4 ай бұрын
​@@nickproudprogrammerlol you suck
How to Encode or Decode Base 64 in C#
6:59
Nick Proud
Рет қаралды 7 М.
Stackalloc and Spans
30:17
Coding Tutorials
Рет қаралды 8 М.
How To Choose Ramen Date Night 🍜
00:58
Jojo Sim
Рет қаралды 57 МЛН
Stupid man 👨😂
00:20
Nadir Show
Рет қаралды 29 МЛН
Achieving compile-time performance  with Reflection in C#
21:34
Nick Chapsas
Рет қаралды 33 М.
Python for Unity Editor 2023
4:15
Error Code Zero
Рет қаралды 2,1 М.
Measure Your Application’s Performance in .NET
12:41
Nick Chapsas
Рет қаралды 33 М.
5 (Extreme) Performance Tips in C#
12:26
LevelUp
Рет қаралды 72 М.
Where are types allocated in .NET and why people get it so wrong
14:35
What does the ref keyword actually do in C#?
13:59
Nick Chapsas
Рет қаралды 27 М.
6 INSANE Things You Didn't Know You Could Write in C#
12:26
Nick Chapsas
Рет қаралды 49 М.
phone charge game #viral #tranding #new #reels
0:18
YODHA GAMING RAAS
Рет қаралды 12 МЛН
IPad Pro fix screen
1:01
Tamar DB (mt)
Рет қаралды 2,6 МЛН
M4 iPad Pro Impressions: Well This is Awkward
12:51
Marques Brownlee
Рет қаралды 6 МЛН
Распаковка айфона в воде😱 #shorts
0:25
Mevaza
Рет қаралды 1,6 МЛН
Радиоприемник из фольги, стаканчика и светодиода с батарейкой?
1:00
Добавления ключа в домофон ДомРу
0:18