哪段 PHP 代码更快?
我们都知道 xHTML/CSS 和 JS 缩小和压缩有利于大流量网站。
看看下面的代码:
include("template.funcs.php");
include("country-redirect.php");
if(($country=='PL')||($country=='POL'))
{
header('Location: index_pol.php');
exit();
}
$include_path = './global_php/';
$page = "couture";
include $include_path."shop.inc.php";
$movie_num = 1 ;
现在看看 minifed 版本:
include("template.funcs.php");include("country-redirect.php");
if(($country=='PL')||($country=='POL')){header('Location: index_pol.php');
exit();}
$include_path='./global_php/';$page="couture";include $include_path."shop.inc.php";
$movie_num=1;
你认为哪一个更快 - 一般来说,我也想开始缩小我的编程,小的 var 和字符串名称,如 $a 而不是 $apple 并尝试删除尽可能多的多余字符。 PHP 编译器喜欢压缩块还是间隔块?
We all know that xHTML/CSS and JS minification and compression benefits large traffic sites.
Take a look at the following code:
include("template.funcs.php");
include("country-redirect.php");
if(($country=='PL')||($country=='POL'))
{
header('Location: index_pol.php');
exit();
}
$include_path = './global_php/';
$page = "couture";
include $include_path."shop.inc.php";
$movie_num = 1 ;
Now see the minifed version:
include("template.funcs.php");include("country-redirect.php");
if(($country=='PL')||($country=='POL')){header('Location: index_pol.php');
exit();}
$include_path='./global_php/';$page="couture";include $include_path."shop.inc.php";
$movie_num=1;
Which one do you think is faster - in general, I want to start also minifying my programming too, small var and string names like $a rather than $apple and trying to remove as much extra character as possible. Will the PHP Compiler like a compressed chunk or spaced out one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,当 PHP 引擎解析器对您的代码发挥其魔力时,它会自动删除所有空白并以任何方式注释。如果您开始处理兆字节的文本,差异可能是 1/10 秒。但这只是网络服务器解析文件。
如果您想要真正的想法,请在 Google 上搜索“最佳 PHP 实践”并养成良好的习惯。我从你上面的片段中可以看出你可能需要一些。只是一些建议。
Well, when the PHP engine parser does its magic on your code it automatically removes all white space and comments any ways. The difference would be maybe 1/10 of a second if you start getting up in the megabytes worth of text. But this is simply the webserver parsing the files.
If you want real over head ideas search Google for "Best PHP Practices" and get into good habits. I can see from your above snippet you might need some. Just some advice.
这实际上并不重要,因为它不是通过互联网传输到客户端的。
This really doesn't matter, as it's not transfered by internet to the client.
不要紧。它们之间的任何差异最多只有微秒。
使代码可读是唯一重要的事情。
It does not matter. Any difference between these will be in the microseconds at best.
Making code readable is the only thing that matters.
PHP 代码保留在服务器上,因此它的大小本质上是无关紧要的。
删除这些换行符是一个非常糟糕的主意。让你的代码对人类来说是可读的。
PHP code stays on the server, so it's size is essentially irrelevant.
Removing those newlines is a really bad idea. Make your code readable for humans.
PHP 并不关心你的代码是否被精简。
编写代码,以便以后可以干净地编辑它。缩小对性能没有可衡量的影响。
你看到缩小的 CSS/JavaScript 的原因不是为了解析/执行速度......而是为了减少数据传输的文件大小。您的 PHP 是在服务器端处理的。唯一发送的内容是代码的输出。
PHP doesn't care if your code is minified or not.
Write code so you can edit it cleanly later. Minification has no measurable impact on performance.
The reason that you see minified CSS/JavaScript is not for parsing/execution speed... it is for cutting down the file size for data transfer. Your PHP is processed server-side. The only thing that is sent is the output of your code.
缩小版本不会更快,原因有两个:
发送
如果您想为 php 代码提供种子,您可以安装 php加速器
The minified version won't be faster for 2 reasons:
is sent
If you want to seed up your php code, you can install a php accelerator
正如其他人所说,PHP 代码的缩小不会对执行速度产生真正的影响。这就是你问题的答案,但我认为这句话也很相关:
请不要缩小代码的主要版本。请不要使用
$a
等变量名称来代替$apple
。阅读和理解代码的能力远比通过缩小而节省的空间或边际速度的提高更有价值。As others have said, minification of PHP code will make no real difference in execution speed. That is the answer to your question, but I think that this quote is also relevant:
Please don't minfiy the primary version of your code. Please don't use variable names like
$a
instead of$apple
. The ability to read and understand your code is far more valuable than any space savings or marginal speed increase that you may get from minification.