I need fast array sorting to determine various quantiles from arrays of accelerometer data. I'm using bangle.js and scaling the values to an Int16Array because it seems to sort() twice as fast than a Float32Array, but I get odd results:
var arr16 = new Int16Array([2,-1,0,-2,1])
=new Int16Array([2, -1, 0, -2, 1])
arr16.sort()
=new Int16Array([-1, -2, 0, 1, 2])
Clearly it should be [-2, -1, 0, 1, 2].
arr16.sort((a,b)=>{return a-b}) gets the order correct, but significantly slower than using sort(). An Int16Array of 250 elements sorts at approximately 0.4s with sort() but takes twice as long with the comparison function.
Or is there some even faster way to sort an array?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi,
I need fast array sorting to determine various quantiles from arrays of accelerometer data. I'm using bangle.js and scaling the values to an Int16Array because it seems to sort() twice as fast than a Float32Array, but I get odd results:
Clearly it should be [-2, -1, 0, 1, 2].
arr16.sort((a,b)=>{return a-b}) gets the order correct, but significantly slower than using sort(). An Int16Array of 250 elements sorts at approximately 0.4s with sort() but takes twice as long with the comparison function.
Or is there some even faster way to sort an array?