-
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:
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?
- Jussi
- Jussi
Thanks a lot, that was fast! With the cutting edge build a Float32Array sorts correctly about six times faster than with a comparison function. And just as fast as an Int16Array, so I can use full accelerometer precision.
I'd completely forgotten about the inline C option, I'll try that next if I still need more speed.