3v4l.org

run code in 300+ PHP versions simultaneously
<?php // test a simple array_map in the real world. function test_array_map($data){ return array_map(function($row){ return array( 'productId' => $row['id'] + 1, 'productName' => $row['name'], 'desc' => $row['remark'] ); }, $data); } // Another with local variable $i function test_array_map_use_local($data){ $i = 0; return array_map(function($row) use ($i) { $i++; return array( 'productId' => $row['id'] + $i, 'productName' => $row['name'], 'desc' => $row['remark'] ); }, $data); } // test a simple foreach in the real world function test_foreach($data){ $result = array(); foreach ($data as $row) { $tmp = array(); $tmp['productId'] = $row['id'] + 1; $tmp['productName'] = $row['name']; $tmp['desc'] = $row['remark']; $result[] = $tmp; } return $result; } // Another with local variable $i function test_foreach_use_local($data){ $result = array(); $i = 0; foreach ($data as $row) { $i++; $tmp = array(); $tmp['productId'] = $row['id'] + $i; $tmp['productName'] = $row['name']; $tmp['desc'] = $row['remark']; $result[] = $tmp; } return $result; } $data = array_fill(0, 10000, array( 'id' => 1, 'name' => 'test', 'remark' => 'ok' )); $tests = array( 'array_map' => array(), 'foreach' => array(), 'array_map_use_local' => array(), 'foreach_use_local' => array(), ); for ($i = 0; $i < 100; $i++){ foreach ($tests as $testName => &$records) { $start = microtime(true); call_user_func("test_$testName", $data); $delta = microtime(true) - $start; $records[] = $delta; } } // output result: foreach ($tests as $name => &$records) { printf('%.4f : %s '.PHP_EOL, array_sum($records) / count($records), $name); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 9
Branch analysis from position: 33
2 jumps found. (Code = 125) Position 1 = 34, Position 2 = 47
Branch analysis from position: 34
2 jumps found. (Code = 126) Position 1 = 35, Position 2 = 47
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
Branch analysis from position: 9
2 jumps found. (Code = 125) Position 1 = 10, Position 2 = 29
Branch analysis from position: 10
2 jumps found. (Code = 126) Position 1 = 11, Position 2 = 29
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 9
Branch analysis from position: 33
Branch analysis from position: 9
Branch analysis from position: 29
filename:       /in/lon7l
function name:  (null)
number of ops:  49
compiled vars:  !0 = $data, !1 = $tests, !2 = $i, !3 = $records, !4 = $testName, !5 = $start, !6 = $delta, !7 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   INIT_FCALL                                               'array_fill'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 10000
   56     3        SEND_VAL                                                 <array>
          4        DO_ICALL                                         $8      
   55     5        ASSIGN                                                   !0, $8
   61     6        ASSIGN                                                   !1, <array>
   68     7        ASSIGN                                                   !2, 0
          8      > JMP                                                      ->31
   69     9    > > FE_RESET_RW                                      $12     !1, ->29
         10    > > FE_FETCH_RW                                      ~13     $12, !3, ->29
         11    >   ASSIGN                                                   !4, ~13
   70    12        INIT_FCALL                                               'microtime'
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $15     
         15        ASSIGN                                                   !5, $15
   71    16        NOP                                                      
         17        FAST_CONCAT                                      ~17     'test_', !4
         18        INIT_USER_CALL                                1          'call_user_func', ~17
         19        SEND_USER                                                !0
         20        DO_FCALL                                      0          
   72    21        INIT_FCALL                                               'microtime'
         22        SEND_VAL                                                 <true>
         23        DO_ICALL                                         $19     
         24        SUB                                              ~20     $19, !5
         25        ASSIGN                                                   !6, ~20
   73    26        ASSIGN_DIM                                               !3
         27        OP_DATA                                                  !6
   69    28      > JMP                                                      ->10
         29    >   FE_FREE                                                  $12
   68    30        PRE_INC                                                  !2
         31    >   IS_SMALLER                                               !2, 100
         32      > JMPNZ                                                    ~24, ->9
   78    33    > > FE_RESET_RW                                      $25     !1, ->47
         34    > > FE_FETCH_RW                                      ~26     $25, !3, ->47
         35    >   ASSIGN                                                   !7, ~26
   79    36        INIT_FCALL                                               'printf'
         37        SEND_VAL                                                 '%25.4f+%3A+%25s+%0A'
   80    38        INIT_FCALL                                               'array_sum'
         39        SEND_VAR                                                 !3
         40        DO_ICALL                                         $28     
         41        COUNT                                            ~29     !3
         42        DIV                                              ~30     $28, ~29
         43        SEND_VAL                                                 ~30
         44        SEND_VAR                                                 !7
         45        DO_ICALL                                                 
   78    46      > JMP                                                      ->34
         47    >   FE_FREE                                                  $25
   81    48      > RETURN                                                   1

Function test_array_map:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lon7l
function name:  test_array_map
number of ops:  8
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Flon7l%3A5%240'
   11     3        SEND_VAL                                                 ~1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6      > RETURN                                                   $2
   12     7*     > RETURN                                                   null

End of function test_array_map

Function %00%7Bclosure%7D%2Fin%2Flon7l%3A5%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lon7l
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    7     1        FETCH_DIM_R                                      ~1      !0, 'id'
          2        ADD                                              ~2      ~1, 1
          3        INIT_ARRAY                                       ~3      ~2, 'productId'
    8     4        FETCH_DIM_R                                      ~4      !0, 'name'
          5        ADD_ARRAY_ELEMENT                                ~3      ~4, 'productName'
    9     6        FETCH_DIM_R                                      ~5      !0, 'remark'
          7        ADD_ARRAY_ELEMENT                                ~3      ~5, 'desc'
          8      > RETURN                                                   ~3
   11     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Flon7l%3A5%240

Function test_array_map_use_local:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lon7l
function name:  test_array_map_use_local
number of ops:  10
compiled vars:  !0 = $data, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1        ASSIGN                                                   !1, 0
   17     2        INIT_FCALL                                               'array_map'
          3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Flon7l%3A17%241'
          4        BIND_LEXICAL                                             ~3, !1
   24     5        SEND_VAL                                                 ~3
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $4      
          8      > RETURN                                                   $4
   25     9*     > RETURN                                                   null

End of function test_array_map_use_local

Function %00%7Bclosure%7D%2Fin%2Flon7l%3A17%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lon7l
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $row, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   18     2        PRE_INC                                                  !1
   20     3        FETCH_DIM_R                                      ~3      !0, 'id'
          4        ADD                                              ~4      ~3, !1
          5        INIT_ARRAY                                       ~5      ~4, 'productId'
   21     6        FETCH_DIM_R                                      ~6      !0, 'name'
          7        ADD_ARRAY_ELEMENT                                ~5      ~6, 'productName'
   22     8        FETCH_DIM_R                                      ~7      !0, 'remark'
          9        ADD_ARRAY_ELEMENT                                ~5      ~7, 'desc'
         10      > RETURN                                                   ~5
   24    11*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Flon7l%3A17%241

Function test_foreach:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 18
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/lon7l
function name:  test_foreach
number of ops:  21
compiled vars:  !0 = $data, !1 = $result, !2 = $row, !3 = $tmp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   29     1        ASSIGN                                                   !1, <array>
   30     2      > FE_RESET_R                                       $5      !0, ->18
          3    > > FE_FETCH_R                                               $5, !2, ->18
   31     4    >   ASSIGN                                                   !3, <array>
   32     5        FETCH_DIM_R                                      ~8      !2, 'id'
          6        ADD                                              ~9      ~8, 1
          7        ASSIGN_DIM                                               !3, 'productId'
          8        OP_DATA                                                  ~9
   33     9        FETCH_DIM_R                                      ~11     !2, 'name'
         10        ASSIGN_DIM                                               !3, 'productName'
         11        OP_DATA                                                  ~11
   34    12        FETCH_DIM_R                                      ~13     !2, 'remark'
         13        ASSIGN_DIM                                               !3, 'desc'
         14        OP_DATA                                                  ~13
   35    15        ASSIGN_DIM                                               !1
         16        OP_DATA                                                  !3
   30    17      > JMP                                                      ->3
         18    >   FE_FREE                                                  $5
   37    19      > RETURN                                                   !1
   38    20*     > RETURN                                                   null

End of function test_foreach

Function test_foreach_use_local:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/lon7l
function name:  test_foreach_use_local
number of ops:  23
compiled vars:  !0 = $data, !1 = $result, !2 = $i, !3 = $row, !4 = $tmp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        ASSIGN                                                   !1, <array>
   43     2        ASSIGN                                                   !2, 0
   44     3      > FE_RESET_R                                       $7      !0, ->20
          4    > > FE_FETCH_R                                               $7, !3, ->20
   45     5    >   PRE_INC                                                  !2
   46     6        ASSIGN                                                   !4, <array>
   47     7        FETCH_DIM_R                                      ~11     !3, 'id'
          8        ADD                                              ~12     ~11, !2
          9        ASSIGN_DIM                                               !4, 'productId'
         10        OP_DATA                                                  ~12
   48    11        FETCH_DIM_R                                      ~14     !3, 'name'
         12        ASSIGN_DIM                                               !4, 'productName'
         13        OP_DATA                                                  ~14
   49    14        FETCH_DIM_R                                      ~16     !3, 'remark'
         15        ASSIGN_DIM                                               !4, 'desc'
         16        OP_DATA                                                  ~16
   50    17        ASSIGN_DIM                                               !1
         18        OP_DATA                                                  !4
   44    19      > JMP                                                      ->4
         20    >   FE_FREE                                                  $7
   52    21      > RETURN                                                   !1
   53    22*     > RETURN                                                   null

End of function test_foreach_use_local

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.23 ms | 1408 KiB | 23 Q