When assigning values to arrays in php, the efficiency of using array [] is higher than that of array_push (). For various reasons, the following test is performed.
Test code:
1 <? Php
2 /**
3 * Array Performance Test
4 * for Loop performance is affected, so it is useless
5 * test is to change the $ count parameter (from 10 ~ 100000 incremental throughout) and call method manual refresh, record time is average approximate time
6 **/
7
8 $ count = 10;
9
10 arrayTest01 ($ count );
11
12 function arrayTest01 ($ count ){
13 $ arr = array ();
14 $ time = microtime (true );
15 for ($ I = 0; $ I <$ count; $ I ++ ){
16 $ array [] = $ I;
17}
18 echo (microtime (true)-$ time );
19}
20
21 function arrayTest02 ($ count ){
22 $ arr = array ();
23 $ time = microtime (true );
24 for ($ I = 0; $ I <$ count; $ I ++ ){
25 array_push ($ arr, $ I );
26}
27 echo (microtime (true)-$ time );
28}
29
30?>
The efficiency is almost double the speed of array []. The testing environment is ubuntu 11 and windows 7.
Windows php-5.2.17/Apache2.2
Times ($ count) Ten 100 1000
10000 100000 1000000
Array [] 2.31E-05 0.000104
0.000867 0.008417 0.043666
0.288323
Array_push 2.79E-05
0.000181 0.001614 0.014447
0.055875 0.491052
Ubuntu11.04 PHP 5.3.6/apache2.2
Array [] 1.91E-05 7.70E-05
0.000726 0.007669 0.040492 Error
Array_push 2.50E-05
1. 26E-04 0.001149 0.013714
Error 0.056978
This is the comments on the official website.
Note: If array_push () is used to add a unit to the array, it is better to use $ array [] =, because there is no additional burden to call the function.
Official Website:
Http://cn.php.net/array_push
From meditation