Home > Web Front-end > JS Tutorial > body text

js view the execution time of a function example code_javascript skills

PHP中文网
Release: 2016-05-16 15:39:40
Original
1392 people have browsed it

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;

}
Copy after login


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";//返回函数执行需要时间

}
Copy after login

Test and view the actual execution time

var time = test(add);

console.log(time);
Copy after login

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)!


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template