How to INCREASE C# Performance using SPAN

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 23

  • @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! :)

    • @danielbrown7534
      @danielbrown7534 3 дня назад

      @@nickproudprogrammer wanted to see the memory allocation

  • @ar_xiv
    @ar_xiv 9 месяцев назад +1

    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 11 месяцев назад +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  11 месяцев назад +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.

  • @Lammot
    @Lammot 11 месяцев назад +1

    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  11 месяцев назад

      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 11 месяцев назад +1

      @@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  10 месяцев назад

      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 10 месяцев назад

      ​@@nickproudprogrammerlol you suck

  • @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 :)

  • @Texas_1985
    @Texas_1985 Год назад +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.

  • @mattmckinstry
    @mattmckinstry Год назад +1

    What were the Allocations like for the example?

  • @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 Год назад

      I wonder the same...