为什么 PHP 中 === 比 == 更快?

发布于 2024-08-24 16:05:43 字数 55 浏览 7 评论 0原文

为什么 PHP 中 ===== 更快?

Why is === faster than == in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(12

负佳期 2024-08-31 16:05:43

因为相等运算符 == 会临时强制或转换数据类型以查看它是否等于另一个操作数,而 === (恒等运算符)则不会不需要进行任何转换,因此完成的工作更少,这使得速度更快。

Because the equality operator == coerces, or converts, the data type temporarily to see if it’s equal to the other operand, whereas === (the identity operator) doesn’t need to do any converting whatsoever and thus less work is done, which makes it faster.

云醉月微眠 2024-08-31 16:05:43

=== 不执行类型转换,因此 0 == '0' 计算结果为 true,但 0 === '0 ' - 为 false

=== does not perform typecasting, so 0 == '0' evaluates to true, but 0 === '0' - to false.

爱本泡沫多脆弱 2024-08-31 16:05:43

有两件事需要考虑:

  1. 如果操作数类型不同,则 ===== 会产生不同的结果。在这种情况下,操作员的速度并不重要;重要的是哪一个产生所需的结果。

  2. 如果操作数类型相同,则可以使用 =====,因为两者都会产生相同的结果。在这种情况下,两个操作员的速度几乎相同。这是因为这两个运算符均不执行类型转换。

我比较了以下速度:

  • $a == $b$a === $b
  • 其中 $a$b 是随机整数 [1, 100]
  • 生成两个变量并比较一百万次,
  • 测试运行 10 次

,结果如下:

 $a == $b $a === $b
--------- ---------
 0.765770  0.762020
 0.753041  0.825965
 0.770631  0.783696
 0.787824  0.781129
 0.757506  0.796142
 0.773537  0.796734
 0.768171  0.767894
 0.747850  0.777244
 0.836462  0.826406
 0.759361  0.773971
--------- ---------
 0.772015  0.789120

您可以看到速度几乎相同。

There are two things to consider:

  1. If operand types are different then == and === produce different results. In that case the speed of the operators does not matter; what matters is which one produces the desired result.

  2. If operand types are same then you can use either == or === as both will produce same results. In that case the speed of both operators is almost identical. This is because no type conversion is performed by either operators.

I compared the speed of:

  • $a == $b vs $a === $b
  • where $a and $b were random integers [1, 100]
  • the two variables were generated and compared one million times
  • the tests were run 10 times

And here are the results:

 $a == $b $a === $b
--------- ---------
 0.765770  0.762020
 0.753041  0.825965
 0.770631  0.783696
 0.787824  0.781129
 0.757506  0.796142
 0.773537  0.796734
 0.768171  0.767894
 0.747850  0.777244
 0.836462  0.826406
 0.759361  0.773971
--------- ---------
 0.772015  0.789120

You can see that the speed is almost identical.

墨落画卷 2024-08-31 16:05:43

首先, === 检查两个参数的类型是否相同 - 因此在实际进行任何比较之前,数字 1 和字符串“1”在类型检查中失败。另一方面, == 不会首先检查类型,而是继续将两个参数转换为相同类型,然后进行比较。

因此, === 可以更快地检查失败条件

First, === checks to see if the two arguments are the same type - so the number 1 and the string '1' fails on the type check before any comparisons are actually carried out. On the other hand, == doesn't check the type first and goes ahead and converts both arguments to the same type and then does the comparison.

Therefore, === is quicker at checking a fail condition

临风闻羌笛 2024-08-31 16:05:43

我真的不知道它是否明显更快,但是在大多数语言中 === 是直接类型比较,而 == 如果需要/可能会尝试进行类型强制以获得匹配。

I don't really know if it's significantly faster, but === in most languages is a direct type comparison, while == will try to do type coercion if necessary/possible to gain a match.

爱的那么颓废 2024-08-31 16:05:43

== 在比较之前会产生较大的类型转换开销。 === 首先检查类型,然后继续进行,无需进行任何类型转换。

The == incurs a larger overhead of type conversion before comparison. === first checks the type, then proceeds without having to do any type conversion.

墨落画卷 2024-08-31 16:05:43

因为 === 不在比较操作数之前需要强制操作数为相同类型

我怀疑速度差异是否很大。在正常情况下,您应该使用更有意义的运算符。

Because === doesn't need to coerce the operands to be of the same type before comparing them.

I doubt the difference in speed is very much though. Under normal circumstances you should use whichever operator makes more sense.

燃情 2024-08-31 16:05:43

总之 === 更快,因为不转换数据类型来查看两个变量是否具有相同的值,但是当您需要查看两个变量是否具有相同的值时,如果不关心变量是什么类型,您将使用 == , 或 === if 变量的类型也很重要。

In conclusion === is faster because don't converts the data type to see if two variables have same value, but when you need to see if two variables have same value you will use == if doesen't mather what type are variables, or === if is important also the type of variables.

贱贱哒 2024-08-31 16:05:43

我发现两个操作员之间实际上存在显着的速度差异。下面是在 docker 容器中运行的 php 8.0.0 RC5 和 php 7.4.12 的结果。该项目托管在 github 上,以便每个人都可以查看该方法。免责声明:我构建了该工具。

$ php src/benchmark.php --custom --filter ~equal~
PHP benchmark

-------------------------------
platform           :  Linux x64
php version        :     7.4.12
xdebug             :        off
memory limit       :       128M
max execution      :          0
time per iteration :       50ms
iterations         :        100
-------------------------------
---------------------------------------------------
0                  :         ==       ===
mean               :     394156    459015    +16.5%
median             :     397448    461822    +16.2%
mode               :     398154    458062    +15.0%
minimum            :     313271    405692    +29.5%
maximum            :     411157    480360    +16.8%
quartile 1         :     393222    454952    +15.7%
quartile 3         :     400881    466491    +16.4%
IQ range           :       7659     11538    +50.7%
std deviation      :      15593     11867    -23.9%
normality          :       0.8%      0.8%
---------------------------------------------------

$ php src/benchmark.php --custom --filter ~equal~
PHP benchmark

-------------------------------
platform           :  Linux x64
php version        :   8.0.0RC5
xdebug             :        off
memory limit       :       128M
max execution      :          0
time per iteration :       50ms
iterations         :        100
-------------------------------
---------------------------------------------------
0                  :         ==       ===
mean               :     405032    474768    +17.2%
median             :     409226    477313    +16.6%
mode               :     408421    479741    +17.5%
minimum            :     311606    386509    +24.0%
maximum            :     417895    491842    +17.7%
quartile 1         :     405740    473436    +16.7%
quartile 3         :     412677    483139    +17.1%
IQ range           :       6937      9703    +39.9%
std deviation      :      17501     15657    -10.5%
normality          :       1.0%      1.0%
---------------------------------------------------

I found out that there actually is a significant speed difference between the 2 operators. Results for php 8.0.0 RC5 and php 7.4.12 running in docker container below. The project is hosted on github so everyone can review the methodology. Disclaimer: I built the tool.

$ php src/benchmark.php --custom --filter ~equal~
PHP benchmark

-------------------------------
platform           :  Linux x64
php version        :     7.4.12
xdebug             :        off
memory limit       :       128M
max execution      :          0
time per iteration :       50ms
iterations         :        100
-------------------------------
---------------------------------------------------
0                  :         ==       ===
mean               :     394156    459015    +16.5%
median             :     397448    461822    +16.2%
mode               :     398154    458062    +15.0%
minimum            :     313271    405692    +29.5%
maximum            :     411157    480360    +16.8%
quartile 1         :     393222    454952    +15.7%
quartile 3         :     400881    466491    +16.4%
IQ range           :       7659     11538    +50.7%
std deviation      :      15593     11867    -23.9%
normality          :       0.8%      0.8%
---------------------------------------------------

$ php src/benchmark.php --custom --filter ~equal~
PHP benchmark

-------------------------------
platform           :  Linux x64
php version        :   8.0.0RC5
xdebug             :        off
memory limit       :       128M
max execution      :          0
time per iteration :       50ms
iterations         :        100
-------------------------------
---------------------------------------------------
0                  :         ==       ===
mean               :     405032    474768    +17.2%
median             :     409226    477313    +16.6%
mode               :     408421    479741    +17.5%
minimum            :     311606    386509    +24.0%
maximum            :     417895    491842    +17.7%
quartile 1         :     405740    473436    +16.7%
quartile 3         :     412677    483139    +17.1%
IQ range           :       6937      9703    +39.9%
std deviation      :      17501     15657    -10.5%
normality          :       1.0%      1.0%
---------------------------------------------------
乙白 2024-08-31 16:05:43

在 php(c 代码)中,值是一个“类”,例如:

class value
{
    $int_;
    $float_;
    $string_;
    $array_;
    $object_;
}

当您比较 $a == $b$a 时,$a == $bint类型,会有类似这样的内容:

if ($a->int_ == $b->int_ || $a->int_ == (int) $b->float_ || $a->int_ == (int) $b->string_ || ...)

但是 string '1' 不会被转换为 ascii 代码 49,它将是 1

当你比较 $a === $b$aint 类型时,会有类似这样的内容:

if ($a->int_ == $b->int_)

In php (c code) value is a "class" like:

class value
{
    $int_;
    $float_;
    $string_;
    $array_;
    $object_;
}

When your are comparing $a == $b and $a is int type, there will be something like:

if ($a->int_ == $b->int_ || $a->int_ == (int) $b->float_ || $a->int_ == (int) $b->string_ || ...)

but string '1' will not be cast to ascii code 49, it will be 1.

When you are comparing $a === $b and $a is int type, there will be someting like:

if ($a->int_ == $b->int_)
蓝梦月影 2024-08-31 16:05:43

更快不应仅以直接执行时间来衡量(在这种情况下,直接性能测试几乎可以忽略不计)。也就是说,我需要查看涉及迭代或递归的测试,以真正查看是否存在显着的累积差异(当在现实环境中使用时)。处理边缘情况时节省的测试和调试时间对您来说也应该很有意义

Faster should not just be measured in direct execution time (direct performance tests are almost negligible in this case). That said, I would need to see a test involving iteration, or recursion, to really see if there is a significant, cumulative difference (when used in a realistic context). The testing and debugging time you will save when dealing with edge cases should be meaningful to you, also

葵雨 2024-08-31 16:05:43

如果测试结果正确,那么它一定是编译器问题,

处理器将在一个时钟周期内执行它被告知要做的任何事情

如果它要做的事情较少,那么它会更快地执行

添加:

嗯,实际上,如果编译器已经创建了大量要处理的机器代码,那么如果它已经添加了无数的东西来处理需要比较的数据类型,那么删除一个“次要”IF 根本不会改变速度。

如果有人仍然阅读本文,那么我有兴趣进行更多讨论。

菲尔

If the test results are correct, then it must be a compiler issue,

The processor will do whatever it is told to do on a clock cycle

If it has less to do then it will be quicker to do

Addition:

Ah well actually if the compiler has already created loads of machine code to be processed, then if it has already added zillions of stuff to cope with what type of data needs comparing, then the removal of one "minor" IF will not change speeds much at all.

If anyone still reads this are then I am interesting in more discussion.

Phil

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