简体   繁体   中英

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.

 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. 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. There are also factors like caching that gets performed by the CPU for frequently requested data, as well as OS caches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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