3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Benchmark: Reflection Performance * * Conclusion: there is no performance-gain from caching reflection-objects. */ define('NUM_TESTS', 10); header('Content-type: text/plain'); $func = function($a, $b, $c) { // ... }; class Foo { public $a; protected $b; private $c; public function foo($a,$b,$c) {} protected function bar($a,$b,$c) {} private function baz($a,$b,$c) {} } for ($i=0; $i<NUM_TESTS; $i++) { $start = microtime(true); $ref = new ReflectionClass($func); $end = microtime(true); echo "ReflectionClass # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n"; } for ($i=0; $i<NUM_TESTS; $i++) { $start = microtime(true); $ref = new ReflectionFunction($func); $end = microtime(true); echo "ReflectionFunction # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n"; } class Cache { private $cache = array(); public function getReflection($class) { if (!isset($this->cache[$class])) { $this->cache[$class] = new ReflectionClass($class); } return $this->cache[$class]; } } $cache = new Cache; for ($i=0; $i<NUM_TESTS; $i++) { $start = microtime(true); $ref = $cache->getReflection('Foo'); $end = microtime(true); echo "Cached ReflectionClass # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 11
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 41
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 99
Branch analysis from position: 99
2 jumps found. (Code = 44) Position 1 = 102, Position 2 = 74
Branch analysis from position: 102
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
2 jumps found. (Code = 44) Position 1 = 102, Position 2 = 74
Branch analysis from position: 102
Branch analysis from position: 74
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 41
Branch analysis from position: 69
Branch analysis from position: 41
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 11
Branch analysis from position: 39
Branch analysis from position: 11
filename:       /in/3r021
function name:  (null)
number of ops:  103
compiled vars:  !0 = $func, !1 = $i, !2 = $start, !3 = $ref, !4 = $end, !5 = $cache
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'NUM_TESTS'
          2        SEND_VAL                                                 10
          3        DO_ICALL                                                 
   11     4        INIT_FCALL                                               'header'
          5        SEND_VAL                                                 'Content-type%3A+text%2Fplain'
          6        DO_ICALL                                                 
   13     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F3r021%3A13%240'
          8        ASSIGN                                                   !0, ~8
   27     9        ASSIGN                                                   !1, 0
         10      > JMP                                                      ->36
   28    11    >   INIT_FCALL                                               'microtime'
         12        SEND_VAL                                                 <true>
         13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !2, $11
   29    15        NEW                                              $13     'ReflectionClass'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0          
         18        ASSIGN                                                   !3, $13
   30    19        INIT_FCALL                                               'microtime'
         20        SEND_VAL                                                 <true>
         21        DO_ICALL                                         $16     
         22        ASSIGN                                                   !4, $16
   31    23        ROPE_INIT                                     3  ~19     'ReflectionClass+%23+'
         24        ROPE_ADD                                      1  ~19     ~19, !1
         25        ROPE_END                                      2  ~18     ~19, '%3A+'
         26        INIT_FCALL                                               'number_format'
         27        SUB                                              ~21     !4, !2
         28        MUL                                              ~22     ~21, 1000000
         29        SEND_VAL                                                 ~22
         30        SEND_VAL                                                 3
         31        DO_ICALL                                         $23     
         32        CONCAT                                           ~24     ~18, $23
         33        CONCAT                                           ~25     ~24, '+%C2%B5sec%0A'
         34        ECHO                                                     ~25
   27    35        PRE_INC                                                  !1
         36    >   FETCH_CONSTANT                                   ~27     'NUM_TESTS'
         37        IS_SMALLER                                               !1, ~27
         38      > JMPNZ                                                    ~28, ->11
   34    39    >   ASSIGN                                                   !1, 0
         40      > JMP                                                      ->66
   35    41    >   INIT_FCALL                                               'microtime'
         42        SEND_VAL                                                 <true>
         43        DO_ICALL                                         $30     
         44        ASSIGN                                                   !2, $30
   36    45        NEW                                              $32     'ReflectionFunction'
         46        SEND_VAR_EX                                              !0
         47        DO_FCALL                                      0          
         48        ASSIGN                                                   !3, $32
   37    49        INIT_FCALL                                               'microtime'
         50        SEND_VAL                                                 <true>
         51        DO_ICALL                                         $35     
         52        ASSIGN                                                   !4, $35
   38    53        ROPE_INIT                                     3  ~38     'ReflectionFunction+%23+'
         54        ROPE_ADD                                      1  ~38     ~38, !1
         55        ROPE_END                                      2  ~37     ~38, '%3A+'
         56        INIT_FCALL                                               'number_format'
         57        SUB                                              ~40     !4, !2
         58        MUL                                              ~41     ~40, 1000000
         59        SEND_VAL                                                 ~41
         60        SEND_VAL                                                 3
         61        DO_ICALL                                         $42     
         62        CONCAT                                           ~43     ~37, $42
         63        CONCAT                                           ~44     ~43, '+%C2%B5sec%0A'
         64        ECHO                                                     ~44
   34    65        PRE_INC                                                  !1
         66    >   FETCH_CONSTANT                                   ~46     'NUM_TESTS'
         67        IS_SMALLER                                               !1, ~46
         68      > JMPNZ                                                    ~47, ->41
   53    69    >   NEW                                              $48     'Cache'
         70        DO_FCALL                                      0          
         71        ASSIGN                                                   !5, $48
   55    72        ASSIGN                                                   !1, 0
         73      > JMP                                                      ->99
   56    74    >   INIT_FCALL                                               'microtime'
         75        SEND_VAL                                                 <true>
         76        DO_ICALL                                         $52     
         77        ASSIGN                                                   !2, $52
   57    78        INIT_METHOD_CALL                                         !5, 'getReflection'
         79        SEND_VAL_EX                                              'Foo'
         80        DO_FCALL                                      0  $54     
         81        ASSIGN                                                   !3, $54
   58    82        INIT_FCALL                                               'microtime'
         83        SEND_VAL                                                 <true>
         84        DO_ICALL                                         $56     
         85        ASSIGN                                                   !4, $56
   59    86        ROPE_INIT                                     3  ~59     'Cached+ReflectionClass+%23+'
         87        ROPE_ADD                                      1  ~59     ~59, !1
         88        ROPE_END                                      2  ~58     ~59, '%3A+'
         89        INIT_FCALL                                               'number_format'
         90        SUB                                              ~61     !4, !2
         91        MUL                                              ~62     ~61, 1000000
         92        SEND_VAL                                                 ~62
         93        SEND_VAL                                                 3
         94        DO_ICALL                                         $63     
         95        CONCAT                                           ~64     ~58, $63
         96        CONCAT                                           ~65     ~64, '+%C2%B5sec%0A'
         97        ECHO                                                     ~65
   55    98        PRE_INC                                                  !1
         99    >   FETCH_CONSTANT                                   ~67     'NUM_TESTS'
        100        IS_SMALLER                                               !1, ~67
        101      > JMPNZ                                                    ~68, ->74
   60   102    > > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F3r021%3A13%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3r021
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   15     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F3r021%3A13%240

Class Foo:
Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3r021
function name:  foo
number of ops:  4
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3      > RETURN                                                   null

End of function foo

Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3r021
function name:  bar
number of ops:  4
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3      > RETURN                                                   null

End of function bar

Function baz:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3r021
function name:  baz
number of ops:  4
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3      > RETURN                                                   null

End of function baz

End of class Foo.

Class Cache:
Function getreflection:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/3r021
function name:  getReflection
number of ops:  15
compiled vars:  !0 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        FETCH_OBJ_IS                                     ~1      'cache'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->11
   47     5    >   NEW                                              $6      'ReflectionClass'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        FETCH_OBJ_W                                      $4      'cache'
          9        ASSIGN_DIM                                               $4, !0
         10        OP_DATA                                                  $6
   49    11    >   FETCH_OBJ_R                                      ~8      'cache'
         12        FETCH_DIM_R                                      ~9      ~8, !0
         13      > RETURN                                                   ~9
   50    14*     > RETURN                                                   null

End of function getreflection

End of class Cache.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.64 ms | 1408 KiB | 21 Q