Home > Backend Development > PHP Tutorial > About PHP array traversal comparison test_PHP tutorial

About PHP array traversal comparison test_PHP tutorial

WBOY
Release: 2016-07-15 13:27:19
Original
817 people have browsed it

PHP is still relatively commonly used, so I studied PHP array traversal and shared it with you here. I hope it will be useful to everyone. Regarding PHP array traversal, many people have actually done tests, such as http://www.lilov.org/post/18.html, but these discussions are actually too simple. Here I will discuss this more comprehensively. Although the question seems a bit picky, you should hold this idea in mind when making programs and constantly strive for excellence~ isn’t it :cool:.

First of all, I won’t go into details about the PHP array traversal methods for, while, and foreach. If you don’t know, you can check the PHP official manual yourself. However, the writing methods of for, while, and foreach to traverse the array are also different, so the effect Well, you will know after reading~ The program is modified from the program written by Lilov in the link above. For specific content, you can download the test source file and rename it array_check.php to run

Test 1: PHP4 .4.1, using one-dimensional array

编号 语句 时间
1 for($i = 0; $i < $num = count($arr); $i++)0.1048162(s)
2for($i = 0, $num = count($arr); $i < $num; $i++)0.0698998(s)
3while(list($key, $val) = each($arr))0.1437800(s)
4while(list(, $val) = each($arr))0.1226320(s)
5while(list($key, ) = each($arr))0.1119628(s)
6foreach($arr as $key => $val) 0.0972550(s)
7 foreach($arr as $val) 0.0649691(s)

can be seen that 7 is the fastest, but this method is not The subscript will be returned. If you need to use subscripts, this method does not meet the requirements. Next is 2. Do you see any difference between this way of writing and 1? 2 calculates the length of the array before looping, while 1 calculates the length of the array every time it loops, so 2 is more efficient than 1. However, 2 and 1 are the same, and can only operate on arrays whose subscripts are numbers and the numbers are consecutive. The third is 6, which is the fastest method considered in general discussions. If you operate the subscript of an array and the subscript contains non-consecutive numbers, then there is no doubt that you should choose this method. As for the slow one, I won’t say more. Forget these. Usage: roll:

Test 2: Under PHP4.4.1, use two-digit array


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446537.htmlTechArticlePHP is still relatively commonly used, so I studied PHP array traversal and shared it with you here. Hope it's useful to everyone. Regarding PHP array traversal, many people have actually done it...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template