简体   繁体   English

php没有警告会运行得更快吗?

[英]Does php run faster without warnings?

因为PHP代码运行得很好,即使它有关于未定义索引的警告和通知以及被称为静态等的非静态方法,问题是,如果我花时间从我的代码中删除所有通知和警告将它跑得快得多?

I bookmarked an article in which the author did some benchmarks about that ; 我给一篇文章添加了书签,其中作者对此做了一些基准测试; unfortunatly, it's in french... but here it is (maybe you'll understand some parts of it) : Ne faites pas d'erreur 不幸的是,这是法语...但在这里(也许你会理解它的一些部分)Ne faites pas d'erreur

And here are the numbers, to help people who don't read french : 这里有数字,以帮助那些不读法语的人:

  • 10k notices, with error_reporting and display_errors enabled : 5,162.76 ms 10k通知,启用error_reportingdisplay_errors :5,162.76 ms
  • same, but with display_errors disabled : 136.18 ms 相同,但禁用了display_errors :136.18 ms
  • same, but with error_reporting disabled too : 117.79 ms 相同,但也禁用了error_reporting :117.79毫秒
  • and, finally, after patching the code so it doesn't produce any notice anymore : 19.51 ms 最后,在修补代码之后,它不再产生任何通知:19.51 ms

Which means that, yes, PHP code runs faster without notices/warnings/errors, even when those are not displayed nor reported. 这意味着,是的,PHP代码运行得更快,没有通知/警告/错误,即使没有显示也没有报告。


Derick Rethans says the same thing in this article : Five reasons why the shut-op operator (@) should be avoided (quoting) : Derick Rethans在本文中说了同样的事情: 为什么应该避免关闭操作符(@)的五个原因 (引用)

Reason 3: It's slow (part 2) 原因3:它很慢(第2部分)

Whenever PHP generates an error message internally, it's processed and formatted all the way up to the fully formatted message that can be outputted straight to the browser. 每当PHP在内部生成错误消息时,它都会被处理并格式化,直到完全格式化的消息,可以直接输出到浏览器。
Only just before it is displayed the error_reporting setting is checked. 仅在它显示之前,检查error_reporting设置。 This however, is not related to the @-operator exclusively. 但是,这与@ -operator无关。
The error message is just always fully formatted before error_reporting is checked—or display_errors for that matter. 在检查error_reporting之前,错误消息总是完全格式化 - 或者就此而言display_errors

Depends on the amount of warnings, but the error handling in PHP is, even when hiding error messages, relatively expensive. 取决于警告的数量,但PHP中的错误处理,即使隐藏错误消息,也相对昂贵。

What you'd have to do to estimate the effect is profiling on C level: Install valgrind (assuming you're on Linux) and then run 你需要做些什么来估计效果是在C级上进行分析:安装valgrind(假设你在Linux上)然后运行

callgrind /path/to/bin/php /path/to/script.php

this generates a file called callgrind.12345 or so, load this file into an app like kcachegrind and look for php_error_docref0 or php_error_cb to see how much time was spent in the error handler. 这会生成一个名为callgrind.12345的文件,将此文件加载到像kcachegrind这样的应用程序中,并查找php_error_docref0php_error_cb以查看在错误处理程序中花费了多少时间。

Please mind the cachegrind and valgrind docs when doing that and mind that there are many system-dependant variables involved. 这样做时请注意cachegrind和valgrind文档,并注意涉及许多与系统相关的变量。

EDIT: Oh one more note: I assume that way more time is spent while talking to databases and similar systems. 编辑:哦,更多的一个音符:我假设的方式 ,一边给数据库和类似系统有更多的时间是花在。 and yet another additional note: fixing notices usually makes the code more robust for future changes so it's a good idea independent from performance. 另外还有一个注意事项:修复通知通常会使代码在未来的更改中更加健壮,因此独立于性能是一个好主意。

I wouldn't quite call it a "significant" improvement for most cases, but running code that does not generate errors of any sort naturally runs faster than code that has to generate a stack trace every other line. 在大多数情况下,我不会将其称为“重大”改进,但是运行不会产生任何类型错误的代码自然会比必须每隔一行生成堆栈跟踪的代码运行得更快。

Check out: http://www.noamdesign.com/Web-Design-Blog/15-tips-to-optimizing-your-php-code/ for more information on minor optimizations you can make to your code. 请访问: http//www.noamdesign.com/Web-Design-Blog/15-tips-to-optimizing-your-php-code/ ,了解有关可以对代码进行的次要优化的更多信息。

In my own experience, I've found that 95% of code optimization usually deals with how you use your database. 根据我自己的经验,我发现95%的代码优化通常会处理您使用数据库的方式。

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

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