pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

          <bdo id='GtBCr'></bdo><ul id='GtBCr'></ul>
      1. <tfoot id='GtBCr'></tfoot>
      2. 在 foreach 循環參數中分解數組

        Exploding an array within a foreach loop parameter(在 foreach 循環參數中分解數組)

      3. <i id='OCjzk'><tr id='OCjzk'><dt id='OCjzk'><q id='OCjzk'><span id='OCjzk'><b id='OCjzk'><form id='OCjzk'><ins id='OCjzk'></ins><ul id='OCjzk'></ul><sub id='OCjzk'></sub></form><legend id='OCjzk'></legend><bdo id='OCjzk'><pre id='OCjzk'><center id='OCjzk'></center></pre></bdo></b><th id='OCjzk'></th></span></q></dt></tr></i><div class="ai5m5t0" id='OCjzk'><tfoot id='OCjzk'></tfoot><dl id='OCjzk'><fieldset id='OCjzk'></fieldset></dl></div>
      4. <small id='OCjzk'></small><noframes id='OCjzk'>

            <bdo id='OCjzk'></bdo><ul id='OCjzk'></ul>

            <legend id='OCjzk'><style id='OCjzk'><dir id='OCjzk'><q id='OCjzk'></q></dir></style></legend>
                <tbody id='OCjzk'></tbody>

                  <tfoot id='OCjzk'></tfoot>
                1. 本文介紹了在 foreach 循環參數中分解數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  foreach(explode(',' $foo) as $bar) { ... }
                  

                  對比

                  $test = explode(',' $foo);
                  foreach($test as $bar) { ... }
                  

                  在第一個例子中,它是在每次迭代時explode$foo 字符串還是PHP 將它保存在內存中并在它自己的臨時變量中爆炸?從效率的角度來看,創建額外的變量 $test 是否有意義,或者兩者幾乎相等?

                  In the first example, does it explode the $foo string for each iteration or does PHP keep it in memory exploded in its own temporary variable? From an efficiency point of view, does it make sense to create the extra variable $test or are both pretty much equal?

                  推薦答案

                  我可以做出有根據的猜測,但讓我們嘗試一下

                  I could make an educated guess, but let's try it out!

                  我認為有三種主要方法可以解決這個問題.

                  I figured there were three main ways to approach this.

                  1. 在進入循環之前爆炸并賦值
                  2. 在循環中爆炸,沒有賦值
                  3. 字符串標記化

                  我的假設:

                  1. 可能由于分配而消耗更多內存
                  2. 可能與 #1 或 #3 相同,不確定哪個
                  3. 可能更快,內存占用也小得多

                  方法

                  這是我的測試腳本:

                  Approach

                  Here's my test script:

                  <?php
                  
                  ini_set('memory_limit', '1024M');
                  
                  $listStr = 'text';
                  $listStr .= str_repeat(',text', 9999999);
                  
                  $timeStart = microtime(true);
                  
                  /*****
                   * {INSERT LOOP HERE}
                   */
                  
                  $timeEnd = microtime(true);
                  $timeElapsed = $timeEnd - $timeStart;
                  
                  printf("Memory used: %s kB
                  ", memory_get_peak_usage()/1024);
                  printf("Total time: %s s
                  ", $timeElapsed);
                  

                  這里是三個版本:

                  1)

                  // explode separately 
                  $arr = explode(',', $listStr);
                  foreach ($arr as $val) {}
                  

                  2)

                  // explode inline-ly 
                  foreach (explode(',', $listStr) as $val) {}
                  

                  3)

                  // tokenize
                  $tok = strtok($listStr, ',');
                  while ($tok = strtok(',')) {}
                  

                  結果

                  看起來有些假設被推翻了.你不愛科學嗎?:-)

                  Looks like some assumptions were disproven. Don't you love science? :-)

                  • 總的來說,這些方法中的任何一種對于合理大小"(幾百或幾千)的列表來說都足夠快.
                  • 如果您要迭代巨大的內容,時間差異相對較小,但內存使用量可能會相差一個數量級!
                  • 當你 explode() 在沒有預賦值的情況下內聯時,由于某種原因它會慢一點.
                  • 令人驚訝的是,標記化比顯式迭代聲明的數組.在如此小的規模上工作,我相信這是由于每次迭代對 strtok() 進行函數調用的調用堆棧開銷.更多內容請參見下文.
                  • In the big picture, any of these methods is sufficiently fast for a list of "reasonable size" (few hundred or few thousand).
                  • If you're iterating over something huge, time difference is relatively minor but memory usage could be different by an order of magnitude!
                  • When you explode() inline without pre-assignment, it's a fair bit slower for some reason.
                  • Surprisingly, tokenizing is a bit slower than explicitly iterating a declared array. Working on such a small scale, I believe that's due to the call stack overhead of making a function call to strtok() every iteration. More on this below.

                  就函數調用的數量而言,explode()ing 確實是標記化的佼佼者.O(1) vs O(n)

                  In terms of number of function calls, explode()ing really tops tokenizing. O(1) vs O(n)

                  我在運行方法 1) 的圖表中添加了一個獎勵,并在循環中調用了一個函數.我使用了 strlen($val),認為這將是一個相對相似的執行時間.這是有爭議的,但我只是想提出一個一般性的觀點.(我只運行了 strlen($val) 并忽略了它的輸出.我沒有沒有將它分配給任何東西,因為分配會增加額外的時間成本.)>

                  I added a bonus to the chart where I run method 1) with a function call in the loop. I used strlen($val), thinking it would be a relatively similar execution time. That's subject to debate, but I was only trying to make a general point. (I only ran strlen($val) and ignored its output. I did not assign it to anything, for an assignment would be an additional time-cost.)

                  // explode separately 
                  $arr = explode(',', $listStr);
                  foreach ($arr as $val) {strlen($val);}
                  

                  正如您從結果表中看到的那樣,它成為三種方法中最慢的方法.

                  As you can see from the results table, it then becomes the slowest method of the three.

                  這很有趣,但我的建議是做任何你認為最易讀/最可維護的事情.只有當您真正處理非常大的數據集時,您才應該擔心這些微優化.

                  This is interesting to know, but my suggestion is to do whatever you feel is most readable/maintainable. Only if you're really dealing with a significantly large dataset should you be worried about these micro-optimizations.

                  這篇關于在 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的訪問被拒絕)

                2. <i id='OtVNI'><tr id='OtVNI'><dt id='OtVNI'><q id='OtVNI'><span id='OtVNI'><b id='OtVNI'><form id='OtVNI'><ins id='OtVNI'></ins><ul id='OtVNI'></ul><sub id='OtVNI'></sub></form><legend id='OtVNI'></legend><bdo id='OtVNI'><pre id='OtVNI'><center id='OtVNI'></center></pre></bdo></b><th id='OtVNI'></th></span></q></dt></tr></i><div class="q8qtmon" id='OtVNI'><tfoot id='OtVNI'></tfoot><dl id='OtVNI'><fieldset id='OtVNI'></fieldset></dl></div>

                    <tfoot id='OtVNI'></tfoot>

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

                      <tbody id='OtVNI'></tbody>
                      • <bdo id='OtVNI'></bdo><ul id='OtVNI'></ul>
                      • <legend id='OtVNI'><style id='OtVNI'><dir id='OtVNI'><q id='OtVNI'></q></dir></style></legend>
                            主站蜘蛛池模板: 福建省教师资格证-福建教师资格证考试网 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 华溶溶出仪-Memmert稳定箱-上海协烁仪器科技有限公司 | 柴油机_柴油发电机_厂家_品牌-江苏卡得城仕发动机有限公司 | 螺旋压榨机-刮泥机-潜水搅拌机-电动泥斗-潜水推流器-南京格林兰环保设备有限公司 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 | 烟气换热器_GGH烟气换热器_空气预热器_高温气气换热器-青岛康景辉 | 百方网-百方电气网,电工电气行业专业的B2B电子商务平台 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 广东成考网-广东成人高考网| 硫化罐_蒸汽硫化罐_大型硫化罐-山东鑫泰鑫智能装备有限公司 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 5nd音乐网|最新流行歌曲|MP3歌曲免费下载|好听的歌|音乐下载 免费听mp3音乐 | 电抗器-能曼电气-电抗器专业制造商| 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 电子厂招聘_工厂招聘_普工招聘_小时工招聘信息平台-众立方招工网 | 紫外可见光分光度计-紫外分光度计-分光光度仪-屹谱仪器制造(上海)有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易| 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 云南成人高考_云南成考网| 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | 模型公司_模型制作_沙盘模型报价-中国模型网 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 气胀轴|气涨轴|安全夹头|安全卡盘|伺服纠偏系统厂家-天机传动 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 睿婕轻钢别墅_钢结构别墅_厂家设计施工报价 | 汕头市盛大文化传播有限公司,www.11400.cc | 最新范文网_实用的精品范文美文网 | 镀锌钢格栅_热镀锌格栅板_钢格栅板_热镀锌钢格板-安平县昊泽丝网制品有限公司 | 志高装潢官网-苏州老房旧房装修改造-二手房装修翻新 |