Array_push Efficiency Test _php Tutorial

Source: Internet
Author: User
Use array[when assigning values to an array in PHP] This method is more efficient than Array_push (), and the following tests are available for various reasons
Test code:
1 2/**
3 * Array Performance test
4 *for Cycle has a performance impact, so it's useless.
5 * Test is to change the $count parameter (increment from 10~100000) and call method to refresh manually, recording time is average approximate time
6 **/
7
8 $count = 10;
9
Ten arrayTest01 ($count);
11
function arrayTest01 ($count) {
$arr = Array ();
$time = Microtime (true);
for ($i = 0; $i < $count; $i + +) {
$array [] = $i;
17}
Echo (Microtime (True)-$time);
19}
20
function arrayTest02 ($count) {
$arr = Array ();
$time = Microtime (true);
($i = 0; $i < $count; $i + +) {
Array_push ($arr, $i);
26}
Echo (Microtime (True)-$time);
28}
29
?>

Efficiency is probably array[] nearly a times, 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 the official website.
Note: If you use Array_push () to add a unit to an array, you might as well use $array [] =, because there is no additional burden of calling the function.
Website Link:
Http://cn.php.net/array_push


From the quiet to repair the heart

http://www.bkjia.com/PHPjc/478305.html www.bkjia.com true http://www.bkjia.com/PHPjc/478305.html techarticle in PHP when the array is assigned to use array[] This method will be more efficient than Array_push (), and for various reasons have the following test code: 1? PHP 2/** 3 * Array performance test ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.