3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Source - https://stackoverflow.com/a/9276284 // Posted by kingmaple, modified by community. See post 'Timeline' for change history // Retrieved 2026-04-08, License - CC BY-SA 4.0 // Source - https://stackoverflow.com/a/53203232 // Posted by slaszu, modified by community. See post 'Timeline' for change history // Retrieved 2026-04-08, License - CC BY-SA 4.0 // ini_set('memory_limit', '2048M'); function formatBytes(int $bytes): string { $units = ['B', 'KB', 'MB', 'GB']; $i = 0; while ($bytes >= 1024 && $i < count($units) - 1) { $bytes /= 1024; $i++; } return sprintf("%.2f %s", $bytes, $units[$i]); } function benchmark(callable $fn, string $label): array { gc_collect_cycles(); gc_mem_caches(); memory_reset_peak_usage(); $mem = -memory_get_peak_usage(); $time = -hrtime(true); $fn(); $time += hrtime(true); $mem += memory_get_peak_usage(); return [ 'label' => $label, 'time_ms' => $time / 1e6, 'mem_used' => $mem, ]; } function manual_intersect($arrayOne, $arrayTwo) { $index = array_flip($arrayOne); foreach ($arrayTwo as $value) { if (isset($index[$value])) { unset($index[$value]); } } foreach ($index as $value => $key) { unset($arrayOne[$key]); } return $arrayOne; } function flipped_intersect($arrayOne, $arrayTwo) { $index = array_flip($arrayOne); $second = array_flip($arrayTwo); $x = array_intersect_key($index, $second); return array_flip($x); } function runBenchmarks(int $n): void { echo "\n=== Array Intersection Benchmark for " . number_format($n) . " elements ===\n"; // Generate test arrays $one = []; $two = []; for ($i = 0; $i < $n; $i++) { $one[] = rand(0, 1000000); $two[] = rand(0, 100000); $two[] = rand(0, 10000); } $one = array_unique($one); $two = array_unique($two); $results = []; $results[] = benchmark( fn() => $res = array_intersect($one, $two), 'array_intersect()' ); $results[] = benchmark( fn() => $res = manual_intersect($one, $two), 'manual_intersect()' ); $results[] = benchmark( fn() => $res = flipped_intersect($one, $two), 'flipped_intersect()' ); // --- Print Table --- echo str_repeat('-', 60) . "\n"; printf("%-25s | %-14s | %-15s\n", 'Method', 'Time (ms)', 'Memory'); echo str_repeat('-', 60) . "\n"; foreach ($results as $r) { printf("%-25s | %11.3f ms | %15s\n", $r['label'], $r['time_ms'], formatBytes($r['mem_used']) ); } echo str_repeat('-', 60) . "\n"; } // Run for various sizes foreach ([20, 20000, 200000] as $n) { runBenchmarks($n); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 6
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 6
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/2kHVL
function name:  (null)
number of ops:  8
compiled vars:  !0 = $n
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  111     0  E > > FE_RESET_R                                           $1      <array>, ->6
          1    > > FE_FETCH_R                                                   $1, !0, ->6
  112     2    >   INIT_FCALL                                                   'runbenchmarks'
          3        SEND_VAR                                                     !0
          4        DO_FCALL                                          0          
  111     5      > JMP                                                          ->1
          6    >   FE_FREE                                                      $1
  113     7      > RETURN                                                       1

Function formatbytes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 4
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
Branch analysis from position: 12
Branch analysis from position: 12
filename:       /in/2kHVL
function name:  formatBytes
number of ops:  23
compiled vars:  !0 = $bytes, !1 = $units, !2 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   15     1        ASSIGN                                                       !1, <array>
   16     2        ASSIGN                                                       !2, 0
   17     3      > JMP                                                          ->6
   18     4    >   ASSIGN_OP                                         4          !0, 1024
   19     5        PRE_INC                                                      !2
   17     6    >   IS_SMALLER_OR_EQUAL                                  ~7      1024, !0
          7      > JMPZ_EX                                              ~7      ~7, ->12
          8    >   COUNT                                                ~8      !1
          9        SUB                                                  ~9      ~8, 1
         10        IS_SMALLER                                           ~10     !2, ~9
         11        BOOL                                                 ~7      ~10
         12    > > JMPNZ                                                        ~7, ->4
   21    13    >   INIT_FCALL                                                   'sprintf'
         14        SEND_VAL                                                     '%25.2f+%25s'
         15        SEND_VAR                                                     !0
         16        FETCH_DIM_R                                          ~11     !1, !2
         17        SEND_VAL                                                     ~11
         18        DO_ICALL                                             $12     
         19        VERIFY_RETURN_TYPE                                           $12
         20      > RETURN                                                       $12
   22    21*       VERIFY_RETURN_TYPE                                           
         22*     > RETURN                                                       null

End of function formatbytes

Function benchmark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2kHVL
function name:  benchmark
number of ops:  34
compiled vars:  !0 = $fn, !1 = $label, !2 = $mem, !3 = $time
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   25     2        INIT_FCALL                                                   'gc_collect_cycles'
          3        DO_ICALL                                                     
   26     4        INIT_FCALL                                                   'gc_mem_caches'
          5        DO_ICALL                                                     
   27     6        INIT_FCALL                                                   'memory_reset_peak_usage'
          7        DO_ICALL                                                     
   28     8        INIT_FCALL                                                   'memory_get_peak_usage'
          9        DO_ICALL                                             $7      
         10        MUL                                                  ~8      $7, -1
         11        ASSIGN                                                       !2, ~8
   29    12        INIT_FCALL                                                   'hrtime'
         13        SEND_VAL                                                     <true>
         14        DO_ICALL                                             $10     
         15        MUL                                                  ~11     $10, -1
         16        ASSIGN                                                       !3, ~11
   30    17        INIT_DYNAMIC_CALL                                            !0
         18        DO_FCALL                                          0          
   31    19        INIT_FCALL                                                   'hrtime'
         20        SEND_VAL                                                     <true>
         21        DO_ICALL                                             $14     
         22        ASSIGN_OP                                         1          !3, $14
   32    23        INIT_FCALL                                                   'memory_get_peak_usage'
         24        DO_ICALL                                             $16     
         25        ASSIGN_OP                                         1          !2, $16
   34    26        INIT_ARRAY                                           ~18     !1, 'label'
   35    27        DIV                                                  ~19     !3, 1.0e+6
         28        ADD_ARRAY_ELEMENT                                    ~18     ~19, 'time_ms'
   36    29        ADD_ARRAY_ELEMENT                                    ~18     !2, 'mem_used'
         30        VERIFY_RETURN_TYPE                                           ~18
         31      > RETURN                                                       ~18
   38    32*       VERIFY_RETURN_TYPE                                           
         33*     > RETURN                                                       null

End of function benchmark

Function manual_intersect:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 11
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
Branch analysis from position: 12
filename:       /in/2kHVL
function name:  manual_intersect
number of ops:  21
compiled vars:  !0 = $arrayOne, !1 = $arrayTwo, !2 = $index, !3 = $value, !4 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   40     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   41     2        INIT_FCALL                                                   'array_flip'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                             $5      
          5        ASSIGN                                                       !2, $5
   42     6      > FE_RESET_R                                           $7      !1, ->12
          7    > > FE_FETCH_R                                                   $7, !3, ->12
   43     8    >   ISSET_ISEMPTY_DIM_OBJ                             0          !2, !3
          9      > JMPZ                                                         ~8, ->11
   44    10    >   UNSET_DIM                                                    !2, !3
   42    11    > > JMP                                                          ->7
         12    >   FE_FREE                                                      $7
   47    13      > FE_RESET_R                                           $9      !2, ->18
         14    > > FE_FETCH_R                                           ~10     $9, !4, ->18
         15    >   ASSIGN                                                       !3, ~10
   48    16        UNSET_DIM                                                    !0, !4
   47    17      > JMP                                                          ->14
         18    >   FE_FREE                                                      $9
   50    19      > RETURN                                                       !0
   51    20*     > RETURN                                                       null

End of function manual_intersect

Function flipped_intersect:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2kHVL
function name:  flipped_intersect
number of ops:  20
compiled vars:  !0 = $arrayOne, !1 = $arrayTwo, !2 = $index, !3 = $second, !4 = $x
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   53     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   54     2        INIT_FCALL                                                   'array_flip'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                             $5      
          5        ASSIGN                                                       !2, $5
   55     6        INIT_FCALL                                                   'array_flip'
          7        SEND_VAR                                                     !1
          8        DO_ICALL                                             $7      
          9        ASSIGN                                                       !3, $7
   56    10        INIT_FCALL                                                   'array_intersect_key'
         11        SEND_VAR                                                     !2
         12        SEND_VAR                                                     !3
         13        DO_ICALL                                             $9      
         14        ASSIGN                                                       !4, $9
   57    15        INIT_FCALL                                                   'array_flip'
         16        SEND_VAR                                                     !4
         17        DO_ICALL                                             $11     
         18      > RETURN                                                       $11
   58    19*     > RETURN                                                       null

End of function flipped_intersect

Function runbenchmarks:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 11
Branch analysis from position: 32
2 jumps found. (Code = 77) Position 1 = 90, Position 2 = 104
Branch analysis from position: 90
2 jumps found. (Code = 78) Position 1 = 91, Position 2 = 104
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 90
Branch analysis from position: 90
Branch analysis from position: 104
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 104
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 11
Branch analysis from position: 32
Branch analysis from position: 11
filename:       /in/2kHVL
function name:  runBenchmarks
number of ops:  112
compiled vars:  !0 = $n, !1 = $one, !2 = $two, !3 = $i, !4 = $results, !5 = $res, !6 = $r
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   61     0  E >   RECV                                                 !0      
   62     1        INIT_FCALL                                                   'number_format'
          2        SEND_VAR                                                     !0
          3        DO_ICALL                                             $7      
          4        CONCAT                                               ~8      '%0A%3D%3D%3D+Array+Intersection+Benchmark+for+', $7
          5        CONCAT                                               ~9      ~8, '+elements+%3D%3D%3D%0A'
          6        ECHO                                                         ~9
   65     7        ASSIGN                                                       !1, <array>
   66     8        ASSIGN                                                       !2, <array>
   67     9        ASSIGN                                                       !3, 0
         10      > JMP                                                          ->30
   68    11    >   INIT_FCALL                                                   'rand'
         12        SEND_VAL                                                     0
         13        SEND_VAL                                                     1000000
         14        DO_ICALL                                             $14     
         15        ASSIGN_DIM                                                   !1
         16        OP_DATA                                                      $14
   69    17        INIT_FCALL                                                   'rand'
         18        SEND_VAL                                                     0
         19        SEND_VAL                                                     100000
         20        DO_ICALL                                             $16     
         21        ASSIGN_DIM                                                   !2
         22        OP_DATA                                                      $16
   70    23        INIT_FCALL                                                   'rand'
         24        SEND_VAL                                                     0
         25        SEND_VAL                                                     10000
         26        DO_ICALL                                             $18     
         27        ASSIGN_DIM                                                   !2
         28        OP_DATA                                                      $18
   67    29        PRE_INC                                                      !3
         30    >   IS_SMALLER                                                   !3, !0
         31      > JMPNZ                                                        ~20, ->11
   73    32    >   INIT_FCALL                                                   'array_unique'
         33        SEND_VAR                                                     !1
         34        DO_ICALL                                             $21     
         35        ASSIGN                                                       !1, $21
   74    36        INIT_FCALL                                                   'array_unique'
         37        SEND_VAR                                                     !2
         38        DO_ICALL                                             $23     
         39        ASSIGN                                                       !2, $23
   76    40        ASSIGN                                                       !4, <array>
   79    41        INIT_FCALL                                                   'benchmark'
   80    42        DECLARE_LAMBDA_FUNCTION                              ~27     [0]
         43        BIND_LEXICAL                                                 ~27, !5
         44        BIND_LEXICAL                                                 ~27, !1
         45        BIND_LEXICAL                                                 ~27, !2
         46        SEND_VAL                                                     ~27
   81    47        SEND_VAL                                                     'array_intersect%28%29'
   79    48        DO_FCALL                                          0  $28     
         49        ASSIGN_DIM                                                   !4
   81    50        OP_DATA                                                      $28
   84    51        INIT_FCALL                                                   'benchmark'
   85    52        DECLARE_LAMBDA_FUNCTION                              ~30     [1]
         53        BIND_LEXICAL                                                 ~30, !5
         54        BIND_LEXICAL                                                 ~30, !1
         55        BIND_LEXICAL                                                 ~30, !2
         56        SEND_VAL                                                     ~30
   86    57        SEND_VAL                                                     'manual_intersect%28%29'
   84    58        DO_FCALL                                          0  $31     
         59        ASSIGN_DIM                                                   !4
   86    60        OP_DATA                                                      $31
   90    61        INIT_FCALL                                                   'benchmark'
   91    62        DECLARE_LAMBDA_FUNCTION                              ~33     [2]
         63        BIND_LEXICAL                                                 ~33, !5
         64        BIND_LEXICAL                                                 ~33, !1
         65        BIND_LEXICAL                                                 ~33, !2
         66        SEND_VAL                                                     ~33
   92    67        SEND_VAL                                                     'flipped_intersect%28%29'
   90    68        DO_FCALL                                          0  $34     
         69        ASSIGN_DIM                                                   !4
   92    70        OP_DATA                                                      $34
   96    71        INIT_FCALL                                                   'str_repeat'
         72        SEND_VAL                                                     '-'
         73        SEND_VAL                                                     60
         74        DO_ICALL                                             $35     
         75        CONCAT                                               ~36     $35, '%0A'
         76        ECHO                                                         ~36
   97    77        INIT_FCALL                                                   'printf'
         78        SEND_VAL                                                     '%25-25s+%7C+%25-14s+%7C+%25-15s%0A'
         79        SEND_VAL                                                     'Method'
         80        SEND_VAL                                                     'Time+%28ms%29'
         81        SEND_VAL                                                     'Memory'
         82        DO_ICALL                                                     
   98    83        INIT_FCALL                                                   'str_repeat'
         84        SEND_VAL                                                     '-'
         85        SEND_VAL                                                     60
         86        DO_ICALL                                             $38     
         87        CONCAT                                               ~39     $38, '%0A'
         88        ECHO                                                         ~39
  100    89      > FE_RESET_R                                           $40     !4, ->104
         90    > > FE_FETCH_R                                                   $40, !6, ->104
  101    91    >   INIT_FCALL                                                   'printf'
         92        SEND_VAL                                                     '%25-25s+%7C+%2511.3f+ms+%7C+%2515s%0A'
  102    93        FETCH_DIM_R                                          ~41     !6, 'label'
         94        SEND_VAL                                                     ~41
  103    95        FETCH_DIM_R                                          ~42     !6, 'time_ms'
         96        SEND_VAL                                                     ~42
  104    97        INIT_FCALL                                                   'formatbytes'
         98        FETCH_DIM_R                                          ~43     !6, 'mem_used'
         99        SEND_VAL                                                     ~43
        100        DO_FCALL                                          0  $44     
        101        SEND_VAR                                                     $44
  101   102        DO_ICALL                                                     
  100   103      > JMP                                                          ->90
        104    >   FE_FREE                                                      $40
  107   105        INIT_FCALL                                                   'str_repeat'
        106        SEND_VAL                                                     '-'
        107        SEND_VAL                                                     60
        108        DO_ICALL                                             $46     
        109        CONCAT                                               ~47     $46, '%0A'
        110        ECHO                                                         ~47
  108   111      > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2kHVL
function name:  {closure:runBenchmarks():80}
number of ops:  10
compiled vars:  !0 = $res, !1 = $one, !2 = $two
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   80     0  E >   BIND_STATIC                                                  !0
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        INIT_FCALL                                                   'array_intersect'
          4        SEND_VAR                                                     !1
          5        SEND_VAR                                                     !2
          6        DO_ICALL                                             $3      
          7        ASSIGN                                               ~4      !0, $3
          8      > RETURN                                                       ~4
          9*     > 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/2kHVL
function name:  {closure:runBenchmarks():85}
number of ops:  10
compiled vars:  !0 = $res, !1 = $one, !2 = $two
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   85     0  E >   BIND_STATIC                                                  !0
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        INIT_FCALL                                                   'manual_intersect'
          4        SEND_VAR                                                     !1
          5        SEND_VAR                                                     !2
          6        DO_FCALL                                          0  $3      
          7        ASSIGN                                               ~4      !0, $3
          8      > RETURN                                                       ~4
          9*     > RETURN                                                       null

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2kHVL
function name:  {closure:runBenchmarks():91}
number of ops:  10
compiled vars:  !0 = $res, !1 = $one, !2 = $two
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   91     0  E >   BIND_STATIC                                                  !0
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        INIT_FCALL                                                   'flipped_intersect'
          4        SEND_VAR                                                     !1
          5        SEND_VAR                                                     !2
          6        DO_FCALL                                          0  $3      
          7        ASSIGN                                               ~4      !0, $3
          8      > RETURN                                                       ~4
          9*     > RETURN                                                       null

End of Dynamic Function 2

End of function runbenchmarks

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
182.06 ms | 1433 KiB | 34 Q