在索引名称周围不使用单引号是否会明显/显着地减慢执行速度?

发布于 2024-12-10 04:19:46 字数 192 浏览 0 评论 0原文

如果我要说

echo $arr[some_index];

而不是说

echo $arr['some_index'];

错误通知会导致大量处理器时间/功率损失吗?我知道这不是正确的语法,但是在我正在从事的项目中已经有大量这样编写的代码。

If I were to say

echo $arr[some_index];

as opposed to saying

echo $arr['some_index'];

Will there be a significant amount of processor time/power lost to the error notice? I am aware that it is not proper syntax, but there is a huge amount of code written like this already on a project I am working on.

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

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

发布评论

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

评论(2

独闯女儿国 2024-12-17 04:19:46

嗯,检查起来很简单。您可以检查任何语句的执行时间,如下所示:

$start = microtime(true);    
//Do your code. Try an echo of one kind here.    
$end = microtime(true);    
echo($end - $start); //The elapsed time, in seconds. Precise up to a microsecond.

为您想要测试的每种类型执行其中一项。自然,无论哪个始终最快,都将是最快的。

您还可以使用 memory_get_usage 来确定每次调用之前和之后使用了多少内存。

现在,您还应该收到大量通知。如果未定义常量,则会将其视为字符串,但会引发通知。另一个问题是,如果您的密钥与常量发生冲突,您将检查错误的值。这确实不是一个好的做法。我会检查并更换所有东西。

Well, it's simple enough to check. You can check the execution time on any statement(s) like such:

$start = microtime(true);    
//Do your code. Try an echo of one kind here.    
$end = microtime(true);    
echo($end - $start); //The elapsed time, in seconds. Precise up to a microsecond.

Do one of those for each type you'd like to test. Whichever is consistently fastest will be the quickest, naturally.

You can also use memory_get_usage to determine how much memory has been used, before and after each call.

Now, you should also be getting a large number of NOTICE's. If a constant isn't defined, it's treated as a string instead, but throws a notice. Another problem is if your key ever conflicts with a constant, you'll be checking the wrong value. It's really just not good practice. I'd go through and replace everything.

柠栀 2024-12-17 04:19:46

我认为对性能的影响可以忽略不计,但是我的纯粹主义者希望看到一致使用引号/不引号。

I think the performance impact would be negligible, however the purist in me would want to see consistent use of quotes/no quotes.

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