如果是简单的在页面上列出40w个单词这种业务,我觉得还是第一种方法快
理由:
一、查询
方法1.扫描一行就能获取到记录,方法2那就需要扫描很多行了,花费的时间就不言而喻了(表越大越明显)。
这个过程方法1比方法2优越太多了
二、输出
方法1需要独立分割,这对php来说根本不叫事。然后都需要缓存输出。
总的来说,方法1的开销比方法2的开销少。
今天来更新一下,验证以下我的结论
首先给出demo代码,然后给出实际运行结果。
$start=microtime(true);
//构造40w个单词组成的字符串
$str="";
for($i=1;$i<=400000;$i++){
$str.="helleworld{$i},";
}
$str=rtrim($str,',');
//分割并遍历输出
$arr=explode(',',$str);
foreach($arr as $v){
echo "{$v}\n";
}
echo "\n";
echo "总耗时:\n";
echo microtime(true)-$start."微秒\n";
?>
helleworld399988
helleworld399989
helleworld399990
helleworld399991
helleworld399992
helleworld399993
helleworld399994
helleworld399995
helleworld399996
helleworld399997
helleworld399998
helleworld399999
helleworld400000
总耗时:
1.4381220340729微秒
因网络原因我这里实验结果的图片上传不上来。结果是:分割40w单词的大字符串,遍历输出。
总耗时:
1.4381220340729微秒
结论,php分割40w单词的字符串真不叫什么事情。如果要从数据库取出40万条数据,那开销我就呵呵了。
换成分割100万的大字符串试试:
helleworld999988
helleworld999989
helleworld999990
helleworld999991
helleworld999992
helleworld999993
helleworld999994
helleworld999995
helleworld999996
helleworld999997
helleworld999998
helleworld999999
helleworld1000000
总耗时:
3.5557680130005微秒