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