English 中文(简体)
php5: does the copy of an array for a foreach incur overhead?
原标题:

Is the copy of an array for a foreach (of php5, in this case) an immediate copy with actual overhead, or merely a lazy copy (copy on write) which only produces overhead if it detects a write manipulation?

The alternative, note in several places, is to run foreach over keys($array) -- how can that really be faster?

最佳回答

OK, so I went off 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
---------------------------  ----------------------------  -------------------------

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

问题回答

PHP uses copy-on-write. It s slower to pass by reference because it has to setup the data structures to maintain the reference.

OK, here is the other requested comparison (what do you think?)

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




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签