好奇,用用array_shift array_pop 來操做數組php
測試,執行代碼10次,for循環執行程序10000次,查看用時,證實 for 比 shift 好太多數組
Shift性能
|
For測試 |
Popcode |
1466612647974----1466612648987 1013it |
1466612859942----1466612860803 861io |
1466612973278----1466612974312 1034for循環 |
1466612661110----1466612662153 1043table |
1466612870539----1466612871387 848class |
1466612990035----1466612991084 1049 |
1466612672061----1466612673079 1018 |
1466612878459----1466612879562 1103 |
1466612999726----1466613000785 1059 |
1466612680562----1466612681612 1050 |
1466612886645----1466612887561 916 |
1466613009125----1466613010195 1070 |
1466612689297----1466612690332 1035 |
1466612893969----1466612894878 909 |
1466613018370----1466613019635 1265 |
1466612700237----1466612701437 1200 |
1466612903951----1466612904835 884 |
1466613025871----1466613026886 1015 |
1466612709077----1466612710098 1021 |
1466612912643----1466612913561 918 |
1466613033574----1466613034577 1003 |
1466612716794----1466612717832 1038 |
1466612921161----1466612922048 887 |
1466613042230----1466613043256 1026 |
1466612724867----1466612725907 1040 |
1466612928817----1466612929679 862 |
1466613050434----1466613051708 1274 |
1466612732746----1466612733797 1051 |
1466612936061----1466612936932 871 |
1466613059171----1466613060224 1053 |
|
|
|
測試代碼
.........
# array_shift list($t1, $t2) = explode(' ', microtime()); echo $startTime = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000),'----'; for($j=0; $j<10000; $j++) { foreach ($results as $k => $v) { $productNameList = explode(',', $v['product_names']); $productImageLocationList = explode(',', $v['product_image_location']); $productIdList = explode(',', $v['product_id']); $productSlugList = explode(',', $v['product_slug']); $productUnitPrice = explode(',', $v['product_unit_price']); $productQuantity = explode(',', $v['product_quantity']); $productTotalPrice = explode(',', $v['product_total_price']); foreach ($productNameList as $kk=>$vv) { $results[$k]['product_list'][$kk] = array( 'product_names' => array_shift($productNameList), 'product_image_location' => array_shift($productImageLocationList), 'product_id' => array_shift($productIdList), 'product_slug' => array_shift($productSlugList), 'product_unit_price' => array_shift($productUnitPrice), 'product_quantity' => array_shift($productQuantity), 'product_total_price' => array_shift($productTotalPrice), ); } } } list($t1, $t2) = explode(' ', microtime()); echo $endTime = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000),"\n\r"; echo $endTime-$startTime; dump($results);exit;
# for list($t1, $t2) = explode(' ', microtime()); echo $startTime = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000),'----'; for($j=0; $j<10000; $j++) { foreach ($results as $k => $v) { $productNameList = explode(',', $v['product_names']); $productImageLocationList = explode(',', $v['product_image_location']); $productIdList = explode(',', $v['product_id']); $productSlugList = explode(',', $v['product_slug']); $productUnitPrice = explode(',', $v['product_unit_price']); $productQuantity = explode(',', $v['product_quantity']); $productTotalPrice = explode(',', $v['product_total_price']); for ($i = 0; $i < count($productNameList); $i++) { $results[$k]['product_list'][$i] = array( 'product_names' => $productNameList[$i], 'product_image_location' => $productImageLocationList[$i], 'product_id' => $productIdList[$i], 'product_slug' => $productSlugList[$i], 'product_unit_price' => $productUnitPrice[$i], 'product_quantity' => $productQuantity[$i], 'product_total_price' => $productTotalPrice[$i], ); } } } list($t1, $t2) = explode(' ', microtime()); echo $endTime = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000),"\n\r"; echo $endTime-$startTime; dump($results);exit;
# array_pop list($t1, $t2) = explode(' ', microtime()); echo $startTime = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000),'----'; for($j=0; $j<10000; $j++) { foreach ($results as $k => $v) { $productNameList = explode(',', $v['product_names']); $productImageLocationList = explode(',', $v['product_image_location']); $productIdList = explode(',', $v['product_id']); $productSlugList = explode(',', $v['product_slug']); $productUnitPrice = explode(',', $v['product_unit_price']); $productQuantity = explode(',', $v['product_quantity']); $productTotalPrice = explode(',', $v['product_total_price']); foreach($productNameList as $kk=>$vv) { $results[$k]['product_list'][$kk] = array( 'product_names' => array_pop($productNameList), 'product_image_location'=> array_pop($productImageLocationList), 'product_id' => array_pop($productIdList), 'product_slug' => array_pop($productSlugList), 'product_unit_price' => array_pop($productUnitPrice), 'product_quantity' => array_pop($productQuantity), 'product_total_price' => array_pop($productTotalPrice), ); } } } list($t1, $t2) = explode(' ', microtime()); echo $endTime = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000),"\n\r"; echo $endTime-$startTime; dump($results);exit;
........
shift 會重建數組,每次執行後,數組都會從0開始重建。形成慢的緣由,而 pop 從此次來看和shift同樣慢,對數組長度進行改變的操做會形成性能上的慢