PHP字符串连接-"$ a $ b";与$ a. " " . $ b-效果 [英] PHP String concatenation - "$a $b" vs $a . " " . $b - performance

查看:80
本文介绍了PHP字符串连接-"$ a $ b";与$ a. " " . $ b-效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如说,之间有速度差异吗?

Is there a speed difference between, say:

$ newstring ="$ a和$ b出去看$ c";

$newstring = "$a and $b went out to see $c";

$ newstring = $ a. "和 " . $ b. 出去看". $ c;

$newstring = $a . " and " . $b . " went out to see " . $c;

如果是,为什么?

推荐答案

取决于PHP版本,如果您将其编写为以下形式,则它的变化幅度以秒为单位更快: $newstring = $a . ' and ' . $b . ' went out to see ' . $c;

Depending on the PHP version, it varies by how much the second is faster if you write it like: $newstring = $a . ' and ' . $b . ' went out to see ' . $c;

PHP在版本之间非常不一致,并且在构建和构建性能时,您必须自己对其进行测试. 需要说的是,它还取决于$a$b$c的类型,如下所示.

PHP is very inconsistent from version to version and build to build when it comes to performance, you have to test it for yourself. What nees to be said is that it also depends on the type of $a, $b and $c, as you can see below.

使用"时,PHP会分析字符串以查看其中是否使用了任何变量/占位符,但是如果仅使用',则PHP会将其视为简单的字符串,而无需进行任何进一步处理.因此,通常'应该更快.至少在理论上.实际上,您必须进行测试.

When you use ", PHP parses the string to see if there are any variable/placeholders used inside of it, but if you use only ' PHP treats it as a simple string without any further processing. So generally ' should be faster. At least in theory. In practice you must test.

结果(以秒为单位):

a, b, c are integers:
all inside "     : 1.2370789051056
split up using " : 1.2362520694733
split up using ' : 1.2344131469727

a, b, c are strings:
all inside "     : 0.67671513557434
split up using " : 0.7719099521637
split up using ' : 0.78600907325745  <--- this is always the slowest in the group. PHP, 'nough said

在Zend Server CE PHP 5.3中使用此代码:

Using this code with Zend Server CE PHP 5.3:

<?php

echo 'a, b, c are integers:<br />';
$a = $b = $c = 123;

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br /><br />a, b, c are strings:<br />';

$a = $b = $c = '123';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';

$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
    $newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br />';

?>

这篇关于PHP字符串连接-"$ a $ b";与$ a. &quot; &quot; . $ b-效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆