3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IteratorV1 implements Iterator { private $index; public $table; function __construct(SplFixedArray $table){ $this->table = $table; $this->rewind(); } function rewind(){ $this->index = -1; $this->next(); } function next(){ while( ++$this->index < $this->table->count() && $this->table->offsetGet($this->index) === NULL ); } function valid(){ return $this->index < $this->table->count(); } function key(){ return $this->table->offsetGet($this->index)->uncommentMe; } function current(){ return $this->table->offsetGet($this->index)->uncommentMe; } } class IteratorV2 implements Iterator { private $index; private $array; private $entry; function __construct(SplFixedArray $array){ $this->array = $array; $this->rewind(); } function rewind(){ for( $this->index = 0; $this->index < $this->array->count(); $this->index++ ){ $this->entry = $this->array->offsetGet($this->index); if($this->entry !== NULL){ return; } } $this->entry = NULL; } function next(){ for( $this->index++; $this->index < $this->array->count(); $this->index++ ){ $this->entry = $this->array->offsetGet($this->index); if($this->entry !== NULL){ return; } } $this->entry = NULL; } function valid(){ return $this->entry !== NULL; } function key(){ return $this->entry->uncommentMe; } function current(){ return $this->entry->uncommentMe; } } class Entry{ public $uncommentMe = 123; } $array = new SplFixedArray(2 ** 18); for($i = 0; $i < (2 ** 18) / 2; $i++){ $array->offsetSet($i * 2, new Entry); } echo "should be slower:\n"; $st = microtime(true); foreach(new IteratorV1($array) as $key => $content){} echo number_format(microtime(true) - $st, 10) . "s\n"; echo "should be faster:\n"; $st = microtime(true); foreach(new IteratorV2($array) as $key => $content){} echo number_format(microtime(true) - $st, 10) . "s\n";
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 = 8
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 30
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 30
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 30
2 jumps found. (Code = 77) Position 1 = 50, Position 2 = 53
Branch analysis from position: 50
2 jumps found. (Code = 78) Position 1 = 51, Position 2 = 53
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
Branch analysis from position: 30
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 8
Branch analysis from position: 18
Branch analysis from position: 8
filename:       /in/3Gr0Y
function name:  (null)
number of ops:  65
compiled vars:  !0 = $array, !1 = $i, !2 = $st, !3 = $content, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'iteratorv1'
   38     1        DECLARE_CLASS                                            'iteratorv2'
   88     2        NEW                                              $5      'SplFixedArray'
          3        SEND_VAL_EX                                              262144
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $5
   89     6        ASSIGN                                                   !1, 0
          7      > JMP                                                      ->16
   90     8    >   INIT_METHOD_CALL                                         !0, 'offsetSet'
          9        MUL                                              ~9      !1, 2
         10        SEND_VAL_EX                                              ~9
         11        NEW                                              $10     'Entry'
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $10
         14        DO_FCALL                                      0          
   89    15        PRE_INC                                                  !1
         16    >   IS_SMALLER                                               !1, 131072
         17      > JMPNZ                                                    ~14, ->8
   94    18    >   ECHO                                                     'should+be+slower%3A%0A'
   95    19        INIT_FCALL                                               'microtime'
         20        SEND_VAL                                                 <true>
         21        DO_ICALL                                         $15     
         22        ASSIGN                                                   !2, $15
   96    23        NEW                                              $17     'IteratorV1'
         24        SEND_VAR_EX                                              !0
         25        DO_FCALL                                      0          
         26      > FE_RESET_R                                       $19     $17, ->30
         27    > > FE_FETCH_R                                       ~20     $19, !3, ->30
         28    >   ASSIGN                                                   !4, ~20
         29      > JMP                                                      ->27
         30    >   FE_FREE                                                  $19
   97    31        INIT_FCALL                                               'number_format'
         32        INIT_FCALL                                               'microtime'
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $22     
         35        SUB                                              ~23     $22, !2
         36        SEND_VAL                                                 ~23
         37        SEND_VAL                                                 10
         38        DO_ICALL                                         $24     
         39        CONCAT                                           ~25     $24, 's%0A'
         40        ECHO                                                     ~25
  100    41        ECHO                                                     'should+be+faster%3A%0A'
  101    42        INIT_FCALL                                               'microtime'
         43        SEND_VAL                                                 <true>
         44        DO_ICALL                                         $26     
         45        ASSIGN                                                   !2, $26
  102    46        NEW                                              $28     'IteratorV2'
         47        SEND_VAR_EX                                              !0
         48        DO_FCALL                                      0          
         49      > FE_RESET_R                                       $30     $28, ->53
         50    > > FE_FETCH_R                                       ~31     $30, !3, ->53
         51    >   ASSIGN                                                   !4, ~31
         52      > JMP                                                      ->50
         53    >   FE_FREE                                                  $30
  103    54        INIT_FCALL                                               'number_format'
         55        INIT_FCALL                                               'microtime'
         56        SEND_VAL                                                 <true>
         57        DO_ICALL                                         $33     
         58        SUB                                              ~34     $33, !2
         59        SEND_VAL                                                 ~34
         60        SEND_VAL                                                 10
         61        DO_ICALL                                         $35     
         62        CONCAT                                           ~36     $35, 's%0A'
         63        ECHO                                                     ~36
         64      > RETURN                                                   1

Class IteratorV1:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  __construct
number of ops:  6
compiled vars:  !0 = $table
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
    9     1        ASSIGN_OBJ                                               'table'
          2        OP_DATA                                                  !0
   10     3        INIT_METHOD_CALL                                         'rewind'
          4        DO_FCALL                                      0          
   11     5      > RETURN                                                   null

End of function __construct

Function rewind:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  rewind
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   ASSIGN_OBJ                                               'index'
          1        OP_DATA                                                  -1
   15     2        INIT_METHOD_CALL                                         'next'
          3        DO_FCALL                                      0          
   16     4      > RETURN                                                   null

End of function rewind

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 1
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 1
Branch analysis from position: 15
filename:       /in/3Gr0Y
function name:  next
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E > > JMP                                                      ->1
          1    >   PRE_INC_OBJ                                      ~0      'index'
          2        FETCH_OBJ_R                                      ~1      'table'
          3        INIT_METHOD_CALL                                         ~1, 'count'
          4        DO_FCALL                                      0  $2      
          5        IS_SMALLER                                       ~3      ~0, $2
          6      > JMPZ_EX                                          ~3      ~3, ->15
   21     7    >   FETCH_OBJ_R                                      ~4      'table'
          8        INIT_METHOD_CALL                                         ~4, 'offsetGet'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_OBJ_FUNC_ARG                               $5      'index'
         11        SEND_FUNC_ARG                                            $5
         12        DO_FCALL                                      0  $6      
         13        TYPE_CHECK                                    2  ~7      $6
         14        BOOL                                             ~3      ~7
         15    > > JMPNZ                                                    ~3, ->1
   23    16    > > RETURN                                                   null

End of function next

Function valid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  valid
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   FETCH_OBJ_R                                      ~0      'index'
          1        FETCH_OBJ_R                                      ~1      'table'
          2        INIT_METHOD_CALL                                         ~1, 'count'
          3        DO_FCALL                                      0  $2      
          4        IS_SMALLER                                       ~3      ~0, $2
          5      > RETURN                                                   ~3
   27     6*     > RETURN                                                   null

End of function valid

Function key:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  key
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   FETCH_OBJ_R                                      ~0      'table'
          1        INIT_METHOD_CALL                                         ~0, 'offsetGet'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'index'
          4        SEND_FUNC_ARG                                            $1
          5        DO_FCALL                                      0  $2      
          6        FETCH_OBJ_R                                      ~3      $2, 'uncommentMe'
          7      > RETURN                                                   ~3
   31     8*     > RETURN                                                   null

End of function key

Function current:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  current
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   FETCH_OBJ_R                                      ~0      'table'
          1        INIT_METHOD_CALL                                         ~0, 'offsetGet'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'index'
          4        SEND_FUNC_ARG                                            $1
          5        DO_FCALL                                      0  $2      
          6        FETCH_OBJ_R                                      ~3      $2, 'uncommentMe'
          7      > RETURN                                                   ~3
   35     8*     > RETURN                                                   null

End of function current

End of class IteratorV1.

Class IteratorV2:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  __construct
number of ops:  6
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   45     1        ASSIGN_OBJ                                               'array'
          2        OP_DATA                                                  !0
   46     3        INIT_METHOD_CALL                                         'rewind'
          4        DO_FCALL                                      0          
   47     5      > RETURN                                                   null

End of function __construct

Function rewind:
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 = 22, Position 2 = 3
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 3
Branch analysis from position: 22
Branch analysis from position: 3
filename:       /in/3Gr0Y
function name:  rewind
number of ops:  25
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   ASSIGN_OBJ                                               'index'
          1        OP_DATA                                                  0
          2      > JMP                                                      ->16
   55     3    >   FETCH_OBJ_R                                      ~2      'array'
          4        INIT_METHOD_CALL                                         ~2, 'offsetGet'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $3      'index'
          7        SEND_FUNC_ARG                                            $3
          8        DO_FCALL                                      0  $4      
          9        ASSIGN_OBJ                                               'entry'
         10        OP_DATA                                                  $4
   56    11        FETCH_OBJ_R                                      ~5      'entry'
         12        TYPE_CHECK                                  1020          ~5
         13      > JMPZ                                                     ~6, ->15
         14    > > RETURN                                                   null
   53    15    >   PRE_INC_OBJ                                              'index'
   52    16    >   FETCH_OBJ_R                                      ~8      'index'
         17        FETCH_OBJ_R                                      ~9      'array'
         18        INIT_METHOD_CALL                                         ~9, 'count'
         19        DO_FCALL                                      0  $10     
         20        IS_SMALLER                                               ~8, $10
         21      > JMPNZ                                                    ~11, ->3
   58    22    >   ASSIGN_OBJ                                               'entry'
         23        OP_DATA                                                  null
   59    24      > RETURN                                                   null

End of function rewind

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 2
Branch analysis from position: 21
Branch analysis from position: 2
filename:       /in/3Gr0Y
function name:  next
number of ops:  24
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   PRE_INC_OBJ                                              'index'
          1      > JMP                                                      ->15
   67     2    >   FETCH_OBJ_R                                      ~2      'array'
          3        INIT_METHOD_CALL                                         ~2, 'offsetGet'
          4        CHECK_FUNC_ARG                                           
          5        FETCH_OBJ_FUNC_ARG                               $3      'index'
          6        SEND_FUNC_ARG                                            $3
          7        DO_FCALL                                      0  $4      
          8        ASSIGN_OBJ                                               'entry'
          9        OP_DATA                                                  $4
   68    10        FETCH_OBJ_R                                      ~5      'entry'
         11        TYPE_CHECK                                  1020          ~5
         12      > JMPZ                                                     ~6, ->14
         13    > > RETURN                                                   null
   65    14    >   PRE_INC_OBJ                                              'index'
   64    15    >   FETCH_OBJ_R                                      ~8      'index'
         16        FETCH_OBJ_R                                      ~9      'array'
         17        INIT_METHOD_CALL                                         ~9, 'count'
         18        DO_FCALL                                      0  $10     
         19        IS_SMALLER                                               ~8, $10
         20      > JMPNZ                                                    ~11, ->2
   70    21    >   ASSIGN_OBJ                                               'entry'
         22        OP_DATA                                                  null
   71    23      > RETURN                                                   null

End of function next

Function valid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  valid
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   FETCH_OBJ_R                                      ~0      'entry'
          1        TYPE_CHECK                                  1020  ~1      ~0
          2      > RETURN                                                   ~1
   75     3*     > RETURN                                                   null

End of function valid

Function key:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  key
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   FETCH_OBJ_R                                      ~0      'entry'
          1        FETCH_OBJ_R                                      ~1      ~0, 'uncommentMe'
          2      > RETURN                                                   ~1
   79     3*     > RETURN                                                   null

End of function key

Function current:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Gr0Y
function name:  current
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   FETCH_OBJ_R                                      ~0      'entry'
          1        FETCH_OBJ_R                                      ~1      ~0, 'uncommentMe'
          2      > RETURN                                                   ~1
   83     3*     > RETURN                                                   null

End of function current

End of class IteratorV2.

Class Entry: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
134.11 ms | 1421 KiB | 17 Q