为什么执行相同的javascript代码所花费的时间会有所不同

时间:2016-06-23 09:01:40

标签: javascript google-chrome underscore.js

每次我不知道为什么

时,请参阅控制台

我正在尝试执行一个函数并获取它所花费的时间,我已经使用了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");
	}
&#13;
.container{
		width:calc(100% - 200px);
		text-align:center;
		margin:0 auto;
		background:#f4f4f4;
	}
&#13;
<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'>
  
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

执行代码所需的时间因CPU执行的其他工作而异。由于计算机上运行的其他进程,中断不断发生。您的Web应用程序只是需要一段CPU时间的众多应用程序之一。还有一些因素,比如CPU为经常请求的数据执行的缓存,以及操作系统缓存。