Home > Article > Backend Development > About PHP array traversal comparison test_PHP tutorial
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) |
2 | for($i = 0, $num = count($arr); $i < $num; $i++) | 0.0698998(s) |
3 | while(list($key, $val) = each($arr)) | 0.1437800(s) |
4 | while(list(, $val) = each($arr)) | 0.1226320(s) |
5 | while(list($key, ) = each($arr)) | 0.1119628(s) |
6 | foreach($arr as $key => $val) | 0.0972550(s) |
7 | foreach($arr as $val) | 0.0649691(s) |
Test 2: Under PHP4.4.1, use two-digit array