3v4l.org

run code in 300+ PHP versions simultaneously
<?php // тест для foreach function foreach_test($range) { // массив $array = range(1, $range); // старт $start = microtime(true); // обработка массива foreach ($array as &$value) { $value /= 2; } // финиш $finish = microtime(true); // время выполнения return round($finish - $start, 5); } // тест для array_walk function array_walk_test($range) { // массив $array = range(1, $range); // старт $start = microtime(true); // обработка array_walk($array, function(&$value){ $value /= 2; }); // финиш $finish = microtime(true); // время выполнения return round($finish - $start, 5); } // тесты $tests = array(1000); // выполнение тестов и результаты foreach ($tests as $range) { $ft = foreach_test($range); $awt = array_walk_test($range); $div = round($awt / $ft, 2); echo "Количество элементов: {$range} <br>"; echo "Время foreach: {$ft}с <br>"; echo "Время array_walk: {$awt}с <br>"; echo "foreach быстрей array_walk в {$div} раз <br><br>"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 34
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 34
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/oZ6sT
function name:  (null)
number of ops:  36
compiled vars:  !0 = $tests, !1 = $range, !2 = $ft, !3 = $awt, !4 = $div
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   ASSIGN                                                   !0, <array>
   49     1      > FE_RESET_R                                       $6      !0, ->34
          2    > > FE_FETCH_R                                               $6, !1, ->34
   50     3    >   INIT_FCALL                                               'foreach_test'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $7      
          6        ASSIGN                                                   !2, $7
   51     7        INIT_FCALL                                               'array_walk_test'
          8        SEND_VAR                                                 !1
          9        DO_FCALL                                      0  $9      
         10        ASSIGN                                                   !3, $9
   52    11        INIT_FCALL                                               'round'
         12        DIV                                              ~11     !3, !2
         13        SEND_VAL                                                 ~11
         14        SEND_VAL                                                 2
         15        DO_ICALL                                         $12     
         16        ASSIGN                                                   !4, $12
   54    17        ROPE_INIT                                     3  ~15     '%D0%9A%D0%BE%D0%BB%D0%B8%D1%87%D0%B5%D1%81%D1%82%D0%B2%D0%BE+%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D0%BE%D0%B2%3A+'
         18        ROPE_ADD                                      1  ~15     ~15, !1
         19        ROPE_END                                      2  ~14     ~15, '+%3Cbr%3E'
         20        ECHO                                                     ~14
   55    21        ROPE_INIT                                     3  ~18     '%D0%92%D1%80%D0%B5%D0%BC%D1%8F+foreach%3A+'
         22        ROPE_ADD                                      1  ~18     ~18, !2
         23        ROPE_END                                      2  ~17     ~18, '%D1%81+%3Cbr%3E'
         24        ECHO                                                     ~17
   56    25        ROPE_INIT                                     3  ~21     '%D0%92%D1%80%D0%B5%D0%BC%D1%8F+array_walk%3A+'
         26        ROPE_ADD                                      1  ~21     ~21, !3
         27        ROPE_END                                      2  ~20     ~21, '%D1%81+%3Cbr%3E'
         28        ECHO                                                     ~20
   57    29        ROPE_INIT                                     3  ~24     'foreach+%D0%B1%D1%8B%D1%81%D1%82%D1%80%D0%B5%D0%B9+array_walk+%D0%B2+'
         30        ROPE_ADD                                      1  ~24     ~24, !4
         31        ROPE_END                                      2  ~23     ~24, '+%D1%80%D0%B0%D0%B7+%3Cbr%3E%3Cbr%3E'
         32        ECHO                                                     ~23
   49    33      > JMP                                                      ->2
         34    >   FE_FREE                                                  $6
   58    35      > RETURN                                                   1

Function foreach_test:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
2 jumps found. (Code = 126) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/oZ6sT
function name:  foreach_test
number of ops:  26
compiled vars:  !0 = $range, !1 = $array, !2 = $start, !3 = $value, !4 = $finish
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    7     1        INIT_FCALL                                               'range'
          2        SEND_VAL                                                 1
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   10     6        INIT_FCALL                                               'microtime'
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $7      
          9        ASSIGN                                                   !2, $7
   13    10      > FE_RESET_RW                                      $9      !1, ->14
         11    > > FE_FETCH_RW                                              $9, !3, ->14
   14    12    >   ASSIGN_OP                                     4          !3, 2
   13    13      > JMP                                                      ->11
         14    >   FE_FREE                                                  $9
   18    15        INIT_FCALL                                               'microtime'
         16        SEND_VAL                                                 <true>
         17        DO_ICALL                                         $11     
         18        ASSIGN                                                   !4, $11
   21    19        INIT_FCALL                                               'round'
         20        SUB                                              ~13     !4, !2
         21        SEND_VAL                                                 ~13
         22        SEND_VAL                                                 5
         23        DO_ICALL                                         $14     
         24      > RETURN                                                   $14
   22    25*     > RETURN                                                   null

End of function foreach_test

Function array_walk_test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oZ6sT
function name:  array_walk_test
number of ops:  26
compiled vars:  !0 = $range, !1 = $array, !2 = $start, !3 = $finish
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   28     1        INIT_FCALL                                               'range'
          2        SEND_VAL                                                 1
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   31     6        INIT_FCALL                                               'microtime'
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $6      
          9        ASSIGN                                                   !2, $6
   34    10        INIT_FCALL                                               'array_walk'
         11        SEND_REF                                                 !1
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoZ6sT%3A34%240'
   36    13        SEND_VAL                                                 ~8
         14        DO_ICALL                                                 
   39    15        INIT_FCALL                                               'microtime'
         16        SEND_VAL                                                 <true>
         17        DO_ICALL                                         $10     
         18        ASSIGN                                                   !3, $10
   42    19        INIT_FCALL                                               'round'
         20        SUB                                              ~12     !3, !2
         21        SEND_VAL                                                 ~12
         22        SEND_VAL                                                 5
         23        DO_ICALL                                         $13     
         24      > RETURN                                                   $13
   43    25*     > RETURN                                                   null

End of function array_walk_test

Function %00%7Bclosure%7D%2Fin%2FoZ6sT%3A34%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oZ6sT
function name:  {closure}
number of ops:  3
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        ASSIGN_OP                                     4          !0, 2
   36     2      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoZ6sT%3A34%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.82 ms | 1407 KiB | 23 Q