-
-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MinHeap.push() vs MaxHeap.push() performance #206
Comments
Hello @RickWong,
Your numbers are well in the range of |
Hello @Yomguithereal. Here's the reverse range 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 And the reverse range 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 |
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 |
Currently MaxHeap is implemented as a MinHeap just with reverse comparator. When pushing the same collection (for example an incremental range between 1 and 65535) though, the comparator is called more frequently for MaxHeap than for MinHeap.
Curiously, when using from() the performance characteristics are flipped:
Why would that be?
The text was updated successfully, but these errors were encountered: