主页 > 前端 > javascript >
来源:未知 时间:2015-07-22 16:51 作者:xxadmin 阅读:次
[导读] 本文讲解:JavaScript对数组进行随机排序 scriptvarcount=100000,arr=[];for(vari=0;icount;i++){arr.push(i);}//常规方法,sort()vart=newDate().getTime();Array.prototype.sort.call(arr,function(a,b){returnMath.random().5?-1:1;});...
|
本文讲解:JavaScript对数组进行随机排序 <script>
var count = 100000,arr = [];
for(var i=0;i<count;i++){
arr.push(i);
}
//常规方法,sort()
var t = new Date().getTime();
Array.prototype.sort.call(arr,function(a,b){ return Math.random()>.5 ? -1 : 1;});
document.write(arr+'<br/>');
var t1 = new Date().getTime();
document.write(t1-t);
//以下方法效率最高
if (!Array.prototype.shuffle) {
Array.prototype.shuffle = function() {
for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
return this;
};
}
var t = new Date().getTime();
arr.shuffle();
document.write('<br/>'+arr+'<br/>');
var t1 = new Date().getTime();
document.write(t1-t);
</script> |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com