简体   繁体   English

为什么执行相同的JavaScript代码所需的时间会有所不同

[英]why the time taken to execute same javascript code varies

See the Console the time varies each time I dont know why 查看控制台,每次我都不知道为什么时间会有所不同

I am trying to execute a function and get the time taken by it, I have used console.time(), but each time when I run it shows different time One more thing, Is time taken by underscore is more than normal execution time. 我试图执行一个函数并获取它所花费的时间,我使用了console.time(),但是每次运行它时,它都会显示不同的时间。还有一点,下划线所花费的时间是否比正常执行时间还要长。

 function map(){ console.time('mycode'); var arr = [234345345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,2345]; var result = []; var constant = 3; var length = arr.length; //will avoid calculation of length each time for(i=0;i<length;i++){ temp = arr[i] * constant; result.push(temp); } console.timeEnd('mycode'); console.time('mycode1'); var underscore = _.map([arr], function(num){ return num * 3; }); console.timeEnd('mycode1'); } function where(){ console.time("myCode"); var arr = [{title: "Cymbeline", author: "Shakespeare", year: 1611}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615}, {title: "The Tempest", author: "Shakespeare", year: 1613}, {title: "The Tempest", author: "Shakespeare", year: 1615} ]; var author = 'Shakespeare'; var year = '1611'; var result = []; var length = arr.length; //will avoid calculation of length each time for(i=0;i<length;i++){ //you know that and takes more time than or statement if(arr[i]['author'] == author && arr[i]['year'] == year){ result.push(arr[i]); } } console.log(result) console.timeEnd("myCode"); console.time("myCode1"); var underscore = _.filter(arr, {author: "Shakespeare", year: 1611}); console.log(underscore) console.timeEnd("myCode1"); } 
 .container{ width:calc(100% - 200px); text-align:center; margin:0 auto; background:#f4f4f4; } 
 <script src="http://underscorejs.org/underscore-min.js"></script> <h1>Evaluating time </h1> <input type='button' onclick='map()' value='Execute'> <input type='button' onclick='where()' value='Execute'> 

The time taken to execute code will vary due to other work that is being performed by your CPU. 执行代码所需的时间将因CPU正在执行的其他工作而异。 Interrupts are constantly occurring because of other processes running on the computer. 由于计算机上正在运行其他进程,因此不断发生中断。 Your web application is just one of the many applications requiring a slice of CPU time. 您的Web应用程序只是许多需要占用CPU时间的应用程序之一。 There are also factors like caching that gets performed by the CPU for frequently requested data, as well as OS caches. 还有一些因素,例如CPU对频繁请求的数据以及OS缓存进行缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM