3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); mb_internal_encoding('UTF-8'); mb_http_output('UTF-8'); gc_enable(); class Ubench { protected $start_time; protected $end_time; protected $memory_usage; /** * Sets start microtime * * @return void */ public function start() { $this->start_time = microtime(true); } /** * Sets end microtime * * @return void */ public function end() { $this->end_time = microtime(true); $this->memory_usage = memory_get_usage(true); } /** * Returns the elapsed time, readable or not * * @param boolean $readable Whether the result must be human readable * @param string $format The format to display (printf format) * @return string|float */ public function getTime($raw = false, $format = null) { $elapsed = $this->end_time - $this->start_time; return $raw ? $elapsed : self::readableElapsedTime($elapsed, $format); } /** * Returns the memory usage at the end checkpoint * * @param boolean $readable Whether the result must be human readable * @param string $format The format to display (printf format) * @return string|float */ public function getMemoryUsage($raw = false, $format = null) { return $raw ? $this->memory_usage : self::readableSize($this->memory_usage, $format); } /** * Returns the memory peak, readable or not * * @param boolean $readable Whether the result must be human readable * @param string $format The format to display (printf format) * @return string|float */ public function getMemoryPeak($raw = false, $format = null) { $memory = memory_get_peak_usage(true); return $raw ? $memory : self::readableSize($memory, $format); } /** * Wraps a callable with start() and end() calls * * Additional arguments passed to this method will be passed to * the callable. * * @param callable $callable * @return mixed */ public function run(callable $callable) { $arguments = func_get_args(); array_shift($arguments); $this->start(); $result = call_user_func_array($callable, $arguments); $this->end(); return $result; } /** * Returns a human readable memory size * * @param int $size * @param string $format The format to display (printf format) * @param int $round * @return string */ public static function readableSize($size, $format = null, $round = 3) { $mod = 1024; if (is_null($format)) { $format = '%.2f%s'; } $units = explode(' ','B Kb Mb Gb Tb'); for ($i = 0; $size > $mod; $i++) { $size /= $mod; } if (0 === $i) { $format = preg_replace('/(%.[\d]+f)/', '%d', $format); } return sprintf($format, round($size, $round), $units[$i]); } /** * Returns a human readable elapsed time * * @param float $microtime * @param string $format The format to display (printf format) * @return string */ public static function readableElapsedTime($microtime, $format = null, $round = 3) { if (is_null($format)) { $format = '%.3f%s'; } if ($microtime >= 1) { $unit = 's'; $time = round($microtime, $round); } else { $unit = 'ms'; $time = round($microtime*1000); $format = preg_replace('/(%.[\d]+f)/', '%d', $format); } return sprintf($format, $time, $unit); } } $bench = new Ubench; $testSum = 0; $bench->start(); // Execute some code for ($i = 0; $i < 1000000; $i++) { $j= $i; } $bench->end(); // Get elapsed time and memory echo "[" . $bench->getTime(); echo " | " . $bench->getTime(true); // elapsed microtime in float echo " | " . $bench->getTime(false, '%d%s') . "]"; echo "\r\n[" . $bench->getMemoryPeak(); echo " | " . $bench->getMemoryPeak(true); // memory peak in bytes echo " | " . $bench->getMemoryPeak(false, '%.3f%s') . "]"; // Returns the memory usage at the end mark echo "\r\n---\r\n" . $bench->getMemoryUsage(); exit(); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 27
Branch analysis from position: 31
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 27
Branch analysis from position: 31
Branch analysis from position: 27
filename:       /in/COXWa
function name:  (null)
number of ops:  71
compiled vars:  !0 = $bench, !1 = $testSum, !2 = $i, !3 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 -1
          2        DO_ICALL                                                 
    4     3        INIT_FCALL                                               'ini_set'
          4        SEND_VAL                                                 'display_errors'
          5        SEND_VAL                                                 '1'
          6        DO_ICALL                                                 
    5     7        INIT_FCALL                                               'ini_set'
          8        SEND_VAL                                                 'display_startup_errors'
          9        SEND_VAL                                                 '1'
         10        DO_ICALL                                                 
    6    11        INIT_FCALL                                               'mb_internal_encoding'
         12        SEND_VAL                                                 'UTF-8'
         13        DO_ICALL                                                 
    7    14        INIT_FCALL                                               'mb_http_output'
         15        SEND_VAL                                                 'UTF-8'
         16        DO_ICALL                                                 
    8    17        INIT_FCALL                                               'gc_enable'
         18        DO_ICALL                                                 
  156    19        NEW                                              $10     'Ubench'
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !0, $10
  157    22        ASSIGN                                                   !1, 0
  158    23        INIT_METHOD_CALL                                         !0, 'start'
         24        DO_FCALL                                      0          
  160    25        ASSIGN                                                   !2, 0
         26      > JMP                                                      ->29
         27    >   ASSIGN                                                   !3, !2
         28        PRE_INC                                                  !2
         29    >   IS_SMALLER                                               !2, 1000000
         30      > JMPNZ                                                    ~18, ->27
  161    31    >   INIT_METHOD_CALL                                         !0, 'end'
         32        DO_FCALL                                      0          
  164    33        INIT_METHOD_CALL                                         !0, 'getTime'
         34        DO_FCALL                                      0  $20     
         35        CONCAT                                           ~21     '%5B', $20
         36        ECHO                                                     ~21
  165    37        INIT_METHOD_CALL                                         !0, 'getTime'
         38        SEND_VAL_EX                                              <true>
         39        DO_FCALL                                      0  $22     
         40        CONCAT                                           ~23     '+%7C+', $22
         41        ECHO                                                     ~23
  166    42        INIT_METHOD_CALL                                         !0, 'getTime'
         43        SEND_VAL_EX                                              <false>
         44        SEND_VAL_EX                                              '%25d%25s'
         45        DO_FCALL                                      0  $24     
         46        CONCAT                                           ~25     '+%7C+', $24
         47        CONCAT                                           ~26     ~25, '%5D'
         48        ECHO                                                     ~26
  168    49        INIT_METHOD_CALL                                         !0, 'getMemoryPeak'
         50        DO_FCALL                                      0  $27     
         51        CONCAT                                           ~28     '%0D%0A%5B', $27
         52        ECHO                                                     ~28
  169    53        INIT_METHOD_CALL                                         !0, 'getMemoryPeak'
         54        SEND_VAL_EX                                              <true>
         55        DO_FCALL                                      0  $29     
         56        CONCAT                                           ~30     '+%7C+', $29
         57        ECHO                                                     ~30
  170    58        INIT_METHOD_CALL                                         !0, 'getMemoryPeak'
         59        SEND_VAL_EX                                              <false>
         60        SEND_VAL_EX                                              '%25.3f%25s'
         61        DO_FCALL                                      0  $31     
         62        CONCAT                                           ~32     '+%7C+', $31
         63        CONCAT                                           ~33     ~32, '%5D'
         64        ECHO                                                     ~33
  173    65        INIT_METHOD_CALL                                         !0, 'getMemoryUsage'
         66        DO_FCALL                                      0  $34     
         67        CONCAT                                           ~35     '%0D%0A---%0D%0A', $34
         68        ECHO                                                     ~35
  175    69      > EXIT                                                     
  177    70*     > RETURN                                                   1

Class Ubench:
Function start:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/COXWa
function name:  start
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'microtime'
          1        SEND_VAL                                                 <true>
          2        DO_ICALL                                         $1      
          3        ASSIGN_OBJ                                               'start_time'
          4        OP_DATA                                                  $1
   26     5      > RETURN                                                   null

End of function start

Function end:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/COXWa
function name:  end
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   INIT_FCALL                                               'microtime'
          1        SEND_VAL                                                 <true>
          2        DO_ICALL                                         $1      
          3        ASSIGN_OBJ                                               'end_time'
          4        OP_DATA                                                  $1
   36     5        INIT_FCALL                                               'memory_get_usage'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $3      
          8        ASSIGN_OBJ                                               'memory_usage'
          9        OP_DATA                                                  $3
   37    10      > RETURN                                                   null

End of function end

Function gettime:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/COXWa
function name:  getTime
number of ops:  16
compiled vars:  !0 = $raw, !1 = $format, !2 = $elapsed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV_INIT                                        !0      <false>
          1        RECV_INIT                                        !1      null
   48     2        FETCH_OBJ_R                                      ~3      'end_time'
          3        FETCH_OBJ_R                                      ~4      'start_time'
          4        SUB                                              ~5      ~3, ~4
          5        ASSIGN                                                   !2, ~5
   50     6      > JMPZ                                                     !0, ->9
          7    >   QM_ASSIGN                                        ~7      !2
          8      > JMP                                                      ->14
          9    >   INIT_STATIC_METHOD_CALL                                  'readableElapsedTime'
         10        SEND_VAR_EX                                              !2
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $8      
         13        QM_ASSIGN                                        ~7      $8
         14    > > RETURN                                                   ~7
   51    15*     > RETURN                                                   null

End of function gettime

Function getmemoryusage:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/COXWa
function name:  getMemoryUsage
number of ops:  15
compiled vars:  !0 = $raw, !1 = $format
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV_INIT                                        !0      <false>
          1        RECV_INIT                                        !1      null
   62     2      > JMPZ                                                     !0, ->6
          3    >   FETCH_OBJ_R                                      ~2      'memory_usage'
          4        QM_ASSIGN                                        ~3      ~2
          5      > JMP                                                      ->13
          6    >   INIT_STATIC_METHOD_CALL                                  'readableSize'
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $4      'memory_usage'
          9        SEND_FUNC_ARG                                            $4
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $5      
         12        QM_ASSIGN                                        ~3      $5
         13    > > RETURN                                                   ~3
   63    14*     > RETURN                                                   null

End of function getmemoryusage

Function getmemorypeak:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/COXWa
function name:  getMemoryPeak
number of ops:  16
compiled vars:  !0 = $raw, !1 = $format, !2 = $memory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV_INIT                                        !0      <false>
          1        RECV_INIT                                        !1      null
   74     2        INIT_FCALL                                               'memory_get_peak_usage'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !2, $3
   76     6      > JMPZ                                                     !0, ->9
          7    >   QM_ASSIGN                                        ~5      !2
          8      > JMP                                                      ->14
          9    >   INIT_STATIC_METHOD_CALL                                  'readableSize'
         10        SEND_VAR_EX                                              !2
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $6      
         13        QM_ASSIGN                                        ~5      $6
         14    > > RETURN                                                   ~5
   77    15*     > RETURN                                                   null

End of function getmemorypeak

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/COXWa
function name:  run
number of ops:  17
compiled vars:  !0 = $callable, !1 = $arguments, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
   90     1        FUNC_GET_ARGS                                    ~3      
          2        ASSIGN                                                   !1, ~3
   91     3        INIT_FCALL                                               'array_shift'
          4        SEND_REF                                                 !1
          5        DO_ICALL                                                 
   93     6        INIT_METHOD_CALL                                         'start'
          7        DO_FCALL                                      0          
   94     8        INIT_USER_CALL                                0          'call_user_func_array', !0
          9        SEND_ARRAY                                               !1
         10        CHECK_UNDEF_ARGS                                         
         11        DO_FCALL                                      1  $7      
         12        ASSIGN                                                   !2, $7
   95    13        INIT_METHOD_CALL                                         'end'
         14        DO_FCALL                                      0          
   97    15      > RETURN                                                   !2
   98    16*     > RETURN                                                   null

End of function run

Function readablesize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 14
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 14
Branch analysis from position: 18
Branch analysis from position: 14
Branch analysis from position: 7
filename:       /in/COXWa
function name:  readableSize
number of ops:  38
compiled vars:  !0 = $size, !1 = $format, !2 = $round, !3 = $mod, !4 = $units, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  108     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      3
  110     3        ASSIGN                                                   !3, 1024
  112     4        TYPE_CHECK                                    2          !1
          5      > JMPZ                                                     ~7, ->7
  113     6    >   ASSIGN                                                   !1, '%25.2f%25s'
  116     7    >   INIT_FCALL                                               'explode'
          8        SEND_VAL                                                 '+'
          9        SEND_VAL                                                 'B+Kb+Mb+Gb+Tb'
         10        DO_ICALL                                         $9      
         11        ASSIGN                                                   !4, $9
  118    12        ASSIGN                                                   !5, 0
         13      > JMP                                                      ->16
  119    14    >   ASSIGN_OP                                     4          !0, !3
  118    15        PRE_INC                                                  !5
         16    >   IS_SMALLER                                               !3, !0
         17      > JMPNZ                                                    ~14, ->14
  122    18    >   IS_IDENTICAL                                             !5, 0
         19      > JMPZ                                                     ~15, ->26
  123    20    >   INIT_FCALL                                               'preg_replace'
         21        SEND_VAL                                                 '%2F%28%25.%5B%5Cd%5D%2Bf%29%2F'
         22        SEND_VAL                                                 '%25d'
         23        SEND_VAR                                                 !1
         24        DO_ICALL                                         $16     
         25        ASSIGN                                                   !1, $16
  126    26    >   INIT_FCALL                                               'sprintf'
         27        SEND_VAR                                                 !1
         28        INIT_FCALL                                               'round'
         29        SEND_VAR                                                 !0
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $18     
         32        SEND_VAR                                                 $18
         33        FETCH_DIM_R                                      ~19     !4, !5
         34        SEND_VAL                                                 ~19
         35        DO_ICALL                                         $20     
         36      > RETURN                                                   $20
  127    37*     > RETURN                                                   null

End of function readablesize

Function readableelapsedtime:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/COXWa
function name:  readableElapsedTime
number of ops:  34
compiled vars:  !0 = $microtime, !1 = $format, !2 = $round, !3 = $unit, !4 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      3
  138     3        TYPE_CHECK                                    2          !1
          4      > JMPZ                                                     ~5, ->6
  139     5    >   ASSIGN                                                   !1, '%25.3f%25s'
  142     6    >   IS_SMALLER_OR_EQUAL                                      1, !0
          7      > JMPZ                                                     ~7, ->15
  143     8    >   ASSIGN                                                   !3, 's'
  144     9        INIT_FCALL                                               'round'
         10        SEND_VAR                                                 !0
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $9      
         13        ASSIGN                                                   !4, $9
  142    14      > JMP                                                      ->27
  146    15    >   ASSIGN                                                   !3, 'ms'
  147    16        INIT_FCALL                                               'round'
         17        MUL                                              ~12     !0, 1000
         18        SEND_VAL                                                 ~12
         19        DO_ICALL                                         $13     
         20        ASSIGN                                                   !4, $13
  149    21        INIT_FCALL                                               'preg_replace'
         22        SEND_VAL                                                 '%2F%28%25.%5B%5Cd%5D%2Bf%29%2F'
         23        SEND_VAL                                                 '%25d'
         24        SEND_VAR                                                 !1
         25        DO_ICALL                                         $15     
         26        ASSIGN                                                   !1, $15
  152    27    >   INIT_FCALL                                               'sprintf'
         28        SEND_VAR                                                 !1
         29        SEND_VAR                                                 !4
         30        SEND_VAR                                                 !3
         31        DO_ICALL                                         $17     
         32      > RETURN                                                   $17
  153    33*     > RETURN                                                   null

End of function readableelapsedtime

End of class Ubench.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.64 ms | 1040 KiB | 26 Q