Home  >  Article  >  Backend Development  >  PHP code optimization array_push

PHP code optimization array_push

little bottle
little bottleforward
2019-04-17 10:19:392392browse

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

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

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’);
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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete