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 = 38, Position 2 = 46
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 46
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 46
2 jumps found. (Code = 77) Position 1 = 61, Position 2 = 69
Branch analysis from position: 61
2 jumps found. (Code = 78) Position 1 = 62, Position 2 = 69
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
Branch analysis from position: 46
Branch analysis from position: 22
filename:       /in/Ocq9Q
function name:  (null)
number of ops:  83
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
   26    23        INIT_FCALL                                               'memory_get_usage'
         24        DO_ICALL                                         $21     
         25        SUB                                              ~22     $21, !1
         26        ECHO                                                     ~22
         27        ECHO                                                     '%3Cbr+%2F%3E'
   37    28        INIT_FCALL                                               'memory_get_usage'
         29        DO_ICALL                                         $23     
         30        ASSIGN                                                   !5, $23
   38    31        DECLARE_CLASS                                            'multiplesiterator'
   72    32        NEW                                              $25     'MultiplesIterator'
         33        SEND_VAL_EX                                              7
         34        SEND_VAR_EX                                              !0
         35        DO_FCALL                                      0          
         36        ASSIGN                                                   !6, $25
   73    37      > FE_RESET_R                                       $28     !6, ->46
         38    > > FE_FETCH_R                                       ~29     $28, !3, ->46
         39    >   ASSIGN                                                   !4, ~29
   75    40        ROPE_INIT                                     4  ~32     !4
         41        ROPE_ADD                                      1  ~32     ~32, '+%3D%3E+'
         42        ROPE_ADD                                      2  ~32     ~32, !3
         43        ROPE_END                                      3  ~31     ~32, '%3Cbr+%2F%3E'
         44        ECHO                                                     ~31
   73    45      > JMP                                                      ->38
         46    >   FE_FREE                                                  $28
   78    47        INIT_FCALL                                               'memory_get_usage'
         48        DO_ICALL                                         $34     
         49        SUB                                              ~35     $34, !5
         50        ECHO                                                     ~35
         51        ECHO                                                     '%3Cbr+%2F%3E'
   80    52        INIT_FCALL                                               'memory_get_usage'
         53        DO_ICALL                                         $36     
         54        ASSIGN                                                   !7, $36
   91    55        INIT_FCALL                                               'multiplesdeg'
         56        SEND_VAL                                                 7
         57        SEND_VAR                                                 !0
         58        DO_FCALL                                      0  $38     
         59        ASSIGN                                                   !8, $38
   92    60      > FE_RESET_R                                       $40     !8, ->69
         61    > > FE_FETCH_R                                       ~41     $40, !3, ->69
         62    >   ASSIGN                                                   !4, ~41
   94    63        ROPE_INIT                                     4  ~44     !4
         64        ROPE_ADD                                      1  ~44     ~44, '+%3D%3E+'
         65        ROPE_ADD                                      2  ~44     ~44, !3
         66        ROPE_END                                      3  ~43     ~44, '%3Cbr+%2F%3E'
         67        ECHO                                                     ~43
   92    68      > JMP                                                      ->61
         69    >   FE_FREE                                                  $40
   96    70        INIT_FCALL                                               'memory_get_usage'
         71        DO_ICALL                                         $46     
         72        SUB                                              ~47     $46, !7
         73        ECHO                                                     ~47
   97    74        INIT_FCALL                                               'var_dump'
         75        INIT_FCALL                                               'multiplesdeg'
         76        SEND_VAL                                                 7
         77        SEND_VAR                                                 !0
         78        DO_FCALL                                      0  $48     
         79        INSTANCEOF                                       ~49     $48, 'Generator'
         80        SEND_VAL                                                 ~49
         81        DO_ICALL                                                 
         82      > 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/Ocq9Q
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/Ocq9Q
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/Ocq9Q
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/Ocq9Q
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/Ocq9Q
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/Ocq9Q
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/Ocq9Q
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/Ocq9Q
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:
254.17 ms | 1415 KiB | 22 Q