function getStr($leng,$num){
$j=0;
while($j<$num){
$str = '';
for($i=0;$i<$leng;$i++){
$Int = rand(0,99);
$str.=$Int.",";
}
echo $j.".";
echo "[".substr($str, 0, strlen($str)-1)."]";
$j++;
}
return $str;
}
echo getStr(6,1000000);以上函数生成1000000个[17,0,21,33,8,17]数据格式存入txt ,数值是1-99的随机数 居然要25秒,有高手帮忙优化一下,越短越好
http://topic.csdn.net/u/20111220/16/6be5544e-54bf-4676-893c-ff9ebda3c67f.html附测试代码for($i=0;$i<10;$i++){
echo fun_rand(1, 10000), "\n";/////这里输出每次都一样,hohoho~~~~~~
}function fun_rand($min, $max){
$res = rand($min, $max);
for($i=0;$i<32767;$i++) rand($min, $max);
return $res;
}
$f=fopen('file.txt','a');
for($i=0,$str='',$arr=range(1,99);$i<$num/1000;$i++){
for($j=0;$j<1000;$j++){
$temp=array_rand($arr,$leng);
$str.='['.join(',',$temp)."]\r\n";
}
fwrite($f,$str);
$str='';
}
fclose($f);
}
getStr(6,1000000);
$t = array();
$j=0;
$r = '';
while($j++ < $num){
if(count($t) == 0) {
$t = range(1, 99);
shuffle($t);
$t = array_chunk($t, $leng);
array_pop($t);
}
$r .= '[' . join(',', array_pop($t)) .']';
}
return $r;
}
$m = microtime(1);
getStr(6,1000000);
echo number_format((microtime(1) - $m) * 1000), '毫秒';
3,876毫秒