array_push efficiency test_PHP tutorial

WBOY
Release: 2016-07-13 17:49:57
Original
1254 people have browsed it

Using array[] when assigning values ​​to arrays in PHP is more efficient than array_push(), so for various reasons the following test is done
Test code:
1 2 /**
3 *Array performance test
4 *The for loop has performance impact so it is useless
5 *The test is to change the $count parameter (increment from 10 to 100000) and call the method to refresh manually. The recording time is the average approximate time
6 **/
7
8 $count = 10;

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 about twice as fast as array[]. The test environment is ubuntu 11 and windows 7

windows php-5.2.17/Apache2.2
times($count) 10 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
0.056978 Error

This is what the official website says
Note: If you use array_push() to add an element to an array, it is better to use $array[] = because there is no additional burden of calling the function.
Official website link:
http://cn.php.net/array_push


Excerpted from Calm to cultivate the mind

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478305.htmlTechArticleUsing array[] when assigning values ​​to arrays in PHP is more efficient than array_push(), so For various reasons, the following test code is produced: 1?php 2 /** 3 *Array performance test...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!