PHP array traversal comparison test_php tutorial

Source: Internet
Author: User
Compare and test PHP array traversal. PHP is still quite common, so I studied PHP array traversal and shared it with you here. I hope it will be useful to you. As for PHP array traversal, many people have already done PHP in common use, so I have studied PHP array traversal and I will share it with you here. I hope it will be useful to you. Many people have tested PHP array traversal, such ~ Isn't it: cool :.

First, PHP array traversal methods such as for, while, and foreach are no longer mentioned. if you do not know, you can go to the official PHP Manual, however, the syntax for, while, and foreach traversal arrays is also different, so the effect is better ~ The program is changed from the program written by Lilov in the link above. you can download the test source file and change it to array_check.php. then you can run it.

Test 1: Use a one-dimensional array in PHP4.4.1

No. Statement Time
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)

It can be seen that 7 is the fastest, but this method does not return subscript. if you need to use subscript, this method does not meet the requirements. The second is 2. do you see the difference between this writing method and 1? 2. calculate the length of the array before the loop, and 1 calculate the length of the array for each loop, so 2 is more efficient than 1. However, both 2 and 1 are the same. you can only operate the array where the subscript is a number and the number is continuous. The third method is 6, which is the fastest method in general discussion. if you operate the subscript of an array with a non-continuous number, you should select this method without any doubt, as for the slow ones, I will not say much. forget these usage: roll:

Test 2: use a two-digit array in PHP4.4.1


Bytes. In fact, many people have done PHP array traversal...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.