Open H246802 opened 5 years ago
function execTimes(call,times) {
// your code
return function(){
console.time()
let begin = Date.now()
for(let i = 0 ; i < times ; i++){
call(arguments[0] ? arguments[0] : '')
}
let end = Date.now()
console.log('执行' + times + '次,耗时' + (end - begin) + 'ms')
console.timeEnd()
}
}
function sort(str) {
return str.split('').sort().join('')
}
// 执行sort1000次,sort的参数是'hello'
execTimes(sort, 1000)('hello')
// 输出:‘执行1000次,耗时43ms’
写一个函数
execTimes
,用于测试一个函数执行一定次数的时长 如:execTimes(sort, 1000)('hello')
表示执行sort函数1000次,sort的参数是'hello'