如何优化下面显示的令人难以置信的慢速代码来加速?我认为,JS会将int转换为浮点数,并且反过来一直反复。我怎么能发现这样的表演杀手呢?我认为,还有很多空间需要优化:-)。我认为,有一种更好的方法来处理半字节值。
起初,我试图放弃& 15操作以获得更高的半字节。这只是减速而不是加速。
(代码不完整。它只是一个较大的部分。它为createBufferSource()填充缓冲区。应该在点击事件后尽快处理。当前,在移动设备上,平均音频文件最多需要2秒。)
for (var len = adpcm.length, i = 0; i < len; i++) {
n = [
adpcm.charCodeAt(i) & 15,
(adpcm.charCodeAt(i) >> 4) & 15
];
for (var j = 0; j < 2; j++) {
s = n[j];
stp = imaStepTable[index];
diff = stp >> 3;
if (s & 4) diff += stp;
if (s & 2) diff += (stp >> 1);
if (s & 1) diff += (stp >> 2);
if (s & 8) diff = 0 - diff;
pred += diff;
if (pred > 32760) pred = 32760;
else if (pred < -32760) pred = -32760; //limiter
index += imaIndexTable[s];
if (index < 0) index = 0;
else if (index > 88) index = 88;
for (var k = 0; k < sampleScale; k++) { //resampler
if (!k) {
lPcm[c] = pred / 32768;
rPcm[c] = (c - shift) < 0 ? 0 : lPcm[(c - shift)];
} else {
lPcm[c] = lPcm[c - k];
rPcm[c] = rPcm[c - k];
}
c++;
}
}
}