开发者

Trying to get property of non-object, and similar errors - performance hit?

开发者 https://www.devze.com 2023-01-25 02:31 出处:网络
If I have code producing the above n开发者_如何学运维otice, and similar such as undefined offset for arrays, would adding isset() or empty() checks improve the performance of the script in addition to

If I have code producing the above n开发者_如何学运维otice, and similar such as undefined offset for arrays, would adding isset() or empty() checks improve the performance of the script in addition to remvoing the error notices?

Edit: Just to clarify, I want error reporting on and I know isset will get around the notices, this question was more about the performance side.


First, I'm assuming you have 3 options.

The first is turning error_reporting off and using unknown array offsets with impunity:

error_reporting(0);

The second is using @ error suppression:

@$my_array['a'];

The third is using isset():

if (isset($my_array['a'])) {
    $my_array['a'];
}

I've hacked up a quick benchmarking script which gives the following results for 1000000 executions:

Turning off error reporting:  6 seconds
Using error suppression:     18 seconds
Using isset():                9 seconds


I'd wager that the biggest performance hit would be on Apache writing these errors out to file. The warning generation itself should not be too expensive but you could check by profiling your code with xdebug or similar to be sure.


Only thing you need is to check if your variables are set using isset() as that will remove your notice. I believe it's good practice to do so if you plan to use any variable which might not be defined.

Also, if you don't care about the variable being set or not, you might as well use @. ( take a look here in case you don't know what @ is )

I don't think there will be any significant performance difference (being a micro optimization if any) but I believe there is an huge visual improvement if no errors/notices/warnings are shown.

About hiding errors/notices/warnings, take a look here.

0

精彩评论

暂无评论...
验证码 换一张
取 消