串联,多个参数还是sprintf? [英] Concatenation, multiple parameters or sprintf?
问题描述
我正在优化我的PHP代码,发现您可以通过以下方式加快回显-确切的说,您可以将echo "The name of the user is $name" . ".";
替换为:
I am working to optimize my PHP code and found out that you can speed up echoing in these ways - exactly, you can replace echo "The name of the user is $name" . ".";
with:
-
echo 'The name of the user is '.$name.'.';
-
echo "The name of the user is", $name, ".";
-
echo sprintf("The name of the user is %s", $name);
echo 'The name of the user is '.$name.'.';
echo "The name of the user is", $name, ".";
echo sprintf("The name of the user is %s", $name);
哪个是最快的?我不仅希望看到基准测试,还希望获得一些技术上的解释.
Which one is the fastest? I'd like not only to see benchmarks, but also some technical explaination, if possible.
推荐答案
首先,这是微优化,您最好为更快的服务器付费并开发更多产品,然后再花费数小时的微优化.但是,根据 http://micro-optimization.com/,以下是结果:
Firstly, this is micro optimization and you're probably better paying for a faster server and developing more product then spending hours and hours micro optimizing. However according to http://micro-optimization.com/ here are the results:
sprintf()比双引号慢138.68%(慢1.4倍)
sprintf() is slower than double quotes by 138.68% (1.4 times slower)
和
sprintf()比单引号慢163.72%(慢1.6倍)
sprintf() is slower than single quotes by 163.72% (1.6 times slower)
这篇关于串联,多个参数还是sprintf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!