Home  >  Article  >  Backend Development  >  Introduction to PHP code optimization methods (graphics and text)

Introduction to PHP code optimization methods (graphics and text)

不言
不言forward
2019-04-01 09:25:153392browse

This article brings you an introduction to PHP code optimization methods (pictures and texts). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

There are usually several ways to insert data into arrays in PHP:

Assign directly when defining

$arr = array(‘apple’, ’banana’);

Use array variable operation

$arr = array();
$arr[] = ‘apple’;
$arr[] = ‘banana’;

Use array_push() Insert

$arr = array(‘apple’);
array_push($arr, ‘banana’, ‘orange’);

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

Regarding the use of array_push, I will not introduce it in detail. Please read the official documentation yourself (https://secure.php.net/manual/en/function.array-push.php).

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:

Introduction to PHP code optimization methods (graphics and text)

Introduction to PHP code optimization methods (graphics and text)

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

Introduction to PHP code optimization methods (graphics and text)

Introduction to PHP code optimization methods (graphics and text)

By running the above code multiple times, you can find that the array is used directly Variable assignment takes about 0.0045, while using array_push takes about 0.006. The memory consumed is the same, because both have 100,000 records.

Comparing the two methods, we found that the second method 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 think the second method is better than the third method. How much faster this method is 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, from the perspective of code size, the two are the same. From the perspective of the array structure, during the test We don't use indexes, and they all start from 0 by default. If you want to use key => value to insert data, array_push() is not easy to handle. In this case, it is very convenient to use method 2: $array1[$key ] => $x.

We all talked about how 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.

[Recommended course:

PHP video tutorial]

The above is the detailed content of Introduction to PHP code optimization methods (graphics and text). 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