3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Benchmarking function with garbage collection disabled function benchmark($callback, $iterations = 100) { gc_disable(); // Disable garbage collection for clearer memory tracking $startTime = microtime(true); $startMemory = memory_get_peak_usage(); for ($i = 0; $i < $iterations; $i++) { $callback(); } $endTime = microtime(true); $endMemory = memory_get_peak_usage(); gc_enable(); // Re-enable garbage collection return [ 'time' => $endTime - $startTime, 'peak_memory' => $endMemory - $startMemory, ]; } // Test data - larger arrays to better see memory impact $baseArray = range(1, 1000); $newData = range(1001, 2000); // Method 1: Using array_merge in a loop $mergeArray = $baseArray; $mergeBenchmark = benchmark(function () use (&$mergeArray, $newData) { $tempArray = $mergeArray; // Clone array for memory comparison $mergeArray = array_merge($tempArray, $newData); }); // Method 2: Using array unpacking in a loop $unpackArray = $baseArray; $unpackBenchmark = benchmark(function () use (&$unpackArray, $newData) { $tempArray = $unpackArray; // Clone array for memory comparison $unpackArray = [...$tempArray, ...$newData]; }); // Display results echo "Array Merge:\n"; echo "Time: " . $mergeBenchmark['time'] . " seconds\n"; echo "Peak Memory: " . $mergeBenchmark['peak_memory'] . " bytes\n\n"; echo "Array Unpacking:\n"; echo "Time: " . $unpackBenchmark['time'] . " seconds\n"; echo "Peak Memory: " . $unpackBenchmark['peak_memory'] . " bytes\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1PS65
function name:  (null)
number of ops:  45
compiled vars:  !0 = $baseArray, !1 = $newData, !2 = $mergeArray, !3 = $mergeBenchmark, !4 = $unpackArray, !5 = $unpackBenchmark
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 1
          2        SEND_VAL                                                 1000
          3        DO_ICALL                                         $6      
          4        ASSIGN                                                   !0, $6
   28     5        INIT_FCALL                                               'range'
          6        SEND_VAL                                                 1001
          7        SEND_VAL                                                 2000
          8        DO_ICALL                                         $8      
          9        ASSIGN                                                   !1, $8
   31    10        ASSIGN                                                   !2, !0
   32    11        INIT_FCALL                                               'benchmark'
         12        DECLARE_LAMBDA_FUNCTION                          ~11     [0]
         13        BIND_LEXICAL                                             ~11, !2
         14        BIND_LEXICAL                                             ~11, !1
   35    15        SEND_VAL                                                 ~11
   32    16        DO_FCALL                                      0  $12     
         17        ASSIGN                                                   !3, $12
   38    18        ASSIGN                                                   !4, !0
   39    19        INIT_FCALL                                               'benchmark'
         20        DECLARE_LAMBDA_FUNCTION                          ~15     [1]
         21        BIND_LEXICAL                                             ~15, !4
         22        BIND_LEXICAL                                             ~15, !1
   42    23        SEND_VAL                                                 ~15
   39    24        DO_FCALL                                      0  $16     
         25        ASSIGN                                                   !5, $16
   45    26        ECHO                                                     'Array+Merge%3A%0A'
   46    27        FETCH_DIM_R                                      ~18     !3, 'time'
         28        CONCAT                                           ~19     'Time%3A+', ~18
         29        CONCAT                                           ~20     ~19, '+seconds%0A'
         30        ECHO                                                     ~20
   47    31        FETCH_DIM_R                                      ~21     !3, 'peak_memory'
         32        CONCAT                                           ~22     'Peak+Memory%3A+', ~21
         33        CONCAT                                           ~23     ~22, '+bytes%0A%0A'
         34        ECHO                                                     ~23
   49    35        ECHO                                                     'Array+Unpacking%3A%0A'
   50    36        FETCH_DIM_R                                      ~24     !5, 'time'
         37        CONCAT                                           ~25     'Time%3A+', ~24
         38        CONCAT                                           ~26     ~25, '+seconds%0A'
         39        ECHO                                                     ~26
   51    40        FETCH_DIM_R                                      ~27     !5, 'peak_memory'
         41        CONCAT                                           ~28     'Peak+Memory%3A+', ~27
         42        CONCAT                                           ~29     ~28, '+bytes%0A'
         43        ECHO                                                     ~29
         44      > 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/1PS65
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $mergeArray, !1 = $newData, !2 = $tempArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   33     2        ASSIGN                                                   !2, !0
   34     3        INIT_FCALL                                               'array_merge'
          4        SEND_VAR                                                 !2
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !0, $4
   35     8      > 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/1PS65
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $unpackArray, !1 = $newData, !2 = $tempArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   40     2        ASSIGN                                                   !2, !0
   41     3        INIT_ARRAY                                       ~4      
          4        ADD_ARRAY_UNPACK                                 ~4      !2
          5        ADD_ARRAY_UNPACK                                 ~4      !1
          6        ASSIGN                                                   !0, ~4
   42     7      > RETURN                                                   null

End of Dynamic Function 1

Function benchmark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 13
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 13
Branch analysis from position: 18
Branch analysis from position: 13
filename:       /in/1PS65
function name:  benchmark
number of ops:  33
compiled vars:  !0 = $callback, !1 = $iterations, !2 = $startTime, !3 = $startMemory, !4 = $i, !5 = $endTime, !6 = $endMemory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      100
    6     2        INIT_FCALL                                               'gc_disable'
          3        DO_ICALL                                                 
    8     4        INIT_FCALL                                               'microtime'
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $8      
          7        ASSIGN                                                   !2, $8
    9     8        INIT_FCALL                                               'memory_get_peak_usage'
          9        DO_ICALL                                         $10     
         10        ASSIGN                                                   !3, $10
   11    11        ASSIGN                                                   !4, 0
         12      > JMP                                                      ->16
   12    13    >   INIT_DYNAMIC_CALL                                        !0
         14        DO_FCALL                                      0          
   11    15        PRE_INC                                                  !4
         16    >   IS_SMALLER                                               !4, !1
         17      > JMPNZ                                                    ~15, ->13
   15    18    >   INIT_FCALL                                               'microtime'
         19        SEND_VAL                                                 <true>
         20        DO_ICALL                                         $16     
         21        ASSIGN                                                   !5, $16
   16    22        INIT_FCALL                                               'memory_get_peak_usage'
         23        DO_ICALL                                         $18     
         24        ASSIGN                                                   !6, $18
   18    25        INIT_FCALL                                               'gc_enable'
         26        DO_ICALL                                                 
   21    27        SUB                                              ~21     !5, !2
         28        INIT_ARRAY                                       ~22     ~21, 'time'
   22    29        SUB                                              ~23     !6, !3
         30        ADD_ARRAY_ELEMENT                                ~22     ~23, 'peak_memory'
         31      > RETURN                                                   ~22
   24    32*     > RETURN                                                   null

End of function benchmark

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.1 ms | 1470 KiB | 21 Q