3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Benchmark; final class Measure { const MEMORY_VALUE = 0; const BENCHMARK_VALUE = 1; const BENCHMARK_AVERAGE = 2; const BENCHMARK_COUNT = 3; private $max, $memory, $trace; public function memoryTick() { $this->memory = memory_get_usage() - $this->memory; $this->max = $this->memory>$this->max?$this->memory:$this->max; $this->memory = memory_get_usage(); } public function profileTick() { $memory = memory_get_usage(); $this->trace[] = $memory-$this->memory; $this->memory = memory_get_usage(); } /** * * @param callable $function A function to be measured * @param array $args Parameters to be passed for measured function * @return array Result contains profile data per each line */ public function profileMemory(callable $function, array $args=array()) { declare(ticks=1); $this->memory = memory_get_usage(); $this->max = 0; $this->trace = array(); register_tick_function('call_user_func', array($this, 'profileTick')); $this->measureFunction($function, $args, 1); unregister_tick_function('call_user_func'); return $this->trace; } /** * * @param callable $function A function to be measured * @param array $args Parameters to be passed for measured function * @return array Result currently contains one value: used memory space */ public function benchmarkMemory(callable $function, array $args=array()) { declare(ticks=1); $this->memory = memory_get_usage(); $this->max = 0; register_tick_function('call_user_func', array($this, 'memoryTick')); $this->measureFunction($function, $args, 1); unregister_tick_function('call_user_func'); return array( self::MEMORY_VALUE => $this->max ); } /** * * @param callable $function A function to be measured * @param array $args Parameters to be passed for measured function * @param int $count Count of measurements * @return array Result currently contains: total time, average time and measurements count * @throws \InvalidArgumentException If measurements count is invalid */ public function benchmarkTime(callable $function, array $args=array(), $count=1) { return $this->benchmarkCustom('microtime', $function, array(1), $args, $count); } /** * * @param callable $benchmark Function which will do measurements * @param callable $function A function to be measured * @param array $benchmarkArgs Parameters to be passed for measurement function * @param array $functionArgs Parameters to be passed for measured function * @param int $count Count of measurements * @return array Result currently contains: total value, average value and measurements count * @throws \InvalidArgumentException If measurements count is invalid * @throws \LogicException If measurement function did not returned numeric value */ public function benchmarkCustom(callable $benchmark, callable $function, array $benchmarkArgs=array(), array $functionArgs=array(), $count=1) { if(!is_int($count) || $count <=0) { throw new \InvalidArgumentException('Count of measure times must be positive integer'); } $init = call_user_func_array($benchmark, $benchmarkArgs); if(!is_numeric($init)) { throw new \LogicException('Benchmark function must return valid numeric value'); } $this->measureFunction($function, $functionArgs, $count); $end = call_user_func_array($benchmark, $benchmarkArgs); return array( self::BENCHMARK_VALUE => $end - $init, self::BENCHMARK_AVERAGE => ($end - $init) / $count, self::BENCHMARK_COUNT => $count ); } private function measureFunction($function, $args, $count) { for($i=0; $i<$count; $i++) { call_user_func_array($function, $args); } } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ajOo5
function name:  (null)
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  113     0  E > > RETURN                                                   1

Class Benchmark\Measure:
Function memorytick:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ajOo5
function name:  memoryTick
number of ops:  22
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cmemory_get_usage'
          1        DO_FCALL                                      0  $1      
          2        FETCH_OBJ_R                                      ~2      'memory'
          3        SUB                                              ~3      $1, ~2
          4        ASSIGN_OBJ                                               'memory'
          5        OP_DATA                                                  ~3
   16     6        FETCH_OBJ_R                                      ~5      'memory'
          7        FETCH_OBJ_R                                      ~6      'max'
          8        IS_SMALLER                                               ~6, ~5
          9      > JMPZ                                                     ~7, ->13
         10    >   FETCH_OBJ_R                                      ~8      'memory'
         11        QM_ASSIGN                                        ~9      ~8
         12      > JMP                                                      ->15
         13    >   FETCH_OBJ_R                                      ~10     'max'
         14        QM_ASSIGN                                        ~9      ~10
         15    >   ASSIGN_OBJ                                               'max'
         16        OP_DATA                                                  ~9
   17    17        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cmemory_get_usage'
         18        DO_FCALL                                      0  $12     
         19        ASSIGN_OBJ                                               'memory'
         20        OP_DATA                                                  $12
   18    21      > RETURN                                                   null

End of function memorytick

Function profiletick:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ajOo5
function name:  profileTick
number of ops:  13
compiled vars:  !0 = $memory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cmemory_get_usage'
          1        DO_FCALL                                      0  $1      
          2        ASSIGN                                                   !0, $1
   23     3        FETCH_OBJ_R                                      ~5      'memory'
          4        SUB                                              ~6      !0, ~5
          5        FETCH_OBJ_W                                      $3      'trace'
          6        ASSIGN_DIM                                               $3
          7        OP_DATA                                                  ~6
   24     8        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cmemory_get_usage'
          9        DO_FCALL                                      0  $8      
         10        ASSIGN_OBJ                                               'memory'
         11        OP_DATA                                                  $8
   25    12      > RETURN                                                   null

End of function profiletick

Function profilememory:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ajOo5
function name:  profileMemory
number of ops:  36
compiled vars:  !0 = $function, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   34     2        TICKS                                                    
   35     3        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cmemory_get_usage'
          4        DO_FCALL                                      0  $3      
          5        ASSIGN_OBJ                                               'memory'
          6        OP_DATA                                                  $3
          7        TICKS                                                    
   36     8        ASSIGN_OBJ                                               'max'
          9        OP_DATA                                                  0
         10        TICKS                                                    
   37    11        ASSIGN_OBJ                                               'trace'
         12        OP_DATA                                                  <array>
         13        TICKS                                                    
   39    14        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cregister_tick_function'
         15        SEND_VAL_EX                                              'call_user_func'
         16        FETCH_THIS                                       ~6      
         17        INIT_ARRAY                                       ~7      ~6
         18        ADD_ARRAY_ELEMENT                                ~7      'profileTick'
         19        SEND_VAL_EX                                              ~7
         20        DO_FCALL                                      0          
         21        TICKS                                                    
   40    22        INIT_METHOD_CALL                                         'measureFunction'
         23        SEND_VAR_EX                                              !0
         24        SEND_VAR_EX                                              !1
         25        SEND_VAL_EX                                              1
         26        DO_FCALL                                      0          
         27        TICKS                                                    
   41    28        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cunregister_tick_function'
         29        SEND_VAL_EX                                              'call_user_func'
         30        DO_FCALL                                      0          
         31        TICKS                                                    
   42    32        FETCH_OBJ_R                                      ~11     'trace'
         33      > RETURN                                                   ~11
         34*       TICKS                                                    
   43    35*     > RETURN                                                   null

End of function profilememory

Function benchmarkmemory:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ajOo5
function name:  benchmarkMemory
number of ops:  35
compiled vars:  !0 = $function, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   52     2        TICKS                                                    
   53     3        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cmemory_get_usage'
          4        DO_FCALL                                      0  $3      
          5        ASSIGN_OBJ                                               'memory'
          6        OP_DATA                                                  $3
          7        TICKS                                                    
   54     8        ASSIGN_OBJ                                               'max'
          9        OP_DATA                                                  0
         10        TICKS                                                    
   56    11        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cregister_tick_function'
         12        SEND_VAL_EX                                              'call_user_func'
         13        FETCH_THIS                                       ~5      
         14        INIT_ARRAY                                       ~6      ~5
         15        ADD_ARRAY_ELEMENT                                ~6      'memoryTick'
         16        SEND_VAL_EX                                              ~6
         17        DO_FCALL                                      0          
         18        TICKS                                                    
   57    19        INIT_METHOD_CALL                                         'measureFunction'
         20        SEND_VAR_EX                                              !0
         21        SEND_VAR_EX                                              !1
         22        SEND_VAL_EX                                              1
         23        DO_FCALL                                      0          
         24        TICKS                                                    
   58    25        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cunregister_tick_function'
         26        SEND_VAL_EX                                              'call_user_func'
         27        DO_FCALL                                      0          
         28        TICKS                                                    
   60    29        FETCH_CLASS_CONSTANT                             ~10     'MEMORY_VALUE'
         30        FETCH_OBJ_R                                      ~11     'max'
         31        INIT_ARRAY                                       ~12     ~11, ~10
         32      > RETURN                                                   ~12
         33*       TICKS                                                    
   62    34*     > RETURN                                                   null

End of function benchmarkmemory

Function benchmarktime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ajOo5
function name:  benchmarkTime
number of ops:  13
compiled vars:  !0 = $function, !1 = $args, !2 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      1
   73     3        INIT_METHOD_CALL                                         'benchmarkCustom'
          4        SEND_VAL_EX                                              'microtime'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAL_EX                                              <array>
          7        SEND_VAR_EX                                              !1
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0  $3      
         10      > RETURN                                                   $3
         11*       TICKS                                                    
   74    12*     > RETURN                                                   null

End of function benchmarktime

Function benchmarkcustom:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/ajOo5
function name:  benchmarkCustom
number of ops:  58
compiled vars:  !0 = $benchmark, !1 = $function, !2 = $benchmarkArgs, !3 = $functionArgs, !4 = $count, !5 = $init, !6 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
          3        RECV_INIT                                        !3      <array>
          4        RECV_INIT                                        !4      1
   88     5        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cis_int'
          6        SEND_VAR_EX                                              !4
          7        DO_FCALL                                      0  $7      
          8        BOOL_NOT                                         ~8      $7
          9      > JMPNZ_EX                                         ~8      ~8, ->12
         10    >   IS_SMALLER_OR_EQUAL                              ~9      !4, 0
         11        BOOL                                             ~8      ~9
         12    > > JMPZ                                                     ~8, ->18
   90    13    >   NEW                                              $10     'InvalidArgumentException'
         14        SEND_VAL_EX                                              'Count+of+measure+times+must+be+positive+integer'
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $10
         17*       TICKS                                                    
   92    18    >   INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Ccall_user_func_array'
         19        SEND_VAR_EX                                              !0
         20        SEND_VAR_EX                                              !2
         21        DO_FCALL                                      0  $12     
         22        ASSIGN                                                   !5, $12
         23        TICKS                                                    
   93    24        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Cis_numeric'
         25        SEND_VAR_EX                                              !5
         26        DO_FCALL                                      0  $14     
         27        BOOL_NOT                                         ~15     $14
         28      > JMPZ                                                     ~15, ->34
   95    29    >   NEW                                              $16     'LogicException'
         30        SEND_VAL_EX                                              'Benchmark+function+must+return+valid+numeric+value'
         31        DO_FCALL                                      0          
         32      > THROW                                         0          $16
         33*       TICKS                                                    
   97    34    >   INIT_METHOD_CALL                                         'measureFunction'
         35        SEND_VAR_EX                                              !1
         36        SEND_VAR_EX                                              !3
         37        SEND_VAR_EX                                              !4
         38        DO_FCALL                                      0          
         39        TICKS                                                    
   98    40        INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Ccall_user_func_array'
         41        SEND_VAR_EX                                              !0
         42        SEND_VAR_EX                                              !2
         43        DO_FCALL                                      0  $19     
         44        ASSIGN                                                   !6, $19
         45        TICKS                                                    
  100    46        FETCH_CLASS_CONSTANT                             ~21     'BENCHMARK_VALUE'
         47        SUB                                              ~22     !6, !5
         48        INIT_ARRAY                                       ~23     ~22, ~21
  101    49        FETCH_CLASS_CONSTANT                             ~24     'BENCHMARK_AVERAGE'
         50        SUB                                              ~25     !6, !5
         51        DIV                                              ~26     ~25, !4
         52        ADD_ARRAY_ELEMENT                                ~23     ~26, ~24
  102    53        FETCH_CLASS_CONSTANT                             ~27     'BENCHMARK_COUNT'
         54        ADD_ARRAY_ELEMENT                                ~23     !4, ~27
         55      > RETURN                                                   ~23
         56*       TICKS                                                    
  104    57*     > RETURN                                                   null

End of function benchmarkcustom

Function measurefunction:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 5
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 5
Branch analysis from position: 13
Branch analysis from position: 5
filename:       /in/ajOo5
function name:  measureFunction
number of ops:  15
compiled vars:  !0 = $function, !1 = $args, !2 = $count, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  106     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
  108     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->11
  110     5    >   INIT_NS_FCALL_BY_NAME                                    'Benchmark%5Ccall_user_func_array'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        TICKS                                                    
  108    10        PRE_INC                                                  !3
         11    >   IS_SMALLER                                               !3, !2
         12      > JMPNZ                                                    ~7, ->5
         13    >   TICKS                                                    
  112    14      > RETURN                                                   null

End of function measurefunction

End of class Benchmark\Measure.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.18 ms | 1416 KiB | 25 Q