Home > Backend Development > PHP Tutorial > PHP code optimization array_push

PHP code optimization array_push

little bottle
Release: 2023-04-05 21:36:01
forward
2410 people have browsed it

There are usually several types of data inserted into arrays in PHP:

  1. Assign directly when defining
$arr = array(‘apple’, ’banana’);
Copy after login
  1. Use array variable operations
$arr = array();
$arr[] = ‘apple’;
$arr[] = ‘banana’;
Copy after login
  1. Use array_push() to insert
$arr = array(‘apple’);
array_push($arr, ‘banana’, ‘orange’);
Copy after login

In most cases, we first obtain data from the database and then convert it into an array form. For the first case, direct assignment when defining the array is often only suitable for situations where the amount of data is small and known, so there is no code optimization. We mainly talk about the second and third cases.

Performance comparison

We used php 7.2 for testing and did not make any configuration optimization. Test method: Define an array and insert 100,000 records using the second and third methods respectively. The code is roughly as follows:

PHP code optimization array_push

PHP code optimization array_push

# The

#convert function is used to check the memory usage. The running results are as follows:

PHP code optimization array_push

PHP code optimization array_push

By running the above code multiple times , it can be found that the direct use of array variable assignment takes about

0.0045, while the method of using array_push takes about 0.006, and the memory consumed is the same. , because they are all 100,000 records.

Comparing the two methods, we found that method two is indeed faster than calling

array_push, because the function call takes some time in each loop, but just looking at the above test, I don’t feel that the second method is faster than calling array_push

. How much faster the second method is than the third method is completely negligible.

Indeed, this kind of optimization is no different from no optimization in most applications, but when the application reaches the bottleneck, every inconspicuous optimization may improve the performance a lot, and our The structure of the array in the test is very simple. In real projects, the data type is not so single. It may be a nested array, and the size of the array may be more than 100,000.

Let’s take a look at the difference between the two. array_push() is a function call and the other is not. That is to say, the two are the same in terms of code size. From the array From a structural point of view, we did not use indexes in the test. The default is to start from 0. If you want to use the key => value form to insert data, array_push() is not very convenient. It’s easy to handle. It’s very convenient to use method 2 at this time: $array1[$key] => $x

.

It was said before that array_push() is not good, but sometimes array_push

is still very useful. For example, when we want to insert multiple elements at one time, we can use it like this:

$arr = array(‘a’, ’b’);
array_push($arr, ‘c’, ‘d’);
// $arr = array(‘a’, ’b’, ‘c’, ‘d’);
Copy after login
As can be seen from the above comparison, there are many methods of array interpolation in PHP. Although each method is similar, it is important to understand when each method It is still necessary to use it.

Related courses: PHP video tutorial

###

The above is the detailed content of PHP code optimization array_push. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template