Here Is Why HashSet Is So Fast in .NET

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 พ.ย. 2024

ความคิดเห็น • 22

  • @zoran-horvat
    @zoran-horvat  ปีที่แล้ว +1

    Become a patron and get access to source code and exclusive live streams: www.patreon.com/posts/here-is-why-is-81380423

  • @IlyaBelozertsev
    @IlyaBelozertsev ปีที่แล้ว +5

    Thanks you very much. I recently failed an interview for a good position at a large corporation. I've had big problems with the HashSet topic and this motivated me to study the topic better. I'm happy that I found such a useful video.

  • @XenidRol
    @XenidRol 7 หลายเดือนก่อน +1

    I had a few very specific questions regarding HashSet implementation and your video answered them all. Thank you very much for such a great explanation!

  • @davidcoorey423
    @davidcoorey423 ปีที่แล้ว +1

    A clear and thorough explanation - very impressive!

  • @vardankosakyan5253
    @vardankosakyan5253 ปีที่แล้ว +2

    Extreamly good explanation !❤ Thanks!

  • @xelaksal6690
    @xelaksal6690 2 ปีที่แล้ว +1

    Top level of explanation! Bravo! And great example!

  • @fcvltee7523
    @fcvltee7523 2 ปีที่แล้ว

    Super great video makes excited to learn more and get started! Love the "stay organized" motto !!

  • @tore28
    @tore28 หลายเดือนก่อน

    About the GetHashCode of strings - Some of the strings seems to have produced negative numbers. This is because the int overflows ?

    • @zoran-horvat
      @zoran-horvat  หลายเดือนก่อน

      @@tore28 No, it's because it is signed :)
      Negative hash code is a regular, valid result.

  • @Ram-hv8xo
    @Ram-hv8xo 2 หลายเดือนก่อน

    Hi Zoran Horvat you did well for explanation. as a entry level developer. suggest can u use little bit less technical words. i hard to get the meaning. ;)

    • @zoran-horvat
      @zoran-horvat  2 หลายเดือนก่อน +1

      @@Ram-hv8xo Technically yes, but in reality an impossible task for me, personally... I do understand what you are saying, though. I might change over time.

    • @victormoreno2767
      @victormoreno2767 หลายเดือนก่อน

      Ram, with all due respect. It is my experience in interview for you to raise the bar. Usually devs do not lower their expectations on candidates, it is usually quite the opposite.

  • @danstoian7721
    @danstoian7721 2 ปีที่แล้ว +1

    Hello!
    First of all, thank you for this video! This is great!
    At 5:10, I'm not sure how the math behind that works? By multiplying the base with hashCode itself, will it raise 31 to the power of the base? First time hashCode is 0, so that's correct, but the next value of hashCode depends on obj, so I'm not sure how that works. Thanks!

    • @zoran-horvat
      @zoran-horvat  2 ปีที่แล้ว +2

      If characters (from right to left) are c0, c1, ... , cn, and if int could accommodate the whole result, then the result would be cn*31^n + cn-1*31^(n-1) + ... + c2*31^2 + c1*31^1 + c0*31^0. Due to overflows, the unchecked block will end up with whatever has remained in the 32-bit int, making the whole process approximate, but fast.
      If you read current source code of the string class, you will note that its GetHashCode is shifting left by 5 bits (i.e. multiplying by 32) and then adding a correction (I guess, to move it closer to factor 31). It is another layer of approximation which makes implementation even faster than that based on full multiplication.
      github.com/microsoft/referencesource/blob/master/mscorlib/system/string.cs

    • @vinayv3160
      @vinayv3160 2 ปีที่แล้ว +4

      First of all, thank you very much Zoran. Very useful video. I too had the same question which Dan has asked. After reading your answer and trying to understand it a bit more, I came up with the below note. Sharing it here, in case if it helps anyone else having the same question.
      Let us say, the string is "abcd"

      Note that when we assign a character to an int, the character's ASCII value is assigned.
      In the explanation below, when we assign 'a' to an int, 'a's ASCII value is assigned.

      The foreach loops through each character in the string.

      before 1st iteration
      hashCode = 0

      1st iteration
      hashCode = 31 * 0 + 'a' = 'a' = 31^0 * 'a'

      2nd iteration
      hashCode = 31 * (31^0 * 'a') + 'b' = 31^1 * 'a' + 31^0 * 'b'

      3rd iteration
      hashCode = 31 * (31^1 * 'a' + 31^0 * 'b') + 'c' = 31^2 * 'a' + 31^1 * 'b' + 31^0 * 'c'

      4th iteration
      hashCode = 31 * (31^2 * 'a' + 31^1 * 'b' + 31^0 * 'c') + 'd' = 31^3 * 'a' + 31^2 * 'b' + 31^1* 'c' + 31^0* 'd'

    •  5 หลายเดือนก่อน

      @@vinayv3160 I was about to ask a similar question. I see it has not been answered yet, though

  • @مسوقالخليج
    @مسوقالخليج 2 ปีที่แล้ว

    Sa . It's hard when you look at the software

  • @electrictrojan6719
    @electrictrojan6719 ปีที่แล้ว

    How do you type so fast?

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +2

      Oh I don't! If you really want to know, I write a coding script in my own scripting language and then run it on VS Code. The result is the video as you see it.

  • @magicbhai7477
    @magicbhai7477 2 ปีที่แล้ว +1

    I tNice tutorialnk i'm gonna stick to gaming...

  • @10199able
    @10199able 2 ปีที่แล้ว

    I recently was told a way to produce combined hash using ValueTuple like this : var vt = (this.State, this.MoreState); var hash = vt.GetHashCode(); It calls HashCode.Combine under the hood.

    • @zoran-horvat
      @zoran-horvat  2 ปีที่แล้ว +4

      That should be the same as calling HashCode.Combine on your own, with an important distinction that ValueTuple will not aggregates hash codes if a component is a collection.