您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP有没有必要实现stringbuilder解决思路

2025/12/4 17:39:20发布28次查看
php有没有必要实现stringbuilder
本人刚刚毕业,也是从c#转来,在学习php的过程中发现php没有类似c#一样的stringbuilder类型。
因为我知道在c#中多次操作字符串用stringbuilder的效率要高过string,那么想问下php为什么没有实现stringbuilder?
还有,如果我自己用数组模拟stringbuilder的话会提高效率么?
------解决方案--------------------
字符串比數組模拟快多了。
字符串
$starttime = getmicrotime();
$str = '';
for($i=0;$i<100000;$i++){
$str .= $i;
}
$endtime = getmicrotime();
printf(run time %f ms\r\n, (float)($endtime-$starttime)*1000);
function getmicrotime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}

run time 61.100006 ms
数组
$starttime = getmicrotime();
$arr = array();
for($i=0;$i<100000;$i++){
array_push($arr, $i);
}
$endtime = getmicrotime();
printf(run time %f ms\r\n, (float)($endtime-$starttime)*1000);
function getmicrotime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}

run time 200.176954 ms
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product