3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Benchmark { private int $startTime; private int $endTime; public function start() { $this->startTime = hrtime(true); } public function end() { $this->endTime = hrtime(true); } public function getElapsedTime() { return round(($this->endTime - $this->startTime) / 1_000_000_000, 8); } } // Example usage $benchmark = new Benchmark(); // ==================================== // PREPARE TEST DATA function getData():array { return $people = array( array('name' => 'Kalle', 'salt' => 856412), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863), array('name' => 'Pierre', 'salt' => 215863) ); } // ==================================== $memory = memory_get_usage(); echo "Initial memory usage: " . ($memory / 1024) . " KB\n"; $benchmark->start(); // Code to benchmark $people = getData(); for ($benchIndex = 0; $benchIndex < 50000; $benchIndex++) { for($i = 0; $i < count($people); ++$i) { $people[$i]['salt'] = rand(1, 1000000); } // for($j = 0, $size = count($people); $j < $size; ++$j) { // $people[$j]['salt'] = rand(1, 1000000); // } } $benchmark->end(); $time1 = $benchmark->getElapsedTime(); echo "Elapsed time: " . $time1 . " seconds\n"; $memory = memory_get_usage(); echo "Memory usage after test 1: " . ($memory / 1024) . " KB\n"; echo "-----------------------------------\n"; $people = array( array('name' => 'Kalle', 'salt' => 856412), array('name' => 'Pierre', 'salt' => 215863) ); $memory = memory_get_usage(); echo "Initial memory usage: " . ($memory / 1024) . " KB\n"; $benchmark->start(); // Code to benchmark $people = getData(); for ($benchIndex = 0; $benchIndex < 50000; $benchIndex++) { for($j = 0, $size = count($people); $j < $size; ++$j) { $people[$j]['salt'] = rand(1, 1000000); } // for($i = 0; $i < count($people); ++$i) { // $people[$i]['salt'] = rand(1, 1000000); // } } $benchmark->end(); $time2 = $benchmark->getElapsedTime(); echo "Elapsed time: " . $time2 . " seconds\n"; $memory = memory_get_usage(); echo "Memory usage after test 2: " . ($memory / 1024) . " KB\n"; echo "Times faster: " . round($time1 / $time2, 2) . "x\n"; // assert($dates1 === $dates2, 'The two methods should produce the same result.' . var_export($dates1, true) . ' !== ' . var_export($dates2, true));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 17
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
2 jumps found. (Code = 44) Position 1 = 81, Position 2 = 64
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
2 jumps found. (Code = 44) Position 1 = 78, Position 2 = 68
Branch analysis from position: 78
2 jumps found. (Code = 44) Position 1 = 81, Position 2 = 64
Branch analysis from position: 81
Branch analysis from position: 64
Branch analysis from position: 68
2 jumps found. (Code = 44) Position 1 = 78, Position 2 = 68
Branch analysis from position: 78
Branch analysis from position: 68
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 19
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 17
Branch analysis from position: 33
Branch analysis from position: 17
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 19
Branch analysis from position: 30
Branch analysis from position: 19
filename:       /in/MsbDb
function name:  (null)
number of ops:  105
compiled vars:  !0 = $benchmark, !1 = $memory, !2 = $people, !3 = $benchIndex, !4 = $i, !5 = $time1, !6 = $j, !7 = $size, !8 = $time2
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   NEW                                                  $9      'Benchmark'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $9
  122     3        INIT_FCALL                                                   'memory_get_usage'
          4        DO_ICALL                                             $12     
          5        ASSIGN                                                       !1, $12
  123     6        DIV                                                  ~14     !1, 1024
          7        CONCAT                                               ~15     'Initial+memory+usage%3A+', ~14
          8        CONCAT                                               ~16     ~15, '+KB%0A'
          9        ECHO                                                         ~16
  124    10        INIT_METHOD_CALL                                             !0, 'start'
         11        DO_FCALL                                          0          
  126    12        INIT_FCALL                                                   'getdata'
         13        DO_FCALL                                          0  $18     
         14        ASSIGN                                                       !2, $18
  127    15        ASSIGN                                                       !3, 0
         16      > JMP                                                          ->31
  128    17    >   ASSIGN                                                       !4, 0
         18      > JMP                                                          ->27
  129    19    >   INIT_FCALL                                                   'rand'
         20        SEND_VAL                                                     1
         21        SEND_VAL                                                     1000000
         22        DO_ICALL                                             $24     
         23        FETCH_DIM_W                                          $22     !2, !4
         24        ASSIGN_DIM                                                   $22, 'salt'
         25        OP_DATA                                                      $24
  128    26        PRE_INC                                                      !4
         27    >   COUNT                                                ~26     !2
         28        IS_SMALLER                                                   !4, ~26
         29      > JMPNZ                                                        ~27, ->19
  127    30    >   PRE_INC                                                      !3
         31    >   IS_SMALLER                                                   !3, 50000
         32      > JMPNZ                                                        ~29, ->17
  136    33    >   INIT_METHOD_CALL                                             !0, 'end'
         34        DO_FCALL                                          0          
  137    35        INIT_METHOD_CALL                                             !0, 'getElapsedTime'
         36        DO_FCALL                                          0  $31     
         37        ASSIGN                                                       !5, $31
  138    38        CONCAT                                               ~33     'Elapsed+time%3A+', !5
         39        CONCAT                                               ~34     ~33, '+seconds%0A'
         40        ECHO                                                         ~34
  139    41        INIT_FCALL                                                   'memory_get_usage'
         42        DO_ICALL                                             $35     
         43        ASSIGN                                                       !1, $35
  140    44        DIV                                                  ~37     !1, 1024
         45        CONCAT                                               ~38     'Memory+usage+after+test+1%3A+', ~37
         46        CONCAT                                               ~39     ~38, '+KB%0A'
         47        ECHO                                                         ~39
  142    48        ECHO                                                         '-----------------------------------%0A'
  144    49        ASSIGN                                                       !2, <array>
  149    50        INIT_FCALL                                                   'memory_get_usage'
         51        DO_ICALL                                             $41     
         52        ASSIGN                                                       !1, $41
  150    53        DIV                                                  ~43     !1, 1024
         54        CONCAT                                               ~44     'Initial+memory+usage%3A+', ~43
         55        CONCAT                                               ~45     ~44, '+KB%0A'
         56        ECHO                                                         ~45
  151    57        INIT_METHOD_CALL                                             !0, 'start'
         58        DO_FCALL                                          0          
  153    59        INIT_FCALL                                                   'getdata'
         60        DO_FCALL                                          0  $47     
         61        ASSIGN                                                       !2, $47
  154    62        ASSIGN                                                       !3, 0
         63      > JMP                                                          ->79
  155    64    >   ASSIGN                                                       !6, 0
         65        COUNT                                                ~51     !2
         66        ASSIGN                                                       !7, ~51
         67      > JMP                                                          ->76
  156    68    >   INIT_FCALL                                                   'rand'
         69        SEND_VAL                                                     1
         70        SEND_VAL                                                     1000000
         71        DO_ICALL                                             $55     
         72        FETCH_DIM_W                                          $53     !2, !6
         73        ASSIGN_DIM                                                   $53, 'salt'
         74        OP_DATA                                                      $55
  155    75        PRE_INC                                                      !6
         76    >   IS_SMALLER                                                   !6, !7
         77      > JMPNZ                                                        ~57, ->68
  154    78    >   PRE_INC                                                      !3
         79    >   IS_SMALLER                                                   !3, 50000
         80      > JMPNZ                                                        ~59, ->64
  163    81    >   INIT_METHOD_CALL                                             !0, 'end'
         82        DO_FCALL                                          0          
  164    83        INIT_METHOD_CALL                                             !0, 'getElapsedTime'
         84        DO_FCALL                                          0  $61     
         85        ASSIGN                                                       !8, $61
  165    86        CONCAT                                               ~63     'Elapsed+time%3A+', !8
         87        CONCAT                                               ~64     ~63, '+seconds%0A'
         88        ECHO                                                         ~64
  166    89        INIT_FCALL                                                   'memory_get_usage'
         90        DO_ICALL                                             $65     
         91        ASSIGN                                                       !1, $65
  167    92        DIV                                                  ~67     !1, 1024
         93        CONCAT                                               ~68     'Memory+usage+after+test+2%3A+', ~67
         94        CONCAT                                               ~69     ~68, '+KB%0A'
         95        ECHO                                                         ~69
  168    96        INIT_FCALL                                                   'round'
         97        DIV                                                  ~70     !5, !8
         98        SEND_VAL                                                     ~70
         99        SEND_VAL                                                     2
        100        DO_ICALL                                             $71     
        101        CONCAT                                               ~72     'Times+faster%3A+', $71
        102        CONCAT                                               ~73     ~72, 'x%0A'
        103        ECHO                                                         ~73
  171   104      > RETURN                                                       1

Function getdata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MsbDb
function name:  getData
number of ops:  5
compiled vars:  !0 = $people
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   ASSIGN                                               ~1      !0, <array>
          1        VERIFY_RETURN_TYPE                                           ~1
          2      > RETURN                                                       ~1
  118     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function getdata

Class Benchmark:
Function start:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MsbDb
function name:  start
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   INIT_FCALL                                                   'hrtime'
          1        SEND_VAL                                                     <true>
          2        DO_ICALL                                             $1      
          3        ASSIGN_OBJ                                                   'startTime'
          4        OP_DATA                                                      $1
   12     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/MsbDb
function name:  end
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   16     0  E >   INIT_FCALL                                                   'hrtime'
          1        SEND_VAL                                                     <true>
          2        DO_ICALL                                             $1      
          3        ASSIGN_OBJ                                                   'endTime'
          4        OP_DATA                                                      $1
   17     5      > RETURN                                                       null

End of function end

Function getelapsedtime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MsbDb
function name:  getElapsedTime
number of ops:  10
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                                   'round'
          1        FETCH_OBJ_R                                          ~0      'endTime'
          2        FETCH_OBJ_R                                          ~1      'startTime'
          3        SUB                                                  ~2      ~0, ~1
          4        DIV                                                  ~3      ~2, 1000000000
          5        SEND_VAL                                                     ~3
          6        SEND_VAL                                                     8
          7        DO_ICALL                                             $4      
          8      > RETURN                                                       $4
   22     9*     > RETURN                                                       null

End of function getelapsedtime

End of class Benchmark.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
166.74 ms | 1446 KiB | 19 Q