3v4l.org

run code in 300+ PHP versions simultaneously
<?php $max = 50; //Fonction qui retourne les multiples d'un diviseur <= max //Logique de calculs $memoryF = memory_get_usage(); function multiplesDe($diviseur, $max) { $result = array(); for($i=0; $i<=$max; $i+=$diviseur) { $result[] = $i; //L'ensemble des résultats dont chargés en mémoire par la fonction } return $result; } //Logique d'affichage //var_dump(multiplesDe(7,$max)); $multiplesDeSept = multiplesDe(7,$max); //Ici se trouve le gros stockage mémoire! foreach(multiplesDe(7,$max) as $i => $multiples) { echo "$i => $multiples<br />"; } // echo memory_get_usage()-$memoryF,"<br />"; //var_dump($multiplesDeSept); //On implémente la classe PHP5 Iterator /* http://fr2.php.net/manual/fr/class.iterator.php abstract public mixed current ( void ) OK abstract public scalar key ( void ) OK abstract public void next ( void ) OK abstract public void rewind ( void ) OK (implémentée) abstract public boolean valid ( void ) OK */ $memoryI = memory_get_usage(); class MultiplesIterator implements Iterator { protected $diviseur,$max; //parametres calculs protected $current, $indiceCurrent; public function __construct($diviseur, $max) { $this->diviseur = $diviseur; $this->max = $max; } public function rewind() { // On revient au début! $this->current = 0; $this->indiceCurrent = 0; } public function valid() { // Condition de fin de boucle! return ($this->current <= $this->max); } public function current() { // retourne la valeur courante ! return $this->current; } public function key() { // retourne l'indice de la velru courante(la clé!) return $this->indiceCurrent; } public function next() { // fait passer au prochain élément! $this->indiceCurrent += 1; $this->current += $this->diviseur; } } $iterator = new MultiplesIterator(7,$max); foreach($iterator as $i => $multiples) { echo "$i => $multiples<br />"; } //Stockage de l'ensemble des résultats dans une liste > peu gourmand. //echo memory_get_usage()-$memoryI,"<br />"; $memoryG = memory_get_usage(); // on implémente la classe Generator! // Yield: Lorsque yield est atteint, la valeur qu’il spécifie est renvoyée à l’appelant, et l’exécution de la fonction est mise en pause // jusqu’à ce que l’appelant demande à ce qu’elle soit continuée. function multiplesDeG($diviseur, $max) { for($i=0,$n=0; $n<=$max; $i++,$n+=$diviseur) { yield $i => $n; //On renvoit directement la valeur à l'appelant: } } $listeMultiples = multiplesDeG(7, $max); foreach($listeMultiples as $i => $multiples) //foreach(multiplesDeG(7, $max) as $i => $multiples) fonctionne aussi! { echo "$i => $multiples<br />"; } // echo memory_get_usage()-$memoryG; var_dump(multiplesDeG(7, $max) instanceof Generator);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 22
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 22
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 33, Position 2 = 41
Branch analysis from position: 33
2 jumps found. (Code = 78) Position 1 = 34, Position 2 = 41
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 51, Position 2 = 59
Branch analysis from position: 51
2 jumps found. (Code = 78) Position 1 = 52, Position 2 = 59
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
Branch analysis from position: 41
Branch analysis from position: 22
filename:       /in/UrRC0
function name:  (null)
number of ops:  69
compiled vars:  !0 = $max, !1 = $memoryF, !2 = $multiplesDeSept, !3 = $multiples, !4 = $i, !5 = $memoryI, !6 = $iterator, !7 = $memoryG, !8 = $listeMultiples
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 50
    7     1        INIT_FCALL                                               'memory_get_usage'
          2        DO_ICALL                                         $10     
          3        ASSIGN                                                   !1, $10
   21     4        INIT_FCALL                                               'multiplesde'
          5        SEND_VAL                                                 7
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $12     
          8        ASSIGN                                                   !2, $12
   22     9        INIT_FCALL                                               'multiplesde'
         10        SEND_VAL                                                 7
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $14     
         13      > FE_RESET_R                                       $15     $14, ->22
         14    > > FE_FETCH_R                                       ~16     $15, !3, ->22
         15    >   ASSIGN                                                   !4, ~16
   24    16        ROPE_INIT                                     4  ~19     !4
         17        ROPE_ADD                                      1  ~19     ~19, '+%3D%3E+'
         18        ROPE_ADD                                      2  ~19     ~19, !3
         19        ROPE_END                                      3  ~18     ~19, '%3Cbr+%2F%3E'
         20        ECHO                                                     ~18
   22    21      > JMP                                                      ->14
         22    >   FE_FREE                                                  $15
   37    23        INIT_FCALL                                               'memory_get_usage'
         24        DO_ICALL                                         $21     
         25        ASSIGN                                                   !5, $21
   38    26        DECLARE_CLASS                                            'multiplesiterator'
   72    27        NEW                                              $23     'MultiplesIterator'
         28        SEND_VAL_EX                                              7
         29        SEND_VAR_EX                                              !0
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !6, $23
   73    32      > FE_RESET_R                                       $26     !6, ->41
         33    > > FE_FETCH_R                                       ~27     $26, !3, ->41
         34    >   ASSIGN                                                   !4, ~27
   75    35        ROPE_INIT                                     4  ~30     !4
         36        ROPE_ADD                                      1  ~30     ~30, '+%3D%3E+'
         37        ROPE_ADD                                      2  ~30     ~30, !3
         38        ROPE_END                                      3  ~29     ~30, '%3Cbr+%2F%3E'
         39        ECHO                                                     ~29
   73    40      > JMP                                                      ->33
         41    >   FE_FREE                                                  $26
   80    42        INIT_FCALL                                               'memory_get_usage'
         43        DO_ICALL                                         $32     
         44        ASSIGN                                                   !7, $32
   91    45        INIT_FCALL                                               'multiplesdeg'
         46        SEND_VAL                                                 7
         47        SEND_VAR                                                 !0
         48        DO_FCALL                                      0  $34     
         49        ASSIGN                                                   !8, $34
   92    50      > FE_RESET_R                                       $36     !8, ->59
         51    > > FE_FETCH_R                                       ~37     $36, !3, ->59
         52    >   ASSIGN                                                   !4, ~37
   94    53        ROPE_INIT                                     4  ~40     !4
         54        ROPE_ADD                                      1  ~40     ~40, '+%3D%3E+'
         55        ROPE_ADD                                      2  ~40     ~40, !3
         56        ROPE_END                                      3  ~39     ~40, '%3Cbr+%2F%3E'
         57        ECHO                                                     ~39
   92    58      > JMP                                                      ->51
         59    >   FE_FREE                                                  $36
   97    60        INIT_FCALL                                               'var_dump'
         61        INIT_FCALL                                               'multiplesdeg'
         62        SEND_VAL                                                 7
         63        SEND_VAR                                                 !0
         64        DO_FCALL                                      0  $42     
         65        INSTANCEOF                                       ~43     $42, 'Generator'
         66        SEND_VAL                                                 ~43
         67        DO_ICALL                                                 
         68      > RETURN                                                   1

Function multiplesde:
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/UrRC0
function name:  multiplesDe
number of ops:  12
compiled vars:  !0 = $diviseur, !1 = $max, !2 = $result, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        ASSIGN                                                   !2, <array>
   11     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->8
   13     5    >   ASSIGN_DIM                                               !2
          6        OP_DATA                                                  !3
   11     7        ASSIGN_OP                                     1          !3, !0
          8    >   IS_SMALLER_OR_EQUAL                                      !3, !1
          9      > JMPNZ                                                    ~8, ->5
   16    10    > > RETURN                                                   !2
   17    11*     > RETURN                                                   null

End of function multiplesde

Function multiplesdeg:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 6
Branch analysis from position: 11
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 6
Branch analysis from position: 11
Branch analysis from position: 6
filename:       /in/UrRC0
function name:  multiplesDeG
number of ops:  12
compiled vars:  !0 = $diviseur, !1 = $max, !2 = $i, !3 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   86     3        ASSIGN                                                   !2, 0
          4        ASSIGN                                                   !3, 0
          5      > JMP                                                      ->9
   88     6    >   YIELD                                                    !3, !2
   86     7        PRE_INC                                                  !2
          8        ASSIGN_OP                                     1          !3, !0
          9    >   IS_SMALLER_OR_EQUAL                                      !3, !1
         10      > JMPNZ                                                    ~9, ->6
   90    11    > > GENERATOR_RETURN                                         

End of function multiplesdeg

Class MultiplesIterator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrRC0
function name:  __construct
number of ops:  7
compiled vars:  !0 = $diviseur, !1 = $max
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   45     2        ASSIGN_OBJ                                               'diviseur'
          3        OP_DATA                                                  !0
   46     4        ASSIGN_OBJ                                               'max'
          5        OP_DATA                                                  !1
   47     6      > 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/UrRC0
function name:  rewind
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   ASSIGN_OBJ                                               'current'
          1        OP_DATA                                                  0
   51     2        ASSIGN_OBJ                                               'indiceCurrent'
          3        OP_DATA                                                  0
   52     4      > RETURN                                                   null

End of function rewind

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

End of function valid

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

End of function current

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

End of function key

Function next:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UrRC0
function name:  next
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   ASSIGN_OBJ_OP                                 1          'indiceCurrent'
          1        OP_DATA                                                  1
   68     2        FETCH_OBJ_R                                      ~2      'diviseur'
          3        ASSIGN_OBJ_OP                                 1          'current'
          4        OP_DATA                                                  ~2
   69     5      > RETURN                                                   null

End of function next

End of class MultiplesIterator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.37 ms | 1411 KiB | 21 Q