Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
queue impl
(version: 0)
Comparing performance of:
simple vs double array
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function createSingleListQueue() { return { queue: [], enqueue(element) { this.queue.push(element) }, dequeue() { return this.queue.shift() }, isEmpty() { return this.queue.length === 0 } } } var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var Queue = /** @class */ (function () { function Queue() { this._length = 0; this.st1 = []; this.st2 = []; } Object.defineProperty(Queue.prototype, "length", { get: function () { return this._length; }, set: function (len) { if (len === 0) { this.st1.length = 0; this.st2.length = 0; } }, enumerable: false, configurable: true }); Queue.prototype[Symbol.iterator] = function () { var st2, i, _i, _a, k; return __generator(this, function (_b) { switch (_b.label) { case 0: st2 = this.st2.slice(); i = st2.length - 1; _b.label = 1; case 1: if (!(i >= 0)) return [3 /*break*/, 4]; return [4 /*yield*/, st2[i]]; case 2: _b.sent(); _b.label = 3; case 3: i--; return [3 /*break*/, 1]; case 4: _i = 0, _a = this.st1; _b.label = 5; case 5: if (!(_i < _a.length)) return [3 /*break*/, 8]; k = _a[_i]; return [4 /*yield*/, k]; case 6: _b.sent(); _b.label = 7; case 7: _i++; return [3 /*break*/, 5]; case 8: return [2 /*return*/]; } }); }; Queue.prototype.entries = function () { var _a; var _this = this; var i = 1; var j = 0; var k = 0; return _a = {}, _a[Symbol.iterator] = function () { return this; }, _a.next = function () { var _ = []; for (var _i = 0; _i < arguments.length; _i++) { _[_i] = arguments[_i]; } if (_this.st2.length >= i) { return { value: [k++, _this.st2[_this.st2.length - i++]], done: false, }; } if (_this.st2.length <= i && !_this.st1.length) { return { value: undefined, done: true }; } return _this.st1.length > j ? { value: [k++, _this.st1[j++]], done: false } : { value: undefined, done: true }; }, _a; }; Queue.prototype.peek = function () { return this.st2.length ? this.st2[this.st2.length - 1] : this.st1[0]; }; Queue.prototype.pop = function () { if (this._length > 0) { this._length--; } if (this.st2.length) { return this.st2.pop(); } while (this.st1.length) { this.st2.push(this.st1.pop()); } return this.st2.pop(); }; Queue.prototype.push = function (v) { this.st1.push(v); this._length++; }; Queue.prototype.toArray = function () { var ret = []; for (var i = this.st2.length - 1; i >= 0; i--) { ret.push(this.st2[i]); } return ret.concat(this.st1); }; return Queue; }()); var singleQ = createSingleListQueue() var doubleQ = new Queue() var queueSize = 100000 for(let i = 0; i < queueSize; i++) { singleQ.enqueue('testString' + i) doubleQ.push('testString' + i) }
Tests:
simple
for(let i = 0; i < 100000; i++) { singleQ.dequeue() } for(let i = 0; i < 10000; i++) { singleQ.enqueue("t"+i); singleQ.enqueue("t"+i); singleQ.enqueue("t"+i); singleQ.dequeue() singleQ.dequeue() singleQ.dequeue() }
double array
for(let i = 0; i < 100000; i++) { doubleQ.pop() } for(let i = 0; i < 10000; i++) { doubleQ.push("t"+i); doubleQ.push("t"+i); doubleQ.push("t"+i); doubleQ.pop() doubleQ.pop() doubleQ.pop() }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
simple
double array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the provided code, I'll provide an answer to the problem. The code defines two queue implementations: `singleQ` and `Queue`. The `singleQ` is a single-linked list-based implementation, while the `Queue` is a more complex implementation that uses two stacks (`st1` and `st2`) to manage its elements. The `Queue` also provides additional methods for peeking, popping, pushing, and getting the queue as an array. The individual test cases are designed to benchmark the performance of these queue implementations under different scenarios: 1. **Simple**: This test case involves enqueuing and dequeuing elements 100,000 times, with a specific pattern of enqueuing and dequeuing multiple elements in between. 2. **Double Array**: This test case is similar to the previous one, but it pushes two additional elements onto the queue before each dequeue operation. To answer this question, I'll provide an analysis of the code's performance based on the provided benchmark results: **Single-Linked List-Based Implementation (singleQ)** * The `simple` test case shows that the single-linked list-based implementation is performing well, with an average of 99.43 executions per second. * The `double array` test case shows that the implementation is also performing reasonably well, but not as consistently as in the `simple` test case. **Two-Stack-Based Implementation (Queue)** * The `singleQ` test case shows that the two-stack-based implementation is significantly slower than the single-linked list-based implementation, with an average of 93.41 executions per second. * The `double array` test case shows similar performance to the `simple` test case for the single-linked list-based implementation, but still outperforms the two-stack-based implementation. Overall, based on the provided benchmark results, it appears that the single-linked list-based implementation (singleQ) is generally more efficient and performs better than the two-stack-based implementation (Queue), especially in scenarios with multiple enqueue and dequeue operations.
Related benchmarks:
single v double array queue
Fast queue like
Queue comparison
Queue comparison4
Comments
Confirm delete:
Do you really want to delete benchmark?