3v4l.org

run code in 300+ PHP versions simultaneously
<?php function returnTime(callable $function) { $count = 100; $repeat = 100; $tests = []; for ($i = 0; $i < $repeat; $i++) { $tests[$i] = []; for ($i = 0; $i < $count; $i++) { $startTime = microtime(true); $function(); $endTime = microtime(true); $tests[$i][] = $endTime - $startTime; } } foreach ($tests as &$test) $test = array_sum($test); return array_sum($tests) / $repeat; } function compareResults($result1, $name1, $result2, $name2) { $results = [ $result1, $result2, ]; $names = [ $name1, $name2, ]; if ($results[0] == $results[1]) { return 0; } if ($results[0] < $results[1]) { $fasterIndex = 0; } else { $fasterIndex = 1; } $slowerIndex = 1 - $fasterIndex; $fasterResult = $results[$fasterIndex]; $slowerResult = $results[$slowerIndex]; echo "{$names[$fasterIndex]} performs faster than {$names[$slowerIndex]}.", PHP_EOL; echo "It is ", round(($slowerResult / $fasterResult) * 100 - 100), "% faster.", PHP_EOL; } // Array with 500000 elements $array = array_fill(0, 500000, 1); compareResults( returnTime(function () use ($array) { end($array); $lastKey = key($array); reset($array); }), "end()", returnTime(function () use ($array) { $lastKey = key(array_slice($array, -1, 1, true)); }), "array_slice()" );

Abusive script

This script was stopped while abusing our resources

Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17XLq
function name:  (null)
number of ops:  23
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   INIT_FCALL                                               'array_fill'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 500000
          3        SEND_VAL                                                 1
          4        DO_ICALL                                         $1      
          5        ASSIGN                                                   !0, $1
   55     6        INIT_FCALL                                               'compareresults'
   56     7        INIT_FCALL                                               'returntime'
          8        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
          9        BIND_LEXICAL                                             ~3, !0
   60    10        SEND_VAL                                                 ~3
   56    11        DO_FCALL                                      0  $4      
   60    12        SEND_VAR                                                 $4
   61    13        SEND_VAL                                                 'end%28%29'
   63    14        INIT_FCALL                                               'returntime'
         15        DECLARE_LAMBDA_FUNCTION                          ~5      [1]
         16        BIND_LEXICAL                                             ~5, !0
   65    17        SEND_VAL                                                 ~5
   63    18        DO_FCALL                                      0  $6      
   65    19        SEND_VAR                                                 $6
   66    20        SEND_VAL                                                 'array_slice%28%29'
   55    21        DO_FCALL                                      0          
   67    22      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17XLq
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $array, !1 = $lastKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   BIND_STATIC                                              !0
   57     1        INIT_FCALL                                               'end'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   58     4        INIT_FCALL                                               'key'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !1, $3
   59     8        INIT_FCALL                                               'reset'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                                 
   60    11      > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17XLq
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $array, !1 = $lastKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   BIND_STATIC                                              !0
   64     1        INIT_FCALL                                               'key'
          2        INIT_FCALL                                               'array_slice'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 -1
          5        SEND_VAL                                                 1
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $2      
          8        SEND_VAR                                                 $2
          9        DO_ICALL                                         $3      
         10        ASSIGN                                                   !1, $3
   65    11      > RETURN                                                   null

End of Dynamic Function 1

Function returntime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 6
Branch analysis from position: 30
2 jumps found. (Code = 125) Position 1 = 31, Position 2 = 37
Branch analysis from position: 31
2 jumps found. (Code = 126) Position 1 = 32, Position 2 = 37
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 10
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 6
Branch analysis from position: 30
Branch analysis from position: 6
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 10
Branch analysis from position: 27
Branch analysis from position: 10
filename:       /in/17XLq
function name:  returnTime
number of ops:  44
compiled vars:  !0 = $function, !1 = $count, !2 = $repeat, !3 = $tests, !4 = $i, !5 = $startTime, !6 = $endTime, !7 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, 100
    6     2        ASSIGN                                                   !2, 100
    8     3        ASSIGN                                                   !3, <array>
    9     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->28
   10     6    >   ASSIGN_DIM                                               !3, !4
          7        OP_DATA                                                  <array>
   12     8        ASSIGN                                                   !4, 0
          9      > JMP                                                      ->25
   13    10    >   INIT_FCALL                                               'microtime'
         11        SEND_VAL                                                 <true>
         12        DO_ICALL                                         $14     
         13        ASSIGN                                                   !5, $14
   14    14        INIT_DYNAMIC_CALL                                        !0
         15        DO_FCALL                                      0          
   15    16        INIT_FCALL                                               'microtime'
         17        SEND_VAL                                                 <true>
         18        DO_ICALL                                         $17     
         19        ASSIGN                                                   !6, $17
   16    20        SUB                                              ~21     !6, !5
         21        FETCH_DIM_W                                      $19     !3, !4
         22        ASSIGN_DIM                                               $19
         23        OP_DATA                                                  ~21
   12    24        PRE_INC                                                  !4
         25    >   IS_SMALLER                                               !4, !1
         26      > JMPNZ                                                    ~23, ->10
    9    27    >   PRE_INC                                                  !4
         28    >   IS_SMALLER                                               !4, !2
         29      > JMPNZ                                                    ~25, ->6
   20    30    > > FE_RESET_RW                                      $26     !3, ->37
         31    > > FE_FETCH_RW                                              $26, !7, ->37
   21    32    >   INIT_FCALL                                               'array_sum'
         33        SEND_VAR                                                 !7
         34        DO_ICALL                                         $27     
         35        ASSIGN                                                   !7, $27
   20    36      > JMP                                                      ->31
         37    >   FE_FREE                                                  $26
   22    38        INIT_FCALL                                               'array_sum'
         39        SEND_VAR                                                 !3
         40        DO_ICALL                                         $29     
         41        DIV                                              ~30     $29, !2
         42      > RETURN                                                   ~30
   23    43*     > RETURN                                                   null

End of function returntime

Function compareresults:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17XLq
function name:  compareResults
number of ops:  47
compiled vars:  !0 = $result1, !1 = $name1, !2 = $result2, !3 = $name2, !4 = $results, !5 = $names, !6 = $fasterIndex, !7 = $slowerIndex, !8 = $fasterResult, !9 = $slowerResult
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   27     4        INIT_ARRAY                                       ~10     !0
   28     5        ADD_ARRAY_ELEMENT                                ~10     !2
   26     6        ASSIGN                                                   !4, ~10
   31     7        INIT_ARRAY                                       ~12     !1
   32     8        ADD_ARRAY_ELEMENT                                ~12     !3
   30     9        ASSIGN                                                   !5, ~12
   35    10        FETCH_DIM_R                                      ~14     !4, 0
         11        FETCH_DIM_R                                      ~15     !4, 1
         12        IS_EQUAL                                                 ~14, ~15
         13      > JMPZ                                                     ~16, ->15
   36    14    > > RETURN                                                   0
   39    15    >   FETCH_DIM_R                                      ~17     !4, 0
         16        FETCH_DIM_R                                      ~18     !4, 1
         17        IS_SMALLER                                               ~17, ~18
         18      > JMPZ                                                     ~19, ->21
   40    19    >   ASSIGN                                                   !6, 0
   39    20      > JMP                                                      ->22
   42    21    >   ASSIGN                                                   !6, 1
   44    22    >   SUB                                              ~22     1, !6
         23        ASSIGN                                                   !7, ~22
   45    24        FETCH_DIM_R                                      ~24     !4, !6
         25        ASSIGN                                                   !8, ~24
   46    26        FETCH_DIM_R                                      ~26     !4, !7
         27        ASSIGN                                                   !9, ~26
   48    28        FETCH_DIM_R                                      ~28     !5, !6
         29        ROPE_INIT                                     4  ~31     ~28
         30        ROPE_ADD                                      1  ~31     ~31, '+performs+faster+than+'
         31        FETCH_DIM_R                                      ~29     !5, !7
         32        ROPE_ADD                                      2  ~31     ~31, ~29
         33        ROPE_END                                      3  ~30     ~31, '.'
         34        ECHO                                                     ~30
         35        ECHO                                                     '%0A'
   49    36        ECHO                                                     'It+is+'
         37        INIT_FCALL                                               'round'
         38        DIV                                              ~33     !9, !8
         39        MUL                                              ~34     ~33, 100
         40        SUB                                              ~35     ~34, 100
         41        SEND_VAL                                                 ~35
         42        DO_ICALL                                         $36     
         43        ECHO                                                     $36
         44        ECHO                                                     '%25+faster.'
         45        ECHO                                                     '%0A'
   50    46      > RETURN                                                   null

End of function compareresults

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.22 ms | 1482 KiB | 24 Q