Coder Social home page Coder Social logo

Comments (3)

Yomguithereal avatar Yomguithereal commented on June 9, 2024

Hello @RickWong,

Why would that be?

Your numbers are well in the range of O(log2(n)) so it might just be that you are hitting a normal worst/best case scenario here since you are adding the numbers in order, which is specific case. Can you try adding them in reverse and check if you obtain some sort of symmetry regarding the number of comparisons?

from mnemonist.

RickWong avatar RickWong commented on June 9, 2024

Hello @Yomguithereal. Here's the reverse range push:

let comp = 0;
let heap = MinHeap.from([0], (a, b) => (comp++, a - b));
range(65535, 1).forEach((i) => heap.push(i));
console.log(comp); // 917522

comp = 0;
heap = MaxHeap.from([0], (a, b) => (comp++, a - b));
range(65535, 1).forEach((i) => heap.push(i));
console.log(comp); // 65550

(Also, there seems to be a slight negligible difference when initializing the above heaps with [] instead of [0].)

And the reverse range from:

let comp = 0;
let heap = MinHeap.from(range(65535, 1), (a, b) => (comp++, a - b));
console.log(comp); // 98286

comp = 0;
heap = MaxHeap.from(range(65535, 1), (a, b) => (comp++, a - b));
console.log(comp); // 131038

So back to the first benchmark, given that it's specifically the ascending number range that favors MinHeap.push, I guess the performance question is why does MaxHeap.from perform better than MinHeap.from?

from mnemonist.

Yomguithereal avatar Yomguithereal commented on June 9, 2024

I think this is just a side effect of the ordering and the fact that the underlying binary tree representation must be balanced more often in one case vs. the other. But the asymptotic logarithmic holds correctly there, so from an analytical standpoint, the performance is equivalent at least. You would have to check the code to understand why a case needs more rebalancing than the other but please note inserting elements in order is a known worst case for a heap algorithm. This said, there remain a possibility that my code has a subtle bad branch somewhere, so if you have time to investigate, what could be nice would be to replicate your benchmark using python and its heapq module (on which my code is actually based) to assess whether you see the same kind of discrepancies between min/max number. If not, maybe I did something wrong.

from mnemonist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.