H246802 / 30-days-challenge

30天每日打卡
4 stars 0 forks source link

day-11-高阶函数传参处理 #11

Open H246802 opened 5 years ago

H246802 commented 5 years ago

写一个函数 execTimes,用于测试一个函数执行一定次数的时长 如:execTimes(sort, 1000)('hello') 表示执行sort函数1000次,sort的参数是'hello'

function execTimes() {
  // your code
}
function sort(str) {
  return str.split('').sort().join('')
}
// 执行sort1000次,sort的参数是'hello'
execTimes(sort, 1000)('hello')
// 输出:‘执行1000次,耗时43ms’
H246802 commented 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’