比较php字符串连接的效率

php字符串连接有三种方式

1)使用 . 链接

2)使用 .= 连接

3)implode 函数连接数组元素

/*以下测试在ci框架进行*/
    private function get_mcrotime()
    {
            list($mic,$sec) = explode(" ",microtime());
            return ((float)$mic + (float)$sec);
    }

        public function test0(){
        //0.49530792236328
        //0.50851202011108
        //0.50111794471741

        $start = $this->get_mcrotime();
        define("num",100000);
        $str1 = 'ha';
        $str2 = '';
        for($i=0;$i<num;$i++){
            $str2 = $str2 . $str1;
        }
        echo $this->get_mcrotime()-$start;
    }
        
        public function test1(){
        //0.0046639442443848
        //0.0040309429168701
        $start = $this->get_mcrotime();
        define("num",100000);
        $str1 = 'ha';
        $str2 = '';
        for($i=0;$i<num;$i++){
            $str2 .= $str1;
        }
        echo $this->get_mcrotime()-$start;
    }

        public function test2(){
        //0.010957956314087
        //0.012393951416016
        $start = $this->get_mcrotime();
        define("num",100000);
        $str1 = 'ha';
        $str2 = '';
        $var = array();
        for($i=0;$i<num;$i++){
            array_push($var,$str1);
        }
        $str2 = implode($var);
        
        echo $this->get_mcrotime()-$start;
    }

 

从上述结果看出,使用 .= 链接字符串最为有效率, 而使用 . 链接最为耗时。

转载于:https://www.cnblogs.com/hejun695/p/5387495.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值