Php5: does a “copy” of the foreach array do overhead?

Is the “copy” of the array for foreach (in this case php5) an immediate copy with actual overhead, or just a lazy copy (copy on write) that only gives overhead if it detects write manipulation?

An alternative, a note in several places, is to run foreach over keys ($ array) - how can this be faster?

+3
source share
3 answers

OK, so I left and measured it -

Test This                    And This                       And, uh, This      
---------------------------  ----------------------------   ----------------------------
Setup                        Setup                          Setup                     
---------------------------- ----------------------------   ----------------------------
$i = array_fill(0,1000,'1'); $j = array_fill(0,1000,'1');   $j = array_fill(0,1000,'1');
$c = 0;                      $d = 0;                        $e = 0;
---------------------------  ----------------------------   ---------------------------
Code Under Test              Code Under Test                Code Under Test                    
---------------------------  ------------------------------ --------------------------
foreach ($i as $v)           foreach (array_keys($j) as $k) foreach ($i as &$v)
{ $c+= $v; }                 { $d+= $j[$k]; }               { $e+= $v; }    
---------------------------  ------------------------------ -------------------------
Tear Down                    Tear Down                      Tear Down                          
---------------------------  -----------------------------  -------------------------
print "c = $c";             print "d = $d";                 print "e = $e";                    
---------------------------- -----------------------------  --------------------------
Test for repetitions.  10000         
---------------------------- -----------------------------  -------------------------
c = 10000000                 d = 10000000                   e = 10000000
---------------------------- -----------------------------  -------------------------
Ran in  1.8540189266205      Ran in 4.0039160251617         Ran in 1.9633851051331
---------------------------  -----------------------------  -------------------------
Winner -0.10936617851257     Looser 2.0405309200287         2nd Best             0
---------------------------  ----------------------------  -------------------------

It seems that foreach ($ a as $ v) is much better than array_keys, and that using & v is in the middle.

+2
source

PHP copy-on-write. , .

+1

, ( ?)

Test This                                 And This                            
---------------------------------------   --------------------------------------
Setup                                     Setup                               
--------------------------------------    --------------------------------------
$k = array_fill(0,1000,'1');              $i = array_fill(0,1000,'1');        
$f = 0;                                   $c = 0;                             
$kMax = count($k);                                                            
--------------------------------------    --------------------------------------
Code Under Test                           Code Under Test                     
--------------------------------------    --------------------------------------
for ($x = 0; $x < kiMax; $x++)            foreach ($i as $v)                  
{ $f+= $k[$x]; }                          { $c+= $v; }                        
--------------------------------------    --------------------------------------
Tear Down                                 Tear Down                           
--------------------------------------    --------------------------------------
print "f = $f";                           print "c = $c";                      
--------------------------------------    --------------------------------------
Test for repetitions:  10,000
--------------------------------------    --------------------------------------
f = 10000000                              c = 10000000
--------------------------------------    --------------------------------------
Ran in 2.8563051223755                    Ran in 1.8667521476746
--------------------------------------    -------------------------------------
2nd best      0.98955297470093            Winner               0
+1

Source: https://habr.com/ru/post/1722959/


All Articles