3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Proposal idea of enabling the unrolling in the userland. function measure(string $label, callable $fn): void { $start = hrtime(true); $result = $fn(); $end = hrtime(true); $elapsed = ($end - $start) / 1e9; echo "Sum: {$result}, {$label}: " . number_format($elapsed, 6) . " seconds\n"; } $array = range(1, 1e6); $n = count($array); measure("Normal for loop", function () use ($array, $n) { $sum = 0; for ($i = 0; $i < $n; $i++) { $sum += $array[$i]; } return $sum; }); // Sum: 500000500000, Normal for loop: 0.047172 seconds measure("Manual Unrolled x4", function () use ($array, $n) { $sum = 0; for ($i = 0; $i < $n; $i += 4) { $sum += $array[$i] + $array[$i + 1] + $array[$i + 2] + $array[$i + 3]; } return $sum; }); // Sum: 500000500000, Manual Unrolled x4: 0.042200 seconds measure("Manual Unrolled x8", function () use ($array, $n) { $sum = 0; for ($i = 0; $i < $n; $i += 8) { $sum += $array[$i] + $array[$i + 1] + $array[$i + 2] + $array[$i + 3] + $array[$i + 4] + $array[$i + 5] + $array[$i + 6] + $array[$i + 7]; } return $sum; }); // Sum: 500000500000, Manual Unrolled x8: 0.036472 seconds // Attributed Unroll - this is handled by your modified compiler measure("Attributed Unrolled #[Unroll(4)]", function () use ($array, $n) { $sum = 0; // @TODO: KhaledAlam: Unrolling logic of zend_compile_for( ) // #[Unroll(4)] for ($i = 0; $i < $n; $i++) { $sum += $array[$i]; } return $sum; }); // Sum: 0, Attributed Unrolled #[Unroll(4)]: 0.000002 seconds
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T23MH
function name:  (null)
number of ops:  36
compiled vars:  !0 = $array, !1 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 1
          2        SEND_VAL                                                 1.0e+6
          3        DO_ICALL                                         $2      
          4        ASSIGN                                                   !0, $2
   15     5        COUNT                                            ~4      !0
          6        ASSIGN                                                   !1, ~4
   17     7        INIT_FCALL                                               'measure'
          8        SEND_VAL                                                 'Normal+for+loop'
          9        DECLARE_LAMBDA_FUNCTION                          ~6      [0]
         10        BIND_LEXICAL                                             ~6, !0
         11        BIND_LEXICAL                                             ~6, !1
   23    12        SEND_VAL                                                 ~6
   17    13        DO_FCALL                                      0          
   25    14        INIT_FCALL                                               'measure'
         15        SEND_VAL                                                 'Manual+Unrolled+x4'
         16        DECLARE_LAMBDA_FUNCTION                          ~8      [1]
         17        BIND_LEXICAL                                             ~8, !0
         18        BIND_LEXICAL                                             ~8, !1
   31    19        SEND_VAL                                                 ~8
   25    20        DO_FCALL                                      0          
   33    21        INIT_FCALL                                               'measure'
         22        SEND_VAL                                                 'Manual+Unrolled+x8'
         23        DECLARE_LAMBDA_FUNCTION                          ~10     [2]
         24        BIND_LEXICAL                                             ~10, !0
         25        BIND_LEXICAL                                             ~10, !1
   40    26        SEND_VAL                                                 ~10
   33    27        DO_FCALL                                      0          
   43    28        INIT_FCALL                                               'measure'
         29        SEND_VAL                                                 'Attributed+Unrolled+%23%5BUnroll%284%29%5D'
         30        DECLARE_LAMBDA_FUNCTION                          ~12     [3]
         31        BIND_LEXICAL                                             ~12, !0
         32        BIND_LEXICAL                                             ~12, !1
   51    33        SEND_VAL                                                 ~12
   43    34        DO_FCALL                                      0          
   51    35      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 5
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 5
Branch analysis from position: 10
Branch analysis from position: 5
filename:       /in/T23MH
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $array, !1 = $n, !2 = $sum, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   18     2        ASSIGN                                                   !2, 0
   19     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->8
   20     5    >   FETCH_DIM_R                                      ~6      !0, !3
          6        ASSIGN_OP                                     1          !2, ~6
   19     7        PRE_INC                                                  !3
          8    >   IS_SMALLER                                               !3, !1
          9      > JMPNZ                                                    ~9, ->5
   22    10    > > RETURN                                                   !2
   23    11*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 5
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 5
Branch analysis from position: 19
Branch analysis from position: 5
filename:       /in/T23MH
function name:  {closure}
number of ops:  21
compiled vars:  !0 = $array, !1 = $n, !2 = $sum, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   26     2        ASSIGN                                                   !2, 0
   27     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->17
   28     5    >   FETCH_DIM_R                                      ~6      !0, !3
          6        ADD                                              ~7      !3, 1
          7        FETCH_DIM_R                                      ~8      !0, ~7
          8        ADD                                              ~9      ~6, ~8
          9        ADD                                              ~10     !3, 2
         10        FETCH_DIM_R                                      ~11     !0, ~10
         11        ADD                                              ~12     ~9, ~11
         12        ADD                                              ~13     !3, 3
         13        FETCH_DIM_R                                      ~14     !0, ~13
         14        ADD                                              ~15     ~12, ~14
         15        ASSIGN_OP                                     1          !2, ~15
   27    16        ASSIGN_OP                                     1          !3, 4
         17    >   IS_SMALLER                                               !3, !1
         18      > JMPNZ                                                    ~18, ->5
   30    19    > > RETURN                                                   !2
   31    20*     > RETURN                                                   null

End of Dynamic Function 1

Dynamic Function 2
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 = 5
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 5
Branch analysis from position: 31
Branch analysis from position: 5
filename:       /in/T23MH
function name:  {closure}
number of ops:  33
compiled vars:  !0 = $array, !1 = $n, !2 = $sum, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   34     2        ASSIGN                                                   !2, 0
   35     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->29
   36     5    >   FETCH_DIM_R                                      ~6      !0, !3
          6        ADD                                              ~7      !3, 1
          7        FETCH_DIM_R                                      ~8      !0, ~7
          8        ADD                                              ~9      ~6, ~8
          9        ADD                                              ~10     !3, 2
         10        FETCH_DIM_R                                      ~11     !0, ~10
         11        ADD                                              ~12     ~9, ~11
         12        ADD                                              ~13     !3, 3
         13        FETCH_DIM_R                                      ~14     !0, ~13
         14        ADD                                              ~15     ~12, ~14
   37    15        ADD                                              ~16     !3, 4
         16        FETCH_DIM_R                                      ~17     !0, ~16
         17        ADD                                              ~18     ~15, ~17
         18        ADD                                              ~19     !3, 5
         19        FETCH_DIM_R                                      ~20     !0, ~19
         20        ADD                                              ~21     ~18, ~20
         21        ADD                                              ~22     !3, 6
         22        FETCH_DIM_R                                      ~23     !0, ~22
         23        ADD                                              ~24     ~21, ~23
         24        ADD                                              ~25     !3, 7
         25        FETCH_DIM_R                                      ~26     !0, ~25
         26        ADD                                              ~27     ~24, ~26
         27        ASSIGN_OP                                     1          !2, ~27
   35    28        ASSIGN_OP                                     1          !3, 8
         29    >   IS_SMALLER                                               !3, !1
         30      > JMPNZ                                                    ~30, ->5
   39    31    > > RETURN                                                   !2
   40    32*     > RETURN                                                   null

End of Dynamic Function 2

Dynamic Function 3
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 5
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 5
Branch analysis from position: 10
Branch analysis from position: 5
filename:       /in/T23MH
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $array, !1 = $n, !2 = $sum, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   44     2        ASSIGN                                                   !2, 0
   47     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->8
   48     5    >   FETCH_DIM_R                                      ~6      !0, !3
          6        ASSIGN_OP                                     1          !2, ~6
   47     7        PRE_INC                                                  !3
          8    >   IS_SMALLER                                               !3, !1
          9      > JMPNZ                                                    ~9, ->5
   50    10    > > RETURN                                                   !2
   51    11*     > RETURN                                                   null

End of Dynamic Function 3

Function measure:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T23MH
function name:  measure
number of ops:  29
compiled vars:  !0 = $label, !1 = $fn, !2 = $start, !3 = $result, !4 = $end, !5 = $elapsed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        INIT_FCALL                                               'hrtime'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !2, $6
    7     6        INIT_DYNAMIC_CALL                                        !1
          7        DO_FCALL                                      0  $8      
          8        ASSIGN                                                   !3, $8
    8     9        INIT_FCALL                                               'hrtime'
         10        SEND_VAL                                                 <true>
         11        DO_ICALL                                         $10     
         12        ASSIGN                                                   !4, $10
    9    13        SUB                                              ~12     !4, !2
         14        DIV                                              ~13     ~12, 1.0e+9
         15        ASSIGN                                                   !5, ~13
   10    16        ROPE_INIT                                     5  ~16     'Sum%3A+'
         17        ROPE_ADD                                      1  ~16     ~16, !3
         18        ROPE_ADD                                      2  ~16     ~16, '%2C+'
         19        ROPE_ADD                                      3  ~16     ~16, !0
         20        ROPE_END                                      4  ~15     ~16, '%3A+'
         21        INIT_FCALL                                               'number_format'
         22        SEND_VAR                                                 !5
         23        SEND_VAL                                                 6
         24        DO_ICALL                                         $19     
         25        CONCAT                                           ~20     ~15, $19
         26        CONCAT                                           ~21     ~20, '+seconds%0A'
         27        ECHO                                                     ~21
   11    28      > RETURN                                                   null

End of function measure

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.04 ms | 1015 KiB | 20 Q