Home >Web Front-end >JS Tutorial >js view the execution time of a function example code_javascript skills

js view the execution time of a function example code_javascript skills

PHP中文网
PHP中文网Original
2016-05-16 15:39:401450browse

js View the execution time of a function example code_javascript skills

Not much to say in detail, please see the code example explanation below

There is a summation as follows function, we need to know the time it takes to execute this function


function add(){

 var sum = 0 ;

 for(var i = 0;i<1000000;i++){

  sum += i;

 }

return sum;

}


Define a test function and pass the function under test as a parameter

function test(func){

 var start = new Date().getTime();//起始时间

 func();//执行待测函数

 var end = new Date().getTime();//接受时间

 return (end - start)+"ms";//返回函数执行需要时间

}

Test and view the actual execution time

var time = test(add);

console.log(time);

js view the execution time of a function example code_javascript skills

The above is the js view of the execution time of a function example code_javascript skills. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn