看到控制檯的時刻變化的每個我不一次知道爲什麼爲什麼執行相同的javascript代碼所花費的時間變化
我試圖執行一個功能並獲得通過它拍攝的時候,我已經使用控制檯.time(),但每當我運行它時顯示不同的時間 還有一件事,下劃線花費的時間比正常的執行時間多。
function map(){
\t \t
\t \t console.time('mycode');
\t \t
\t \t 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];
\t \t var result = [];
\t \t var constant = 3;
\t \t var length = arr.length; //will avoid calculation of length each time
\t \t for(i=0;i<length;i++){
\t \t \t temp = arr[i] * constant;
\t \t \t result.push(temp);
\t \t }
\t \t
\t \t console.timeEnd('mycode');
\t \t
\t \t console.time('mycode1');
\t \t
\t \t var underscore = _.map([arr], function(num){ return num * 3; });
\t \t
\t \t console.timeEnd('mycode1');
\t }
\t
\t function where(){
\t \t
\t \t console.time("myCode");
\t \t var arr = [{title: "Cymbeline", author: "Shakespeare", year: 1611},
\t \t \t
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1613},
\t \t \t {title: "The Tempest", author: "Shakespeare", year: 1615}
\t \t ];
\t \t var author = 'Shakespeare';
\t \t var year = '1611';
\t \t var result = [];
\t \t var length = arr.length; //will avoid calculation of length each time
\t \t for(i=0;i<length;i++){ //you know that and takes more time than or statement
\t \t \t if(arr[i]['author'] == author && arr[i]['year'] == year){
\t \t \t \t result.push(arr[i]);
\t \t \t }
\t \t }
\t \t console.log(result)
\t \t console.timeEnd("myCode");
\t \t
\t \t console.time("myCode1");
\t \t var underscore = _.filter(arr, {author: "Shakespeare", year: 1611});
\t \t
\t \t console.log(underscore)
\t \t console.timeEnd("myCode1");
\t }
.container{
\t \t width:calc(100% - 200px);
\t \t text-align:center;
\t \t margin:0 auto;
\t \t background:#f4f4f4;
\t }
<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'>
爲什麼你有一個期望,它會保持不變?電腦不穩定,時間總是會有所不同,至少有一點點。 –
如果它變得至少有點它的好,但有時它會給出1.565ms,有時.056毫秒..這不是一個微小的差異正確 –