3v4l.org

run code in 300+ PHP versions simultaneously
<?php function swap(&$arr, $i, $j) { $temp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $temp; } function quicksort(&$arr, $left, $right) { $pivot = $arr[($left + $right) >> 1]; $i = $left; $j = $right; while ($i <= $j) { while ($arr[$i] < $pivot) { $i++; } while ($arr[$j] > $pivot) { $j--; } if ($i <= $j) { swap($arr, $i++, $j--); } } if ($left < $j) { quicksort($arr, $left, $j); } if ($i < $right) { quicksort($arr, $i, $right); } } function qsort($len) { // Sample data $arr = array(); for ($i = 0 ; $i < $len ; $i++) { $arr[$i] = floor(rand(0, $len)); if ($i % 2 == 0) { $arr[$i] = -1 * $arr[$i]; } } $start = microtime(true); quicksort($arr, 0, count($arr) - 1); $diff = microtime(true) - $start; for ($i = 1 ; $i < count($arr) ; $i++) { if ($arr[$i-1] > $arr[$i]) { die("failed to sort!!"); } } return $diff; } if (count($argv) != 2) { die("usage: php -q quicksort.php ARRAY_LENGTH"); } $len = 100; $min = 0; for ($i = 0 ; $i < 3 ; $i++) { $time = qsort($len); if ($time < $min) { $min = $time; } } echo "PHP quicksort: " . round($min*1000) . " ms (" . $len . ")\n" ;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 8
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 8
Branch analysis from position: 18
Branch analysis from position: 8
Branch analysis from position: 15
filename:       /in/0CueS
function name:  (null)
number of ops:  28
compiled vars:  !0 = $argv, !1 = $len, !2 = $min, !3 = $i, !4 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   COUNT                                            ~5      !0
          1        IS_NOT_EQUAL                                             ~5, 2
          2      > JMPZ                                                     ~6, ->4
   77     3    > > EXIT                                                     'usage%3A+php+-q+quicksort.php+ARRAY_LENGTH'
   80     4    >   ASSIGN                                                   !1, 100
   81     5        ASSIGN                                                   !2, 0
   83     6        ASSIGN                                                   !3, 0
          7      > JMP                                                      ->16
   85     8    >   INIT_FCALL                                               'qsort'
          9        SEND_VAR                                                 !1
         10        DO_FCALL                                      0  $10     
         11        ASSIGN                                                   !4, $10
   87    12        IS_SMALLER                                               !4, !2
         13      > JMPZ                                                     ~12, ->15
   89    14    >   ASSIGN                                                   !2, !4
   83    15    >   PRE_INC                                                  !3
         16    >   IS_SMALLER                                               !3, 3
         17      > JMPNZ                                                    ~15, ->8
   93    18    >   INIT_FCALL                                               'round'
         19        MUL                                              ~16     !2, 1000
         20        SEND_VAL                                                 ~16
         21        DO_ICALL                                         $17     
         22        CONCAT                                           ~18     'PHP+quicksort%3A+', $17
         23        CONCAT                                           ~19     ~18, '+ms+%28'
         24        CONCAT                                           ~20     ~19, !1
         25        CONCAT                                           ~21     ~20, '%29%0A'
         26        ECHO                                                     ~21
         27      > RETURN                                                   1

Function swap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0CueS
function name:  swap
number of ops:  11
compiled vars:  !0 = $arr, !1 = $i, !2 = $j, !3 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    5     3        FETCH_DIM_R                                      ~4      !0, !1
          4        ASSIGN                                                   !3, ~4
    6     5        FETCH_DIM_R                                      ~7      !0, !2
          6        ASSIGN_DIM                                               !0, !1
          7        OP_DATA                                                  ~7
    7     8        ASSIGN_DIM                                               !0, !2
          9        OP_DATA                                                  !3
    8    10      > RETURN                                                   null

End of function swap

Function quicksort:
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 = 10
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 38
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 45
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 38
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 11
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 16
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 10
Branch analysis from position: 31
Branch analysis from position: 10
Branch analysis from position: 29
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 16
Branch analysis from position: 20
Branch analysis from position: 16
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 11
Branch analysis from position: 15
Branch analysis from position: 11
filename:       /in/0CueS
function name:  quicksort
number of ops:  46
compiled vars:  !0 = $arr, !1 = $left, !2 = $right, !3 = $pivot, !4 = $i, !5 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   12     3        ADD                                              ~6      !1, !2
          4        SR                                               ~7      ~6, 1
          5        FETCH_DIM_R                                      ~8      !0, ~7
          6        ASSIGN                                                   !3, ~8
   13     7        ASSIGN                                                   !4, !1
   14     8        ASSIGN                                                   !5, !2
   16     9      > JMP                                                      ->29
   18    10    > > JMP                                                      ->12
   20    11    >   PRE_INC                                                  !4
   18    12    >   FETCH_DIM_R                                      ~13     !0, !4
         13        IS_SMALLER                                               ~13, !3
         14      > JMPNZ                                                    ~14, ->11
   23    15    > > JMP                                                      ->17
   25    16    >   PRE_DEC                                                  !5
   23    17    >   FETCH_DIM_R                                      ~16     !0, !5
         18        IS_SMALLER                                               !3, ~16
         19      > JMPNZ                                                    ~17, ->16
   28    20    >   IS_SMALLER_OR_EQUAL                                      !4, !5
         21      > JMPZ                                                     ~18, ->29
   30    22    >   INIT_FCALL                                               'swap'
         23        SEND_REF                                                 !0
         24        POST_INC                                         ~19     !4
         25        SEND_VAL                                                 ~19
         26        POST_DEC                                         ~20     !5
         27        SEND_VAL                                                 ~20
         28        DO_FCALL                                      0          
   16    29    >   IS_SMALLER_OR_EQUAL                                      !4, !5
         30      > JMPNZ                                                    ~22, ->10
   34    31    >   IS_SMALLER                                               !1, !5
         32      > JMPZ                                                     ~23, ->38
   36    33    >   INIT_FCALL_BY_NAME                                       'quicksort'
         34        SEND_VAR_EX                                              !0
         35        SEND_VAR_EX                                              !1
         36        SEND_VAR_EX                                              !5
         37        DO_FCALL                                      0          
   39    38    >   IS_SMALLER                                               !4, !2
         39      > JMPZ                                                     ~25, ->45
   41    40    >   INIT_FCALL_BY_NAME                                       'quicksort'
         41        SEND_VAR_EX                                              !0
         42        SEND_VAR_EX                                              !4
         43        SEND_VAR_EX                                              !2
         44        DO_FCALL                                      0          
   43    45    > > RETURN                                                   null

End of function quicksort

Function qsort:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 4
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 41
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 47
Branch analysis from position: 46
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 41
Branch analysis from position: 51
Branch analysis from position: 41
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 20
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 4
Branch analysis from position: 23
Branch analysis from position: 4
Branch analysis from position: 20
filename:       /in/0CueS
function name:  qsort
number of ops:  53
compiled vars:  !0 = $len, !1 = $arr, !2 = $i, !3 = $start, !4 = $diff
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   48     1        ASSIGN                                                   !1, <array>
   50     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->21
   52     4    >   INIT_FCALL                                               'floor'
          5        INIT_FCALL                                               'rand'
          6        SEND_VAL                                                 0
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $8      
          9        SEND_VAR                                                 $8
         10        DO_ICALL                                         $9      
         11        ASSIGN_DIM                                               !1, !2
         12        OP_DATA                                                  $9
   54    13        MOD                                              ~10     !2, 2
         14        IS_EQUAL                                                 ~10, 0
         15      > JMPZ                                                     ~11, ->20
   56    16    >   FETCH_DIM_R                                      ~13     !1, !2
         17        MUL                                              ~14     ~13, -1
         18        ASSIGN_DIM                                               !1, !2
         19        OP_DATA                                                  ~14
   50    20    >   PRE_INC                                                  !2
         21    >   IS_SMALLER                                               !2, !0
         22      > JMPNZ                                                    ~16, ->4
   60    23    >   INIT_FCALL                                               'microtime'
         24        SEND_VAL                                                 <true>
         25        DO_ICALL                                         $17     
         26        ASSIGN                                                   !3, $17
   61    27        INIT_FCALL                                               'quicksort'
         28        SEND_REF                                                 !1
         29        SEND_VAL                                                 0
         30        COUNT                                            ~19     !1
         31        SUB                                              ~20     ~19, 1
         32        SEND_VAL                                                 ~20
         33        DO_FCALL                                      0          
   62    34        INIT_FCALL                                               'microtime'
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $22     
         37        SUB                                              ~23     $22, !3
         38        ASSIGN                                                   !4, ~23
   64    39        ASSIGN                                                   !2, 1
         40      > JMP                                                      ->48
   66    41    >   SUB                                              ~26     !2, 1
         42        FETCH_DIM_R                                      ~27     !1, ~26
         43        FETCH_DIM_R                                      ~28     !1, !2
         44        IS_SMALLER                                               ~28, ~27
         45      > JMPZ                                                     ~29, ->47
   68    46    > > EXIT                                                     'failed+to+sort%21%21'
   64    47    >   PRE_INC                                                  !2
         48    >   COUNT                                            ~31     !1
         49        IS_SMALLER                                               !2, ~31
         50      > JMPNZ                                                    ~32, ->41
   72    51    > > RETURN                                                   !4
   73    52*     > RETURN                                                   null

End of function qsort

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.82 ms | 1402 KiB | 24 Q