用代码来说明就是将

for (i = 0; i < len; i++) {
    sum += arry[i]
}

 

替换为

for (i = 0; i < len; i += 2) {
    newSum += arry[i] + arry[i + 1]
}

 

循环展开对于算术运算来说,优化的作用是很大的。我分别对整数运算和浮点数运算作了多次测试,得出表格如下:

操作 整数 整数(优化后) 浮点数 浮点数(优化后)
+ 360 163 354 164
- 379 167 341 177
* 350 160 364 163
/ 118 57 152 63

测试环境

  • cpu:i5-7400
  • 浏览器: chrome 70.0.3538.110

运算是用了1千万个数,取值是运行十次测试得出的平均数。附上加法测试的代码

const arry = []
let num = 10000000
while (num) {
    arry.push(num)
    num--
}

let sum = 0
let last = new Date()
let i 
let len = arry.length
for (i = 0; i < len; i++) {
    sum += arry[i]
}
let now = new Date()

console.log(now - last)

let newSum = 0
last = new Date()
for (i = 0; i < len; i += 2) {
    newSum += arry[i] + arry[i + 1]
}
now = new Date()

console.log(now - last)
console.log(sum, newSum)

 

 

相关文章:

  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-15
  • 2022-12-23
  • 2021-05-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2021-06-13
相关资源
相似解决方案