让我们考虑以下代码段作为示例:
var len = 1000000,
testArr = []
for (var i = 0; i < len; i++) {
testArr.push(i+1)
}
function mprofile(name, subject, object) {
var start = new Date().getTime(),
result = subject(object),
end = new Date().getTime() - start
console.log(name)
console.log('Result: ' + result)
console.log(end)
}
var length = testArr.length,
start = new Date().getTime(),
cnt = 0
for (i = 0; i < length; i++) {
cnt += testArr[i]
}
console.log('Regular loop:')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
start = new Date().getTime()
cnt = i = 0
for (i = length; i--; ) {
cnt += testArr[i]
}
console.log('Reversered loop')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
start = new Date().getTime()
cnt = i = 0
var startAt = length%8,
iterations = Math.floor((length+7) / 8)
do {
switch (startAt) {
case 0: cnt += testArr[i++]
case 7: cnt += testArr[i++]
case 6: cnt += testArr[i++]
case 5: cnt += testArr[i++]
case 4: cnt += testArr[i++]
case 3: cnt += testArr[i++]
case 2: cnt += testArr[i++]
case 1: cnt += testArr[i++]
}
startAt = 0
} while(--iterations)
console.log('Duffs device')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
start = new Date().getTime()
cnt = i = 0
iterations = Math.floor((length+7) / 8)
switch (length % 8) {
case 0: cnt += testArr[i++]
case 7: cnt += testArr[i++]
case 6: cnt += testArr[i++]
case 5: cnt += testArr[i++]
case 4: cnt += testArr[i++]
case 3: cnt += testArr[i++]
case 2: cnt += testArr[i++]
case 1: cnt += testArr[i++]
}
while(--iterations) {
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
}
console.log('Optimized Duffs device')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
mprofile(
'Profiled regular loop',
function(arr) {
var cnt = 0,
length = arr.length
for (i = 0; i < length; i++) {
cnt += testArr[i]
}
return cnt
},
testArr
)
mprofile(
'Profiled reversed loop',
function(arr) {
var cnt = 0,
length = arr.length
for (i = length; i--; ) {
cnt += testArr[i]
}
return cnt
},
testArr
)
mprofile(
'Profiled Duffs device',
function(arr) {
var cnt = i = 0,
length = arr.length,
startAt = length%8,
iterations = Math.floor((length+7) / 8)
do {
switch (startAt) {
case 0: cnt += arr[i++]
case 7: cnt += arr[i++]
case 6: cnt += arr[i++]
case 5: cnt += arr[i++]
case 4: cnt += arr[i++]
case 3: cnt+ = arr[i++]
case 2: cnt += arr[i++]
case 1: cnt += arr[i++]
}
startAt = 0
} while(--iterations)
return cnt
},
testArr
)
mprofile(
'Profiled optimized Duffs device',
function(arr) {
var cnt = i = 0,
length = arr.length,
iterations = Math.floor((length+7) / 8)
switch (length % 8) {
case 0: cnt += arr[i++]
case 7: cnt += arr[i++]
case 6: cnt += arr[i++]
case 5: cnt += arr[i++]
case 4: cnt += arr[i++]
case 3: cnt += arr[i++]
case 2: cnt += arr[i++]
case 1: cnt += arr[i++]
}
while(--iterations) {
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
}
return cnt
},
testArr
)
报告的执行时间与普通循环和回调内执行的循环之间存在差异.另外,如果你在头部的脚本标签内部运行它而在执行时控制台中执行它,则执行时间会有所不同,如下图所示:
脚本标记的结果:
控制台(Firefox)的结果:
任何人都可以解释为什么会发生这种情况或提供任何资源的链接,我可以找到与此相关的任何信息.如果您将链接的响应或文档中涵盖浏览器差异,也将不胜感激.
感谢您的时间和帮助.
performance.now()
是衡量绩效的最佳选择.
https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
结束时间计算错误.在你的mprofile
函数中,你在调用第一个之前计算差异console.log
,在普通代码中 - 在第二个之后.因此,在一种情况下,您将控制台与控制台的交互包含在您的测量中,而不是
另外,整个例子有点不正确.你应该创建一个像loop
,reverseLoop
等等的函数,并在调用它们之前/之后测量时间.那么你应该测量回调中相同功能的时间.你应该每个案例运行至少10次并检查平均时间.