How to INCREASE C# Performance using SPAN

Поділитися
Вставка
  • Опубліковано 8 чер 2024
  • 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 Рік тому +3

    Really good one Nick, waiting for your benchmark. Net video

    • @nickproudprogrammer
      @nickproudprogrammer  Рік тому +1

      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 місяців тому +1

    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  6 місяців тому +1

      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.

  • @TexasJoe1985
    @TexasJoe1985 7 місяців тому +1

    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.

  • @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  Рік тому

      Thanks Abdul! Sounds good. I'll add Dependency Injection to my list. Thanks for the input :)

  • @mattmckinstry
    @mattmckinstry 9 місяців тому +1

    What were the Allocations like for the example?

    • @nickproudprogrammer
      @nickproudprogrammer  8 місяців тому

      Good question. Would need to profile the example to find out

  • @SkullSkill912
    @SkullSkill912 Рік тому +1

    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 Рік тому +1

    Is it only faster for the alternative for startswith or also for contains helper function?

    • @iam8401
      @iam8401 10 місяців тому

      I wonder the same...

  • @Lammot
    @Lammot 6 місяців тому

    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  6 місяців тому

      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 6 місяців тому

      @@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  6 місяців тому

      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 5 місяців тому

      ​@@nickproudprogrammerlol you suck