Arrays vs Lists

2022 ж. 7 Шіл.
5 753 Рет қаралды

What do Santa Claus and the .NET runtime have in common? When they make a list, they check it twice.
Source code available at: github.com/JasperKent/List-Ve...
Topics include:
- C# collection performance
- Comparing generic lists and arrays in .NET
- Underlying collection structures
- Collection benchmarking

Пікірлер
  • What's your worst performance mistake? Let me know in the comments. Source code available at: github.com/JasperKent/List-Versus-Array Remember to subscribe at kzhead.info/tools/qWQzlUDdllnLmtgfSgYTCA.html And if you liked the video, click the 👍.

    @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
  • Your channel is goldmine! Huge respect

    @Mati22011995@Mati22011995 Жыл бұрын
  • I found your channel recently, but I surprised of quality of your videos. Thx for your tutorials 😊

    @kurumi690@kurumi690 Жыл бұрын
  • Thank you for the explanation. Always learn things from you ❤

    @imikhan83@imikhan83 Жыл бұрын
  • Something like a open/closed state of a List would be useful to have the best of both worlds. In open state it would act as a regular List and in closed state would be have a fixed size like an Array. To have .closed() method which would change its internal bool state and free up unassigned memory.

    @tayablackrose29@tayablackrose29 Жыл бұрын
    • Yeah, but I think you'd still have to do the copy.

      @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
  • The trade-off at the end of the video can be harmful in terms of GC. If you call this code many times, GC time can be longer than iteration over the list with the double check. Especially if you have a web app scenario, having 100 slightly slower requests can be better than having 99 faster requests and one really slow one (when GC happens), for example, if you have a timeout at the client side.

    @MrXzxzxc@MrXzxzxc Жыл бұрын
    • Absolutely. IN the end, with any performance question, you've got to measure what is actually effective.

      @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
  • Another gem.

    @davidwhite2011@davidwhite2011 Жыл бұрын
  • Your channel it's awsome.

    @williamgomes3730@williamgomes3730 Жыл бұрын
  • In hot paths the difference can be between meeting or not meeting requirements.

    @RiversJ@RiversJ Жыл бұрын
  • 11:11 wow

    @vendettasnares@vendettasnares Жыл бұрын
  • Beatiful.

    @0i0l0o@0i0l0o Жыл бұрын
  • I got such message when running program. Please, give me a tip what's going on? // Validating benchmarks: Assembly ListVersusArray which defines benchmarks is non-optimized Benchmark was built without enabled (most probably a DEBUG configuration). Please, build it in RELEASE.

    @Maxim_Grekov@Maxim_Grekov Жыл бұрын
    • You can only benchmark a release build. See kzhead.info/sun/fpalopeOfWKtdXA/bejne.html

      @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
    • Much thanks!

      @Maxim_Grekov@Maxim_Grekov Жыл бұрын
  • arrays vs dicts(hash tables), who wins at performance and speed ?

    @muireachgriogalach483@muireachgriogalach483 Жыл бұрын
    • I'll leave it to you to do the benchmarking, but my bet would be on arrays.

      @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
  • Tell me please Jasper, is this example specific only to VS2022?

    @Maxim_Grekov@Maxim_Grekov Жыл бұрын
    • The basic principles apply way back to the early days of .NET Framework. The top-level statements in program.cs only work in C# 9 or later. For older versions, just put all that code in Program.Main().

      @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
  • cool, keep it up ._.

    @dd-rm6ju@dd-rm6ju Жыл бұрын
  • I recommend *against* doing this, but if anyone is curious how you would read array elements without bounds checks, like this: int[] arr = new int[10]; // get reference to element at [0] without bounds checks ref int first = ref MemoryMarshal.GetArrayDataReference(arr); // add 4 elements to the reference, making a new reference that points to element at [4] // then dereference it, now you read the fifth element without bounds checks int fifth = Unsafe.Add(ref first, 4); That being said though, if you write a *for* loop for the array, the bounds checks should be compiled away anyway.

    @petrusion2827@petrusion2827 Жыл бұрын
    • As you say - not recommended.

      @CodingTutorialsAreGo@CodingTutorialsAreGo Жыл бұрын
KZhead