DEV Community

Codeur Malin
Codeur Malin

Posted on

PHP Tips : How to use optimized built-it functions ?

Did you already use built-in function in PHP like count, strlen or in_array ?
I think so, these are essential but do you know there are some of these functions that can be executed faster !? Thanks to OpCode optimized.

OpCode you said ?

Yeah OpCode, you know this is the list of instructions that are generated when your php code is compiled and easier interpreted by the computer.

For exemple this code :


<?php

namespace foo;

$myString = 'This is my string !';
strlen($myString);

Will generate this OpCode :

line     #* E I O op                         fetch       ext  return  operands
--------------------------------------------------------------------------------
   5     0  E >   ASSIGN                                              !0, 'This+is+my+string+%21'
   6     1        INIT_NS_FCALL_BY_NAME                               'foo%5Cstrlen'
         2        SEND_VAR_EX                                         !0
         3        DO_FCALL                                 0          
         4      > RETURN                                              1

And some functions like strlen has is own opCode (you can see this in the next pictures at line 6)

line     #* E I O op            fetch          ext  return  operands
---------------------------------------------------------------------------
   5     0  E >   ASSIGN                                    !0, 'This+is+my+string+%21'
   6     1        STRLEN                            ~2      !0        
         2        FREE                                      ~2
         3      > RETURN                                    1

Instead of use a call to a function INIT_NS_FCALL_BY_NAME you can use the OpCode STRLEN directly that was optimized.

This OpCode is not use by default when you are in a namespace (like my exemple), if you want to use the optimized version you have to prefix your built-it function by a backslash \ like this : \strlen('foo bar').

To compare the performance of the two versions, I make a little script which use strlen in a loop with and without backslash and this is the result :

Not optimized strlen in a loop


<?php

namespace foo;

$bar = [];
for($i = 0; $i < 10000; $i++) {
    $bar[] = strlen('this is my string !'.$i);
}

strlen not optimized

Optimized \strlen always in a loop

<?php

namespace foo;

$bar = [];
for($i = 0; $i < 10000; $i++) {
    $bar[] = \strlen('this is my string !'.$i);
}

strlen optimized

It's a micro optimization, but always good to take.
A list of opCode was introduced with PHP 7.0, you can see it bellow:

Most useful functions

Comparison functions

  • is_null
  • is_bool
  • is_long
  • is_int
  • is_integer
  • is_float
  • is_double
  • is_real
  • is_string
  • is_array
  • is_object
  • is_resource
  • is_callable

Cast functions

  • boolval
  • intval
  • floatval
  • doubleval
  • strval

Other functions

  • chr
  • ord
  • dirname
  • defined
  • constant
  • define
  • gettype
  • get_class
  • get_called_class
  • func_num_args
  • func_get_args
  • function_exists
  • extension_loaded
  • call_user_func_array
  • call_user_func
  • array_slice

If you don't like prefix your functions with a \ you can import the function with use function instead and enjoy the benefits of optimized code.

<?php

namespace foo;

use function strlen;

$myString = 'This is my string !';
strlen($myString);

You can use a tool like PHP-CS-FIXER to indicate in your code base which optimisations can be done and fix them, for optimized functions the option is native_function_invocation.

In conclusion, use the optimized functions as much as possible, it does not require much effort and can make your code more efficient.

Here is some links :

Thank you for reading,
Codeur Malin

Discussion (1)

Collapse
gbursson profile image
Greg R.

a very nice, and quite well-hidden gem of knowledge!