Comments (3)
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.
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.
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)
- Allowing modern Javascript syntax (or specifying the earliest target Ecmascript version) HOT 5
- KDTree constructor not working as documented HOT 4
- Missing OrderedMap or SortedMap data structure HOT 1
- Negative sampling tree HOT 1
- longestCommonSubsequence return wrong result HOT 1
- Multiset.size=NaN when removing non-existent element HOT 2
- Distinguishing MultiSet & Counter semantics
- Adding JSDoc to ts.d files HOT 3
- Have MultiSet.clear() return this HOT 4
- What is the simple way of copying all elements from one Multiset into another? HOT 2
- Typescript definition of `keys` does not work with subsets of string. HOT 1
- MultiSet.toJSON output is surprising HOT 5
- JS array with getter by ID ? HOT 2
- Possible bug Generalized Suffix Array - longestCommonSubsequence HOT 1
- SymSpell.from(words, {maxDistance: 3, verbosity: 2}) taking long time to load large word dictionary HOT 3
- Proposal to Integrate SIEVE Eviction Algorithm HOT 9
- Consolidate named exports tests
- CircularBuffer peekLast undefined HOT 1
- Double check sparse queue set bound testing
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mnemonist.