3v4l.org

run code in 300+ PHP versions simultaneously
<?php class HardcodedLocator { public function getFoo() { return null; } } class ConfigurableLocator { protected $services = array(); public function set( $name, $value ) { if ( isset( $this->services[$name] ) ) { throw new LogicException( 'Cannot redefine!' ); } $this->services[$name] = $value; } public function get( $name ) { if ( !isset( $this->services[$name] ) ) { throw new LogicException( 'No such service' ); } return $this->services[$name](); } } $hl = new HardcodedLocator(); $cl = new ConfigurableLocator(); $cl->set( 'foo', function() { return null; } ); $rounds = 10000; $htime = microtime( true ); for ( $i = 0; $i < $rounds; $i++ ) { $hl->getFoo(); } $htime = microtime( true ) - $htime; $ctime = microtime( true ); for ( $i = 0; $i < $rounds; $i++ ) { $cl->get( 'foo' ); } $ctime = microtime( true ) - $ctime; echo 'overhead: ' . ( ( $ctime - $htime ) / $rounds );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 18
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 34
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 34
Branch analysis from position: 40
Branch analysis from position: 34
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 18
Branch analysis from position: 23
Branch analysis from position: 18
filename:       /in/dpfvk
function name:  (null)
number of ops:  50
compiled vars:  !0 = $hl, !1 = $cl, !2 = $rounds, !3 = $htime, !4 = $i, !5 = $ctime
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   NEW                                              $6      'HardcodedLocator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $6
   26     3        NEW                                              $9      'ConfigurableLocator'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $9
   27     6        INIT_METHOD_CALL                                         !1, 'set'
          7        SEND_VAL_EX                                              'foo'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fdpfvk%3A27%240'
          9        SEND_VAL_EX                                              ~12
         10        DO_FCALL                                      0          
   29    11        ASSIGN                                                   !2, 10000
   31    12        INIT_FCALL                                               'microtime'
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $15     
         15        ASSIGN                                                   !3, $15
   32    16        ASSIGN                                                   !4, 0
         17      > JMP                                                      ->21
   33    18    >   INIT_METHOD_CALL                                         !0, 'getFoo'
         19        DO_FCALL                                      0          
   32    20        PRE_INC                                                  !4
         21    >   IS_SMALLER                                               !4, !2
         22      > JMPNZ                                                    ~20, ->18
   35    23    >   INIT_FCALL                                               'microtime'
         24        SEND_VAL                                                 <true>
         25        DO_ICALL                                         $21     
         26        SUB                                              ~22     $21, !3
         27        ASSIGN                                                   !3, ~22
   37    28        INIT_FCALL                                               'microtime'
         29        SEND_VAL                                                 <true>
         30        DO_ICALL                                         $24     
         31        ASSIGN                                                   !5, $24
   38    32        ASSIGN                                                   !4, 0
         33      > JMP                                                      ->38
   39    34    >   INIT_METHOD_CALL                                         !1, 'get'
         35        SEND_VAL_EX                                              'foo'
         36        DO_FCALL                                      0          
   38    37        PRE_INC                                                  !4
         38    >   IS_SMALLER                                               !4, !2
         39      > JMPNZ                                                    ~29, ->34
   41    40    >   INIT_FCALL                                               'microtime'
         41        SEND_VAL                                                 <true>
         42        DO_ICALL                                         $30     
         43        SUB                                              ~31     $30, !5
         44        ASSIGN                                                   !5, ~31
   43    45        SUB                                              ~33     !5, !3
         46        DIV                                              ~34     ~33, !2
         47        CONCAT                                           ~35     'overhead%3A+', ~34
         48        ECHO                                                     ~35
         49      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2Fdpfvk%3A27%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dpfvk
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E > > RETURN                                                   null
          1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fdpfvk%3A27%240

Class HardcodedLocator:
Function getfoo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dpfvk
function name:  getFoo
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E > > RETURN                                                   null
          1*     > RETURN                                                   null

End of function getfoo

End of class HardcodedLocator.

Class ConfigurableLocator:
Function set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dpfvk
function name:  set
number of ops:  13
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        FETCH_OBJ_IS                                     ~2      'services'
          3        ISSET_ISEMPTY_DIM_OBJ                         0          ~2, !0
          4      > JMPZ                                                     ~3, ->9
   12     5    >   NEW                                              $4      'LogicException'
          6        SEND_VAL_EX                                              'Cannot+redefine%21'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $4
   14     9    >   FETCH_OBJ_W                                      $6      'services'
         10        ASSIGN_DIM                                               $6, !0
         11        OP_DATA                                                  !1
   15    12      > RETURN                                                   null

End of function set

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dpfvk
function name:  get
number of ops:  15
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        FETCH_OBJ_IS                                     ~1      'services'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->9
   19     5    >   NEW                                              $4      'LogicException'
          6        SEND_VAL_EX                                              'No+such+service'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $4
   21     9    >   FETCH_OBJ_R                                      ~6      'services'
         10        FETCH_DIM_R                                      ~7      ~6, !0
         11        INIT_DYNAMIC_CALL                                        ~7
         12        DO_FCALL                                      0  $8      
         13      > RETURN                                                   $8
   22    14*     > RETURN                                                   null

End of function get

End of class ConfigurableLocator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.67 ms | 1404 KiB | 15 Q