在ios上 Math函数实现貌似有些问题,我把min max函数替换成三元后快了一倍, 但还是比Android慢。之前在使用Math.round的时候也出现了问题,具体场景记不清了。希望官方大佬给关注一下这个Math的问题。
IOS上纯运算效率低下,同计算量竟然比android慢20倍业务需要在canvas上实现模糊效果,在模糊计算时发现IOS上对计算速度要慢很多倍。 提出代码片段进行测试发现,在iphone X上计算耗时(纯运算过程)22822ms, 小米 MIX 2上耗时928ms, 模拟器上运算耗时384ms。在其他ios 和android机型测试都是相同情况。两者耗时竟然相差20多倍。 iphoneX[图片] Mix 2 [图片] 模糊运算代码 [代码]function[代码] [代码]blurUint8Array(uint8Arr, width, height, blur) {[代码][代码] [代码][代码]var[代码] [代码]result = [代码][代码]new[代码] [代码]Uint8ClampedArray(width * height * 4);[代码][代码] [代码][代码]var[代码] [代码]source = uint8Arr.slice()[代码][代码] [代码][代码]var[代码] [代码]bxs = boxesForGauss(blur, 3)[代码][代码] [代码][代码]boxBlurUint8(source, result, width, height, (bxs[0] - 1) / 2)[代码][代码] [代码][代码]boxBlurUint8(result, source, width, height, (bxs[1] - 1) / 2)[代码][代码] [代码][代码]boxBlurUint8(source, result, width, height, (bxs[2] - 1) / 2)[代码][代码] [代码][代码]return[代码] [代码]result[代码][代码]}[代码] [代码]function[代码] [代码]boxBlurUint8(scl, tcl, w, h, r) {[代码][代码] [代码][代码]for[代码] [代码]([代码][代码]var[代码] [代码]i = 0; i < h; i++) {[代码][代码] [代码][代码]for[代码] [代码]([代码][代码]var[代码] [代码]j = 0; j < w; j++) {[代码][代码] [代码][代码]var[代码] [代码]R = 0[代码][代码] [代码][代码]var[代码] [代码]G = 0[代码][代码] [代码][代码]var[代码] [代码]B = 0[代码][代码] [代码][代码]var[代码] [代码]A = 0[代码][代码] [代码][代码]for[代码] [代码]([代码][代码]var[代码] [代码]iy = i - r; iy < i + r + 1; iy++) {[代码][代码] [代码][代码]for[代码] [代码]([代码][代码]var[代码] [代码]ix = j - r; ix < j + r + 1; ix++) {[代码][代码] [代码][代码]var[代码] [代码]x = Math.min(w - 1, Math.max(0, ix))[代码][代码] [代码][代码]var[代码] [代码]y = Math.min(h - 1, Math.max(0, iy))[代码][代码] [代码][代码]var[代码] [代码]index = (y * w + x) * 4[代码][代码] [代码][代码]R += scl[index][代码][代码] [代码][代码]G += scl[index + 1][代码][代码] [代码][代码]B += scl[index + 2][代码][代码] [代码][代码]A += scl[index + 3][代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]var[代码] [代码]count = (r + r + 1) * (r + r + 1)[代码][代码] [代码][代码]var[代码] [代码]tIndex = (i * w + j) * 4[代码][代码] [代码][代码]tcl[tIndex] = R / count[代码][代码] [代码][代码]tcl[tIndex + 1] = G / count[代码][代码] [代码][代码]tcl[tIndex + 2] = B / count[代码][代码] [代码][代码]tcl[tIndex + 3] = A / count[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码]}[代码][代码]function[代码] [代码]boxesForGauss(sigma, n) {[代码][代码] [代码][代码]var[代码] [代码]wIdeal = Math.sqrt((12 * sigma * sigma / n) + 1);[代码][代码] [代码][代码]var[代码] [代码]wl = Math.floor(wIdeal);[代码][代码] [代码][代码]if[代码] [代码](wl % 2 === 0) wl--;[代码][代码] [代码][代码]var[代码] [代码]wu = wl + 2;[代码] [代码] [代码][代码]var[代码] [代码]mIdeal = (12 * sigma * sigma - n * wl * wl - 4 * n * wl - 3 * n) / (-4 * wl - 4);[代码][代码] [代码][代码]var[代码] [代码]m = Math.round(mIdeal);[代码] [代码] [代码][代码]var[代码] [代码]sizes = [];[代码][代码] [代码][代码]for[代码] [代码]([代码][代码]var[代码] [代码]i = 0; i < n; i++) sizes.push(i < m ? wl : wu);[代码][代码] [代码][代码]return[代码] [代码]sizes;[代码][代码]}[代码]
2018-08-16