简体   繁体   English

PHP-更快使用代码

[英]PHP - Faster to use code

In general, is code length affective to the speed of the program? 通常,代码长度是否会影响程序速度? I'm talking about differences in small sizes not comparing 10,000 lines to 10. 我说的是小尺寸的差异,而不是将10,000行与10行比较。

Example #1: 范例1:

// Ternary Operator
$todo = (empty($_POST['todo'])) ? 'default' : $_POST['todo'];

VS VS

// The above is identical to this if/else statement
if (empty($_POST['todo'])) {
    $action = 'default';
} else {
    $action = $_POST['todo'];
}

And other examples like not using common brackets and universal indentation. 还有其他一些例子,例如不使用通用括号和通用缩进。

Example #2: 范例2:

if ($gollum === 'halfling') {
    $height --;
}

VS VS

if ($gollum === 'halfling') $height --;

Also using CamelCase naming convention will lower the characters length, is that effective at all? 另外,使用CamelCase命名约定将缩短字符长度,这是否有效? I doubt this is effective, but still it uses less characters. 我怀疑这是否有效,但仍然使用较少的字符。

Cheers! 干杯!

In your first example , you are using two different constructs, namely the 'ternary operator' and an 'if / else' statement. 在第一个示例中 ,您使用两种不同的构造,即“三元运算符”和“ if / else”语句。

Since the 'if / else' statement is more general (ie you can do more thing with it), the compiler / parser will have more difficulties to optimize it. 由于“ if / else”语句更通用(即您可以用它做更多的事情),因此编译器/解析器在优化它方面会遇到更多困难。 So I would say that the ternary operator might be a little bit faster. 所以我想说三元运算符可能会快一点。

In the second example , it is only about coding style, both codes will probably be compiled / parsed to exactly the same thing, so no gain here. 在第二个示例中 ,仅涉及编码样式,两个代码可能都将被编译/解析为完全相同的东西,因此这里没有好处。

However, premature optimization is the root of all evil ! 但是, 过早的优化是万恶之源 The gain from doing such micro tiny optimization, even on a whole application will probably be negligible and will most of the time be at the expense of readability and maintainability. 即使是在整个应用程序上,进行这种微小的优化所带来的收益也将是微不足道的,并且在大多数情况下会以可读性和可维护性为代价。 So I would strongly advice against that kind of thing. 因此,我强烈反对这种事情。

Concerning characters length , it won't matter at all, the compiler / parser will transform that anyway to some other representation that could be understood by the computer, so the only thing that will change is the space that your source code will take on the hard drive. 关于字符长度 ,根本没有关系,编译器/解析器会将其转换为计算机可以理解的其他表示形式,因此唯一会改变的是您的源代码占用的空间。硬盘。

Just write your code the way it is easier for you to understand and let the compiler / parser do those kind of thing. 只需以更易于理解的方式编写代码,然后让编译器/解析器执行此类操作即可。 It is much better to optimize the algorithm itself than the way of writing it. 优化算法本身要比编写算法好得多。

I think you shoud concern coding style more than efficiency now. 我认为您现在应该更加关注编码风格,而不是效率。

if you already have a style guide, follow it. 如果您已经有了样式指南,请遵循它。 if you not, you can find one or create one. 如果没有,您可以找到一个或创建一个。 There aren't such difference between your codes both of two examples. 两个示例的代码之间没有这种区别。 You can pick anyone to create your own style guide. 您可以选择任何人来创建自己的样式指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM