哪段 PHP 代码更快?

发布于 2024-12-06 09:06:19 字数 748 浏览 0 评论 0原文

我们都知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

篱下浅笙歌 2024-12-13 09:06:20

好吧,当 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.

一袭白衣梦中忆 2024-12-13 09:06:20

这实际上并不重要,因为它不是通过互联网传输到客户端的。

This really doesn't matter, as it's not transfered by internet to the client.

满栀 2024-12-13 09:06:19

PHP 编译器喜欢压缩块还是间隔块?

不要紧。它们之间的任何差异最多只有微秒。

使代码可读是唯一重要的事情。

Will the PHP Compiler like a compressed chunk or spaced out one?

It does not matter. Any difference between these will be in the microseconds at best.

Making code readable is the only thing that matters.

浪荡不羁 2024-12-13 09:06:19

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.

当爱已成负担 2024-12-13 09:06:19

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.

千寻… 2024-12-13 09:06:19

缩小版本不会更快,原因有两个:

  • 脚本不会发送到客户端,而是进行解释,然后得出结果
    发送
  • 缩小 php 脚本对性能影响很小

如果您想为 php 代码提供种子,您可以安装 php加速器

The minified version won't be faster for 2 reasons:

  • The script is not sent to the client, but interpreted, and then the result
    is sent
  • Minifying a php script has very little impact on performance

If you want to seed up your php code, you can install a php accelerator

水波映月 2024-12-13 09:06:19

正如其他人所说,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:

Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文