Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP does lots of type checks slow down your code? [closed]

I have a couple of classes that deal with objects, arrays and strings.

I was wondering what the impact on performance is to do type checks like:

is_object(), is_array() and is_string();

I use these quite a lot in my code.

In functions that are being called many thousands of times, is it better to assume the arguments passed is the correct type or check to prevent errors?

Is there any resource where you can have a look at the performance weight of php functions?

like image 502
Dieter Gribnitz Avatar asked Apr 26 '26 07:04

Dieter Gribnitz


1 Answers

You can find out yourself.

Store the current time in milliseconds at the start of your script, run a loop which does a type check several thousand times, and compare the time at the end of your script with the time at the start of your script.

like image 101
Ryan Avatar answered Apr 27 '26 19:04

Ryan