Get the App
SLTechnology News&Howtos  ›  Development  › 

Comparative Test of PHP Array traversal

Shulou Source: shulou.com Published: 2022-06-03 11:18:26 10月03日 Update

This article introduces the knowledge of "comparative testing of PHP array traversal". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Test 1: under PHP4.4.1, use one-dimensional array

Numbered statement time 1for ($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) 7foreach ($arr as $val) 0.0649691 (s)

You can see that 7 is the fastest, but this method does not return subscripts, and this method does not meet the requirements if you need to use subscripts. The second is 2. Do you see any difference between this way of writing and 1? 2 calculates the length of the array before the loop, while 1 calculates the length of the array for each loop, so 2 is more efficient than 1. However, 2 and 1 are the same, and you can only manipulate arrays where the subscript is numeric and the numbers are consecutive. The third is 6, which is considered the fastest method in general discussion. If you manipulate the subscript of the array and the subscript is marked with discontinuous numbers, then there is no doubt that you should choose this method. As for the slow ones, forget these uses: roll:

Test 2: using a two-digit array under PHP4.4.1

Numbered statement time 1for ($I = 0; $I

< $num = count($arr); $i++)0.0824819(s)2for($i = 0, $num = count($arr); $i < $num; $i++)0.0523129(s)3while(list($key, $val) = each($arr))0.1273971(s)4while(list(, $val) = each($arr))0.0961161(s)5while(list($key, ) = each($arr))0.1245570(s)6foreach($arr as $key =>

$val) 0.0764248 (s) 7foreach ($arr as $val) 1.1415598 (s)

As a result, major changes have taken place

The fastest is 2, because for does not care about multi-dimensional problems, so the speed does not change much compared with one-dimensional array (as for why it is faster than one-dimensional, I can only say that it is affected by the operation in the file. This kind of influence does exist. When I test with PEAR's Benchmark class, I find that the time of one test, 100th test, and 1000 test is very different.

The second is 6, as mentioned in Test 1, if 2 cannot be used (that is, the subscript is not contiguous), 6 is undoubtedly the choice of *, and the eye-breaking is 7, which is actually the slowest and 10 times slower than the second slowest. This problem is really puzzling. Later, I did a more detailed test and found that the method of 7 is mainly affected by the number of elements. if there are 10 elements in one dimension, then if there are eight elements in the second dimension, the speed of 7 is almost the same as that of 6. if there are 30 elements in one dimension, then the speed of 7 and 6 is basically the same when there are 6 elements in two dimensions. But if there are 1000 elements in one dimension and more than three elements in two dimensions, then 7 will be much slower than 6.

That's all for the comparison Test of PHP Array traversal. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Array test element one-dimensional subscript method fastest number time speed two-dimensional influence that is content more no doubt question knowledge statement length Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Huawei MySQL NVidia