久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <i id='4Xcix'><tr id='4Xcix'><dt id='4Xcix'><q id='4Xcix'><span id='4Xcix'><b id='4Xcix'><form id='4Xcix'><ins id='4Xcix'></ins><ul id='4Xcix'></ul><sub id='4Xcix'></sub></form><legend id='4Xcix'></legend><bdo id='4Xcix'><pre id='4Xcix'><center id='4Xcix'></center></pre></bdo></b><th id='4Xcix'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4Xcix'><tfoot id='4Xcix'></tfoot><dl id='4Xcix'><fieldset id='4Xcix'></fieldset></dl></div>
  • <tfoot id='4Xcix'></tfoot>

    <legend id='4Xcix'><style id='4Xcix'><dir id='4Xcix'><q id='4Xcix'></q></dir></style></legend>

    <small id='4Xcix'></small><noframes id='4Xcix'>

      <bdo id='4Xcix'></bdo><ul id='4Xcix'></ul>

        php foreach,為什么使用數組的引用傳遞很快?

        php foreach, why using pass by reference of a array is fast?(php foreach,為什么使用數組的引用傳遞很快?)

          <i id='vEbBj'><tr id='vEbBj'><dt id='vEbBj'><q id='vEbBj'><span id='vEbBj'><b id='vEbBj'><form id='vEbBj'><ins id='vEbBj'></ins><ul id='vEbBj'></ul><sub id='vEbBj'></sub></form><legend id='vEbBj'></legend><bdo id='vEbBj'><pre id='vEbBj'><center id='vEbBj'></center></pre></bdo></b><th id='vEbBj'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vEbBj'><tfoot id='vEbBj'></tfoot><dl id='vEbBj'><fieldset id='vEbBj'></fieldset></dl></div>
          <legend id='vEbBj'><style id='vEbBj'><dir id='vEbBj'><q id='vEbBj'></q></dir></style></legend>
              <bdo id='vEbBj'></bdo><ul id='vEbBj'></ul>
                <tbody id='vEbBj'></tbody>

              1. <tfoot id='vEbBj'></tfoot>

                  <small id='vEbBj'></small><noframes id='vEbBj'>

                • 本文介紹了php foreach,為什么使用數組的引用傳遞很快?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  下面是一個大數組的php foreach循環的測試,我認為如果$v不改變,真正的copy就不會發生,因為copy on write,但為什么按引用傳遞速度很快?

                  Below is a test of php foreach loop of a big array, I thought that if the $v don't change, the real copy will not happen because of copy on write, but why it is fast when pass by reference?

                  代碼 1:

                  function test1($a){
                    $c = 0;
                    foreach($a as $v){ if($v=='xxxxx') ++$c; }
                  }
                  
                  function test2(&$a){
                    $c = 0;
                    foreach($a as $v){ if($v=='xxxxx') ++$c; }
                  }
                  
                  $x = array_fill(0, 100000, 'xxxxx');
                  
                  $begin = microtime(true);
                  test1($x);
                  $end1 = microtime(true);
                  test2($x);
                  $end2 = microtime(true);
                  
                  echo $end1 - $begin . "
                  ";   //0.03320002555847
                  echo $end2 - $end1;           //0.02147388458252
                  

                  但這一次,使用傳遞引用很慢.

                  But this time, using pass by reference is slow.

                  代碼 2:

                  function test1($a){
                    $cnt = count($a); $c = 0;
                    for($i=0; $i<$cnt; ++$i)
                      if($a[$i]=='xxxxx') ++$c;
                  }
                  function test2(&$a){
                    $cnt = count($a); $c = 0;
                    for($i=0; $i<$cnt; ++$i)
                      if($a[$i]=='xxxxx') ++$c;
                  }
                  $x = array_fill(0, 100000, 'xxxxx');
                  
                  $begin = microtime(true);
                  test1($x);
                  $end1 = microtime(true);
                  test2($x);
                  $end2 = microtime(true);
                  
                  echo $end1 - $begin . "
                  ";   //0.024326801300049
                  echo $end2 - $end1;           //0.037616014480591
                  

                  誰能解釋一下為什么通過引用傳遞在代碼 1 中很快,而在代碼 2 中卻很慢?

                  Can someone explain why passing by reference is fast in code1 but slow in code2?

                  對于代碼 2,主要區別在于 count($a),因此循環所用的時間幾乎相同.

                  With Code 2, the count($a) makes the main difference, so the time of the loop took is almost the same.

                  推薦答案

                  我認為如果$v 不改變[foreach($a as $v)],真正的復制不會發生,因為寫入時復制,但為什么按引用傳遞時速度很快?

                  I thought that if the $v don't change [foreach($a as $v)], the real copy will not happen because of copy on write, but why it is fast when pass by reference?

                  影響的不是$v,而是$a,這個龐大的數組.您可以將它作為值或作為函數的引用傳遞.在函數內部,它是值 (test1) 或引用 (test2).

                  The impact is not on $v but on $a, the huge array. You either pass it as value or as reference to the function. Inside the function it's then value (test1) or reference (test2).

                  您有兩個代碼(代碼 1 和代碼 2).

                  You have two codes (code 1 and code 2).

                  代碼 1: 正在使用 foreach.使用 foreach 你有兩個選擇:迭代一個值或一個引用(示例).當您迭代一個值時,迭代是在該值的副本上完成的.如果您對引用進行迭代,則不會進行復制.

                  Code 1: Is using foreach. With foreach you've got two options: iterate over a value or a reference (Example). When you iterate over a value, the iteration is done on a copy of the value. If you iterate over a reference, no copy is done.

                  當您在 test2 中使用參考時,它會更快.不需要復制這些值.但是在 test1 中,您將數組作為值傳遞,該數組會被復制.

                  As you use the reference in test2, it's faster. The values do not need to be copied. But in test1, you pass the array as value, the array gets copied.

                  代碼 2: 正在使用 for.因為在這里實際上什么都不做.在這兩種情況下.您訪問變量并從數組中讀取值.無論是引用還是副本,這幾乎都是一樣的(感謝 PHP 中的寫入時復制優化).

                  Code 2: Is using for. For does nothing actually here. In both cases. You access the variable and read value from the array. That's pretty much the same regardless if it's a reference or a copy (thanks to the copy on write optimization in PHP).

                  您現在可能想知道,為什么代碼 2 中存在 差異.差異不是因為 for,而是因為 count.如果你傳遞一個對 count 的引用,PHP 會在內部創建一個它的副本,因為它 count 需要一個副本,而不是一個引用.

                  You might now wonder, why there is a difference in code 2. The difference is not because of for but because of count. If you pass a reference to count PHP internally creates a copy of it because it count needs a copy, not a reference.

                  另請閱讀:請勿使用PHP 參考作者:Johannes Schlüter

                  Read as well: Do not use PHP references by Johannes Schlüter

                  我也編譯了一組測試.但我更具體地將代碼放入測試函數中.

                  I've compiled a set of tests as well. But I more specifically put code into the test functions.

                  • 空白 - 調用函數有什么區別?
                  • 計數 - count 有區別嗎?
                  • For - foronly (not count) 會發生什么?
                  • Foreach - 只是 foreach - 甚至打破第一個元素.
                  • Blank - What's the difference in calling the function?
                  • Count - Does count make a difference?
                  • For - What happens with foronly (not count)?
                  • Foreach - Just foreach - even breaking on first element.

                  每個測試都有兩個版本,一個稱為_copy(將數組作為副本傳遞到函數中)和一個稱為_ref(將數組作為引用傳遞).

                  Every test is in two versions, one called _copy (passing the array as copy into the function) and one called _ref (passing the array as reference).

                  這些微基準并不總是告訴你真相,但如果你能夠隔離特定點,你可以很好地進行有根據的猜測,例如不是for而是count 有影響:

                  It's not always that these micro-benchmarks tell you the truth, but if you're able to isolate specific points, you can quite well do an educated guess, for example that not for but count had the impact:

                  function blank_copy($a){
                  }
                  function blank_ref(&$a){
                  }
                  function foreach_copy($a){
                      foreach($a as $v) break;
                  }
                  function foreach_ref(&$a){
                      foreach($a as $v) break;
                  }
                  function count_copy($a){
                    $cnt = count($a);
                  }
                  function count_ref(&$a){
                    $cnt = count($a);
                  }
                  function for_copy($a){
                      for($i=0;$i<100000;$i++)
                          $a[$i];
                  }
                  function for_ref(&$a){
                      for($i=0;$i<100000;$i++)
                          $a[$i];
                  }
                  
                  $tests = array('blank_copy', 'blank_ref', 'foreach_copy', 'foreach_ref', 'count_copy', 'count_ref', 'for_copy', 'for_ref');
                  
                  
                  $x = array_fill(0, 100000, 'xxxxx');
                  $count = count($x);
                  $runs = 10;
                  
                  ob_start();
                  
                  for($i=0;$i<10;$i++)
                  {
                      shuffle($tests);
                      foreach($tests as $test)
                      {
                          $begin = microtime(true);
                          for($r=0;$r<$runs;$r++)
                              $test($x);
                          $end = microtime(true);
                          $result = $end - $begin;
                          printf("* %'.-16s: %f
                  ", $test, $result);
                      }
                  }
                  
                  $buffer = explode("
                  ", ob_get_clean());
                  sort($buffer);
                  echo implode("
                  ", $buffer);
                  

                  輸出:

                  * blank_copy......: 0.000011
                  * blank_copy......: 0.000011
                  * blank_copy......: 0.000012
                  * blank_copy......: 0.000012
                  * blank_copy......: 0.000012
                  * blank_copy......: 0.000015
                  * blank_copy......: 0.000015
                  * blank_copy......: 0.000015
                  * blank_copy......: 0.000015
                  * blank_copy......: 0.000020
                  * blank_ref.......: 0.000012
                  * blank_ref.......: 0.000012
                  * blank_ref.......: 0.000014
                  * blank_ref.......: 0.000014
                  * blank_ref.......: 0.000014
                  * blank_ref.......: 0.000014
                  * blank_ref.......: 0.000015
                  * blank_ref.......: 0.000015
                  * blank_ref.......: 0.000015
                  * blank_ref.......: 0.000015
                  * count_copy......: 0.000020
                  * count_copy......: 0.000022
                  * count_copy......: 0.000022
                  * count_copy......: 0.000023
                  * count_copy......: 0.000024
                  * count_copy......: 0.000025
                  * count_copy......: 0.000025
                  * count_copy......: 0.000025
                  * count_copy......: 0.000026
                  * count_copy......: 0.000031
                  * count_ref.......: 0.113634
                  * count_ref.......: 0.114165
                  * count_ref.......: 0.114390
                  * count_ref.......: 0.114878
                  * count_ref.......: 0.114923
                  * count_ref.......: 0.115106
                  * count_ref.......: 0.116698
                  * count_ref.......: 0.118077
                  * count_ref.......: 0.118197
                  * count_ref.......: 0.123201
                  * for_copy........: 0.190837
                  * for_copy........: 0.191883
                  * for_copy........: 0.193080
                  * for_copy........: 0.194947
                  * for_copy........: 0.195045
                  * for_copy........: 0.195944
                  * for_copy........: 0.198314
                  * for_copy........: 0.198878
                  * for_copy........: 0.200016
                  * for_copy........: 0.227953
                  * for_ref.........: 0.191918
                  * for_ref.........: 0.194227
                  * for_ref.........: 0.195952
                  * for_ref.........: 0.196045
                  * for_ref.........: 0.197392
                  * for_ref.........: 0.197730
                  * for_ref.........: 0.201936
                  * for_ref.........: 0.207102
                  * for_ref.........: 0.208017
                  * for_ref.........: 0.217156
                  * foreach_copy....: 0.111968
                  * foreach_copy....: 0.113224
                  * foreach_copy....: 0.113574
                  * foreach_copy....: 0.113575
                  * foreach_copy....: 0.113879
                  * foreach_copy....: 0.113959
                  * foreach_copy....: 0.114194
                  * foreach_copy....: 0.114450
                  * foreach_copy....: 0.114610
                  * foreach_copy....: 0.118020
                  * foreach_ref.....: 0.000015
                  * foreach_ref.....: 0.000016
                  * foreach_ref.....: 0.000016
                  * foreach_ref.....: 0.000016
                  * foreach_ref.....: 0.000018
                  * foreach_ref.....: 0.000019
                  * foreach_ref.....: 0.000019
                  * foreach_ref.....: 0.000019
                  * foreach_ref.....: 0.000019
                  * foreach_ref.....: 0.000020
                  

                  這篇關于php foreach,為什么使用數組的引用傳遞很快?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

                    1. <i id='k2uFx'><tr id='k2uFx'><dt id='k2uFx'><q id='k2uFx'><span id='k2uFx'><b id='k2uFx'><form id='k2uFx'><ins id='k2uFx'></ins><ul id='k2uFx'></ul><sub id='k2uFx'></sub></form><legend id='k2uFx'></legend><bdo id='k2uFx'><pre id='k2uFx'><center id='k2uFx'></center></pre></bdo></b><th id='k2uFx'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='k2uFx'><tfoot id='k2uFx'></tfoot><dl id='k2uFx'><fieldset id='k2uFx'></fieldset></dl></div>
                      <tfoot id='k2uFx'></tfoot>
                      1. <legend id='k2uFx'><style id='k2uFx'><dir id='k2uFx'><q id='k2uFx'></q></dir></style></legend>

                          • <bdo id='k2uFx'></bdo><ul id='k2uFx'></ul>

                            <small id='k2uFx'></small><noframes id='k2uFx'>

                              <tbody id='k2uFx'></tbody>
                            主站蜘蛛池模板: 色婷婷亚洲国产女人的天堂 | 欧美综合一区 | 成人欧美一区二区三区黑人孕妇 | 色精品 | 欧美日韩综合一区 | 亚洲一区二区中文字幕 | 不卡av在线| 99久久精品国产一区二区三区 | 国产精品福利在线观看 | 精品视频在线观看 | av在线免费播放 | 欧美中文字幕一区二区三区亚洲 | 国产精品1区2区3区 国产在线观看一区 | 精品久久不卡 | 日韩欧美亚洲 | 一级黄a视频 | 激情久久网 | 国产精品一区二区视频 | 男女精品久久 | 精品欧美一区二区在线观看 | 成人欧美一区二区三区视频xxx | 国产成人啪免费观看软件 | 久久亚| 懂色av蜜桃av | 性一交一乱一透一a级 | 特级a欧美做爰片毛片 | 欧美在线日韩 | 青青草国产在线观看 | 亚洲一区二区三区视频 | 在线资源视频 | 国产精品激情小视频 | 欧美一级久久 | 国产欧美日韩一区二区三区 | 91久久精品国产 | 91精品久久久久久久久 | 国产激情小视频 | 国产精品久久久久久久久图文区 | 欧美一级特黄aaa大片在线观看 | 天天操天天舔 | 久在线 | 91精品国产综合久久久久蜜臀 |