Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. let slowHeapOperationCount = 0
  2. let fastHeapOperationCount = 0
  3.  
  4. let slowHeapExchanged = []
  5. let fastHeapExchanged = []
  6.  
  7. function SlowHeap(cmp = (a, b) => a > b ? 1 : a < b ? -1 : 0) {
  8. this.cmp = cmp
  9. this._arr = [undefined]
  10. this.size = 0
  11. }
  12. function FastHeap(cmp = (a, b) => a > b ? 1 : a < b ? -1 : 0) {
  13. this.cmp = cmp
  14. this._arr = []
  15. this.size = 0
  16. }
  17.  
  18. SlowHeap.prototype.bubbleUp = function (cmp, _arr, val) {
  19. let idxNr = this.size, parentIdxNr = idxNr >> 1
  20. while (idxNr > 0 && cmp(_arr[parentIdxNr], val) > 0) {
  21. slowHeapExchanged.push([_arr[parentIdxNr], val])
  22.  
  23. _arr[idxNr] = _arr[parentIdxNr]
  24. _arr[parentIdxNr] = val
  25.  
  26. idxNr = parentIdxNr
  27. parentIdxNr = idxNr >> 1
  28.  
  29. slowHeapOperationCount++
  30. }
  31. }
  32. FastHeap.prototype.bubbleUp = function (cmp, _arr, val) {
  33. var idxNr = this.size,
  34. parentIdxNr = ((idxNr - 1) / 2) | 0;
  35.  
  36. while (idxNr > 0 && cmp(_arr[parentIdxNr], val) > 0) {
  37. fastHeapExchanged.push([_arr[parentIdxNr], val])
  38.  
  39. _arr[idxNr] = _arr[parentIdxNr];
  40. _arr[parentIdxNr] = val;
  41.  
  42. idxNr = parentIdxNr;
  43. parentIdxNr = ((idxNr - 1) / 2) | 0;
  44.  
  45. fastHeapOperationCount++
  46. }
  47. }
  48.  
  49. SlowHeap.prototype.push = function (val) {
  50. ++this.size
  51. this._arr.push(val)
  52. this.bubbleUp(this.cmp, this._arr, val)
  53. return this.size
  54. }
  55. FastHeap.prototype.push = function (val) {
  56. this._arr.push(val);
  57. this.bubbleUp(this.cmp, this._arr, val);
  58. return ++this.size;
  59. }
  60.  
  61. const itemCount = 4000000
  62.  
  63. const slowHeap = new SlowHeap()
  64. const fastHeap = new FastHeap()
  65.  
  66. //
  67. // Append the same Number Collection to each Heap:
  68. const numberCollection = []
  69.  
  70. for (let idxNr = 0; idxNr < itemCount; idxNr++) numberCollection.push(Math.random())
  71.  
  72. //
  73. // Benchmark for the Slow Heap:
  74. console.time('SlowHeap')
  75.  
  76. for (let idxNr = 0; idxNr < itemCount; idxNr++) {
  77. slowHeap.push(numberCollection[idxNr])
  78. }
  79.  
  80. console.timeEnd('SlowHeap')
  81.  
  82. //
  83. // Benchmark for the Fast Heap:
  84. console.time('FastHeap')
  85.  
  86. for (let idxNr = 0; idxNr < itemCount; idxNr++) {
  87. fastHeap.push(numberCollection[idxNr])
  88. }
  89.  
  90. console.timeEnd('FastHeap')
  91.  
  92. //
  93. // Verify the "_arr" from the Slow Heap against the Fast Heap:
  94. for (let idxNr = 0; idxNr < itemCount; idxNr++) {
  95. if (slowHeap._arr[idxNr + 1] !== fastHeap._arr[idxNr]) console.log('Unequal value found!')
  96. }
  97.  
  98. if (slowHeapExchanged.length !== fastHeapExchanged.length) console.log('Help! Comp. is not equal.')
  99.  
  100. for (let idxNr = 0, maxNr = slowHeapExchanged.length; idxNr < maxNr; idxNr++) {
  101. if (slowHeapExchanged[idxNr][0] !== fastHeapExchanged[idxNr][0] || slowHeapExchanged[idxNr][1] !== fastHeapExchanged[idxNr][1]) {
  102. console.log('Help!')
  103. }
  104. }
  105.  
  106. console.log(slowHeapOperationCount)
  107. console.log(fastHeapOperationCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement