Most recent activity
-
For the octal part, 019 gets me an non-octal SyntaxError in Firefox, however, Espruino doesn't handle exceptions at the moment, so I guess either it should stay as-is or output NaN.
As for the 2 constants, I guess this is a normal behaviour in the case of how floats are implemented on hardware. It can not be more precise than the hardware itself.
-
And here is my final code for my needs, this won't stack overflow:
nqsort: 425.1626749999999788087 (0.343325)
// Non-nested quick sort Array.prototype.nqsort = function(depth) { var pivot, i=0, left, right; if (depth === undefined) { depth = parseInt(Math.floor(this.length/5), 10); } var begin = Uint16Array(depth); var end = Uint16Array(depth); begin[0] = 0; end[0] = this.length; while (i >= 0) { left = begin[i]; right = end[i]-1; if (left < right) { pivot = this[left]; if (i === depth-1) return false; while (left < right) { while (this[right] >= pivot && left < right) right--; if (left < right) this[left++] = this[right]; while (this[left] <= pivot && left < right) left++; if (left < right) this[right--] = this[left]; } this[left] = pivot; begin[i+1] = left+1; end[i+1] = end[i]; end[i++] = left; } else i--; } return true; }; Uint16Array.prototype.nqsort = Array.prototype.nqsort;
References: http://alienryderflex.com/quicksort/
-
I tried optimizing the function, removing functions call seem to help quite a bit, and prevent stack overflows just a bit since it is easy to get one.
qsort: 165526.5649749999865889 (0.398799999995389953255)
swap and qsort_partition not needed anymore:
// Quick sort Array.prototype.qsort = function (left, right) { var pivot, newPivot, pivotVal, tmp; if (left === undefined) { left = 0; right = this.length - 1; } if (left < right) { pivot = left + Math.ceil((right - left) / 2); // Partition pivotVal = this[pivot]; newPivot = left; this[pivot] = this[right]; this[right] = pivotVal; for (var i = left; i < right; i++) { if (this[i] < pivotVal) { tmp = this[i]; this[newPivot++] = i; this[i] = tmp; } } this.qsort(left, newPivot - 1); this.qsort(newPivot + 1, right); } }; Uint16Array.prototype.qsort = Array.prototype.qsort;
-
Attaching did not work, I'll just paste it inline:
// Useful functions Array.prototype.median = function () { return this[Math.floor(this.length/2)]; }; Uint16Array.prototype.median = Array.prototype.median; Array.prototype.swap = function (left, right) { var tmp = this[left]; this[left] = this[right]; this[right] = tmp; return this; }; Uint16Array.prototype.swap = Array.prototype.swap; Uint16Array.prototype.clear = function () { for (var i = 0; i < this.length; i++) this[i] = 0; }; // Bubble sort Array.prototype.sort = function () { var len = this.length - 1; for (var i = 0; i < len; i++) { for (var j = 0, swapping, endIndex = len - i; j < endIndex; j++) { if (this[j] > this[j + 1]) { swapping = this[j]; this[j] = this[j + 1]; this[j + 1] = swapping; } } } return this; }; Uint16Array.prototype.sort = Array.prototype.sort; // Quick sort Array.prototype.qsort_partition = function (pivot, left, right) { var tmp, storeIndex = left, pivotVal = this[pivot]; this.swap(pivot, right); for (var i = left; i < right; i++) { if (this[i] < pivotVal) { this.swap(i, storeIndex); storeIndex++; } } this.swap(right, storeIndex); return storeIndex; }; Uint16Array.prototype.qsort_partition = Array.prototype.qsort_partition; Array.prototype.qsort = function (left, right) { var pivot, newPivot; if (left === undefined) { left = 0; right = this.length - 1; } if (left < right) { pivot = left + Math.ceil((right - left) / 2); newPivot = this.qsort_partition(pivot, left, right); this.qsort(left, newPivot - 1); this.qsort(newPivot + 1, right); } return this; }; Uint16Array.prototype.qsort = Array.prototype.qsort; // Tests var tQsort = new Uint16Array([5454,5449,5380,5412,5380,5366,5344,5395,5398,5424,5422,5473,5420,5432,5376,5354,5561,5288,5393,5388,5422,5427,5476,5407,5385,5180,5363,5324,5395,5393,5410,5405,5349,5361,5385,5412,5373,5373,5478,5420,5446,5395,5339,5407,5420,5356,5336,5427,5459,5378,5336,5349,5420,5405,5434,5383,5446,5422,5349,5329,5405,5434,5446,5336,5427,5473,5402,5170,5388,5412,5456,123,456,789]); var tBsort = new Uint16Array([5454,5449,5380,5412,5380,5366,5344,5395,5398,5424,5422,5473,5420,5432,5376,5354,5561,5288,5393,5388,5422,5427,5476,5407,5385,5180,5363,5324,5395,5393,5410,5405,5349,5361,5385,5412,5373,5373,5478,5420,5446,5395,5339,5407,5420,5356,5336,5427,5459,5378,5336,5349,5420,5405,5434,5383,5446,5422,5349,5329,5405,5434,5446,5336,5427,5473,5402,5170,5388,5412,5456,123,456,789]); var qsortTime, bsortTime, startTime = getTime(); tQsort.qsort(); qsortTime = getTime(); tBsort.sort(); bsortTime = getTime(); print("start: " + startTime); print("qsort: " + qsortTime + " (" + (qsortTime-startTime) + ")"); print("bsort: " + bsortTime + " (" + (bsortTime-qsortTime) + ")");
-
-
Would it be possible for a function to have a point where it could "yield" the CPU for other functions to execute? I added a quick sort function to Uint16Array which takes about 15 seconds to execute which breaks some timings when setInterval() are too short.
I take the "yield" expression from Lua which implements multi-threading as coroutines, see: http://lua-users.org/wiki/CoroutinesTutorial
-
Thinking about this, is there a way to control when the Bluetooth radio is on? Or is there any Bluetooth LE module available? This is one of the rare thing that my RFduino has better than Espruino at the moment! Those RFduino are very very low power; maximum current is 18mA while transmitting on BLE with ultra low power modes down to 4uA. Not the same speed, features and size but its integrated BLE radio really rocks.
I'm trying to set-up an input_pullup and each time I do an analogRead on it, the pinMode resets from "input_pullup" to standard "input", I'm not sure this has always been to correct behaviour. I'm using the original Espruino board on latest version 1v75.