Comparison between array_push and $ arr [] = $ value in php

Source: Internet
Author: User

Comparison between array_push and $ arr [] = $ value in php

This article briefly analyzes the performance of the Methods array_push and $ arr [] = $ value in php that add elements to the end of the array, in this way, problems can be handled in different scenarios.

1. array_push Method

The array_push method pushes one or more elements to the end of the array. Array_push () treats array as a stack and pushes the passed variables to the end of array. The length of array increases according to the number of variables in the stack.

Int array_push (array & $ array, mixed $ var [, mixed $...])
 
2. $ arr [] = $ value; Method

Add a value to the array. The effect is the same as that of array_push.

Compare the performance of array_push and $ arr [] = $ value

Use array_push to push 1000000 Elements

<?php$starttime = get_microtime();$arr = array();for ($i = 0;$i < 1000000;$i++) {    array_push($arr, $i);}$endtime = get_microtime();printf("run time %f ms\r\n", ($endtime - $starttime) * 1000);function get_microtime() {    list($usec, $sec) = explode(' ', microtime());    return (float)$usec + (float)$sec;}

Execution time: 2735.545158 MS

Use $ arr [] = $ value to push 100000 Elements

<?php$starttime = get_microtime();$arr = array();for($i=0; $i<1000000; $i++){    $arr[] = $i;}$endtime = get_microtime();printf("run time %f ms\r\n", ($endtime-$starttime)*1000);function get_microtime(){    list($usec, $sec) = explode(' ', microtime());    return (float)$usec + (float)$sec;}

Execution time: 417.458057 MS

Result: $ arr [] = $ value is seven times faster than array_push.

Performance Comparison of pressing multiple elements at the same time

The array_push method is used to push 100000 elements and 50 elements at a time.

<?php$starttime = get_microtime();$arr = array();for ($i = 0;$i < 1000000;$i = $i + 50) {    array_push($arr, $i, $i + 1, $i + 2, $i + 3, $i + 4, $i + 5, $i + 6, $i + 7, $i + 8, $i + 9, $i + 10, $i + 11, $i + 12, $i + 13, $i + 14, $i + 15, $i + 16, $i + 17, $i + 18, $i + 19, $i + 21, $i + 22, $i + 23, $i + 24, $i + 25, $i + 26, $i + 27, $i + 28, $i + 29, $i + 31, $i + 32, $i + 33, $i + 34, $i + 35, $i + 36, $i + 37, $i + 38, $i + 39, $i + 41, $i + 42, $i + 43, $i + 44, $i + 45, $i + 46, $i + 47, $i + 48, $i + 49);}$endtime = get_microtime();printf("run time %f ms\r\n", ($endtime - $starttime) * 1000);function get_microtime() {    list($usec, $sec) = explode(' ', microtime());    return (float)$usec + (float)$sec;}

Execution time: 250.149012 MS

Result: Using array_push to press multiple elements at a time is faster than using $ arr [] = $ value for multiple times. The more elements the array_push pushes at a time, the higher the efficiency.

Summary:

If you press an element, use $ arr [] = $ value for high efficiency, because it can save the extra burden of calling a function.

If you press multiple elements at the same time, array_push is highly efficient because you do not need to obtain the pointer at the end of the file repeatedly.

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.