How to INCREASE C# Performance using SPAN

  Рет қаралды 9,485

Nick Proud

Nick Proud

Күн бұрын

Пікірлер
@ar_xiv
@ar_xiv 11 ай бұрын
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?
@manojambhore2772
@manojambhore2772 2 жыл бұрын
Really good one Nick, waiting for your benchmark. Net video
@nickproudprogrammer
@nickproudprogrammer 2 жыл бұрын
Thanks! Glad you enjoyed it. Yes Benchmark.NET is on my list for upcoming videos. Stay tuned! :)
@danielbrown7534
@danielbrown7534 Ай бұрын
@@nickproudprogrammer wanted to see the memory allocation
@DuncanSmart
@DuncanSmart Жыл бұрын
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 Жыл бұрын
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.
@Lammot
@Lammot Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
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 11 ай бұрын
​@@nickproudprogrammerlol you suck
@mattmckinstry
@mattmckinstry Жыл бұрын
What were the Allocations like for the example?
@nickproudprogrammer
@nickproudprogrammer Жыл бұрын
Good question. Would need to profile the example to find out
@abdulj1283
@abdulj1283 2 жыл бұрын
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 2 жыл бұрын
Thanks Abdul! Sounds good. I'll add Dependency Injection to my list. Thanks for the input :)
@SkullSkill912
@SkullSkill912 2 жыл бұрын
can you make an httplistener that intercepts a POST, you can edit it, and forward it?
@nickproudprogrammer
@nickproudprogrammer 2 жыл бұрын
Like a relay? Would you just be forwarding the request onto another API?
@SkullSkill912
@SkullSkill912 2 жыл бұрын
​@@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 2 жыл бұрын
@@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
@Texas_1985
@Texas_1985 Жыл бұрын
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 Жыл бұрын
Interesting idea. Thanks!
@sebnig7270
@sebnig7270 2 жыл бұрын
Is it only faster for the alternative for startswith or also for contains helper function?
@iam8401
@iam8401 Жыл бұрын
I wonder the same...
How to Encode or Decode Base 64 in C#
6:59
Nick Proud
Рет қаралды 9 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 261 М.
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
Stackalloc and Spans
30:17
Coding Tutorials
Рет қаралды 11 М.
RECORD TYPES | Exploring C# and DOTNET | Rahul Nath
15:32
Rahul Nath
Рет қаралды 4,2 М.
The Fastest Way to Modify a List in C# | Coding Demo
10:30
Zoran Horvat
Рет қаралды 26 М.
6 C# keywords you (probably) never had to use
17:33
Nick Chapsas
Рет қаралды 69 М.
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 264 М.
Understanding .NET C# Heaps (Deep Dive)
28:23
Raw Coding
Рет қаралды 14 М.
Span of T vs. Memory of T
9:44
Jeff Zuerlein
Рет қаралды 7 М.
7 C# Tips and Tricks to impress your .NET Developer friends
7:48
Writing C# without allocating ANY memory
19:36
Nick Chapsas
Рет қаралды 152 М.