8 comments

  • juancn23 minutes ago
    That&#x27;s usually true of all common hash table implementations (when objects don&#x27;t have a defined order, if they have you can get O(1) average and O(log N) worst case), regardless of language.<p>The O(1) is the expected average case, which usually holds.<p>Yeah, O(N^2) is theoretically possible, but unless you&#x27;re defending against some sort of denial of service attack, in practice it rarely matters.<p>Still, if you can guess a sensible initial size for a hash table you can avoid a lot of the overhead of rehashing.
  • TristanDaCunha18 minutes ago
    Which statement in this article applies only to Python?
  • emil-lp51 minutes ago
    &gt; To put it differently, saying that a hash table is O(1) or constant time is a model<p>Nobody really says that, nor is it a model. It is the expected time complexity.
    • robertlagrant21 minutes ago
      I think people do say a hash table is O(1). It&#x27;s the <i>average</i> time complexity (for some value of average) though, not the worst case.
  • brudgers20 hours ago
    <i>But could we create a hash table that would be truly constant-time? No. As the size of your data structure grows, it requires progressively slower memory.</i><p>At a large enough size to be interesting, everything is dominated by IO and because IO is slow, at any interesting size performance is a matter of tailoring the implementation to the details of the data {0}.<p>Engineering is hard work, not naive math.<p>[0] Data might be arbitrary but it is never random. Not being random is what makes it data.
  • oefrha8 minutes ago
    This &quot;quadratic-time performance&quot; is incredibly disingenuous. First, it&#x27;s doing n operations that are each O(n), so it&#x27;s more like &quot;can have linear time performance, but done n times so I can give you a scary title&quot;.<p><i>Edit: A charitable take is constructing a set&#x2F;dict from a list is indeed a common operation so it&#x27;s worthwhile to think about its complexity, but it&#x27;s not really one of the standard operations when discussing the performance of a hashset&#x2F;hashmap, so really shouldn&#x27;t be this handwavy.</i><p>And instead of attacking some straw man &quot;It is indeed widely believed that ...&quot; claim (widely believed by who?), why not attack what&#x27;s literally on docs.python.org? <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;time-complexity.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;time-complexity.html</a>:<p>&gt; dict<p>&gt; The times listed for dict objects are average-case times, as they assume the hash function for the objects is sufficiently robust to make collisions uncommon. They also assume the keys are well-distributed among the set of possible keys. In the worst case, when every key hashes to the same value, each of the O(1) operations below instead takes O(n) time. They also assume that hashing and comparing a key is O(1). For more detail on the implementation, see How are dictionaries implemented in CPython?.<p>&gt; ...<p>&gt; set, frozenset<p>&gt; See dict as the set and frozenset implementations are similar, and the same caveats apply. In the worst case, O(1) operations instead take O(n) time, and operations that look up every element degrade accordingly.<p><pre><code> +--------------------------------------+------------+ | Operation | Complexity | +--------------------------------------+------------+ | x in s | O(1) | | Copy (s.copy()) [6] [7] | O(n) | | Add (s.add(x)) [1] | O(1) | | Discard (s.discard(x), s.remove(x)) | O(1) | | ... | ... | +--------------------------------------+------------+ </code></pre> You explicitly construct a list of ints that are all multiples of sys.hash_info.modulus and hence all hash to 0, no shit you get that well documented O(n) behavior.<p>The discussion of CPU cache is good though, so why hide that behind this clickbait.
  • javcasas1 day ago
    Java&#x27;s HashMap also has O(log(N)) complexity on hash collision, and that is before memory&#x2F;cache details.<p><a href="https:&#x2F;&#x2F;docs.oracle.com&#x2F;javase&#x2F;8&#x2F;docs&#x2F;api&#x2F;java&#x2F;util&#x2F;HashMap.html" rel="nofollow">https:&#x2F;&#x2F;docs.oracle.com&#x2F;javase&#x2F;8&#x2F;docs&#x2F;api&#x2F;java&#x2F;util&#x2F;HashMap....</a><p>In fact, some studying on data structures probably leads to the conclusion that it is impossible to guarantee that an unbounded set&#x2F;map to have access performance under O(log(N)).
    • aw162110730 minutes ago
      &gt; Java&#x27;s HashMap also has O(log(N)) complexity on hash collision<p>Only for keys that implement Comparable.
    • emil-lp53 minutes ago
      Expected
      • marcosdumay15 minutes ago
        Nowadays I expected an opaque dictionary to be amortized O(1).<p>Granted, one can technically call that O(log(n)), but that&#x27;s not a helpful categorization.
  • 0xa21 day ago
    The map is not the territory.