真像实验室:php中使用双引号的效率真的比单引号慢吗?第二章
上一文中,楼主只对比了纯的字符串定义,不包含变量解析的,很多朋友反馈不免严谨,不能说明什么,所以楼主再开文章,补了以下case:
一次变量解析
字符串的$在字符串最尾
字符串的$在字符串中间位置
试验代码分别如下:
<?php
$aa = "aa";
for($i=0;$i<100000;$i++) {
$b = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$aa";
}
<?php
$aa = 'aa';
for($i=0;$i<100000;$i++) {
$b = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$';
}
实验数据如下:
次数 | 双引号一次变量 | 单引号一次变量 | 双引号$在最后 | 单引号$在最后 | 双引号$在中间 | 单引号$在中间 |
1 | real 0m0.044s user 0m0.023s sys 0m0.021s | real 0m0.044s user 0m0.020s sys 0m0.023s | real 0m0.039s user 0m0.019s sys 0m0.019s | real 0m0.039s user 0m0.019s sys 0m0.020s | real 0m0.039s user 0m0.014s sys 0m0.024s | real 0m0.039s user 0m0.022s sys 0m0.016s |
2 | real 0m0.044s user 0m0.027s sys 0m0.017s | real 0m0.044s user 0m0.023s sys 0m0.021s | real 0m0.038s user 0m0.021s sys 0m0.016s | real 0m0.037s user 0m0.015s sys 0m0.022s | real 0m0.038s user 0m0.017s sys 0m0.020s | real 0m0.038s user 0m0.018s sys 0m0.019s |
3 | real 0m0.045s user 0m0.022s sys 0m0.022s | real 0m0.044s user 0m0.027s sys 0m0.016s | real 0m0.038s user 0m0.017s sys 0m0.020s | real 0m0.037s user 0m0.016s sys 0m0.021s | real 0m0.038s user 0m0.014s sys 0m0.023s | real 0m0.038s user 0m0.017s sys 0m0.020s |
4 | real 0m0.044s user 0m0.025s sys 0m0.018s | real 0m0.044s user 0m0.023s sys 0m0.020s | real 0m0.038s user 0m0.018s sys 0m0.020s | real 0m0.037s user 0m0.018s sys 0m0.020s | real 0m0.037s user 0m0.020s sys 0m0.018s | real 0m0.038s user 0m0.018s sys 0m0.020s |
5 | real 0m0.044s user 0m0.022s sys 0m0.022s | real 0m0.044s user 0m0.027s sys 0m0.017s | real 0m0.038s user 0m0.020s sys 0m0.017s | real 0m0.037s user 0m0.021s sys 0m0.016s | real 0m0.038s user 0m0.017s sys 0m0.020s | real 0m0.038s user 0m0.015s sys 0m0.022s |
6 | real 0m0.044s user 0m0.024s sys 0m0.019s | real 0m0.044s user 0m0.022s sys 0m0.021s | real 0m0.038s user 0m0.017s sys 0m0.021s | real 0m0.037s user 0m0.017s sys 0m0.020s | real 0m0.038s user 0m0.017s sys 0m0.020s | real 0m0.038s user 0m0.016s sys 0m0.021s |
7 | real 0m0.044s user 0m0.021s sys 0m0.022s | real 0m0.044s user 0m0.019s sys 0m0.024s | real 0m0.038s user 0m0.017s sys 0m0.021s | real 0m0.037s user 0m0.023s sys 0m0.014s | real 0m0.038s user 0m0.018s sys 0m0.020s | real 0m0.038s user 0m0.020s sys 0m0.018s |
8 | real 0m0.045s user 0m0.024s sys 0m0.021s | real 0m0.044s user 0m0.022s sys 0m0.023s | real 0m0.038s user 0m0.016s sys 0m0.021s | real 0m0.037s user 0m0.021s sys 0m0.017s | real 0m0.038s user 0m0.016s sys 0m0.021s | real 0m0.038s user 0m0.022s sys 0m0.015s |
9 | real 0m0.044s user 0m0.021s sys 0m0.022s | real 0m0.057s user 0m0.031s sys 0m0.025s | real 0m0.037s user 0m0.022s sys 0m0.015s | real 0m0.038s user 0m0.021s sys 0m0.016s | real 0m0.037s user 0m0.020s sys 0m0.017s | real 0m0.039s user 0m0.022s sys 0m0.016s |
10 | real 0m0.044s user 0m0.025s sys 0m0.018s | real 0m0.044s user 0m0.023s sys 0m0.020s | real 0m0.038s user 0m0.016s sys 0m0.021s | real 0m0.038s user 0m0.020s sys 0m0.017s | real 0m0.038s user 0m0.020s sys 0m0.017s | real 0m0.038s user 0m0.015s sys 0m0.022s |
AVG. | real:0m0.0442s user:0m0.0234s sys:0m0.0202s | real:0m0.0453s user:0m0.0237s sys:0m0.021s | real 0m0.038s user 0m0.0183s sys 0m0.019s | real: 0m0.0374s user:0m0.0191s sys: 0m0.0183s | real 0m0.0379s user 0m0.0173s sys 0m0.020s | real 0m0.0382s user 0m0.0185s sys 0m0.0189s |
实验结果和前文提到的一致:没有数据表明双引号定义字符串会比单引号慢。
相反双引号可以零活的嵌套变量,不需要反复的拼接字符串,并且支持转义字符,使用上更加方便,当然如上文网友的评论一样,单引号输入更方便,不需要同时按下shift键,编码效率有一定的影响。