PHP Array Performance

Source: Internet
Author: User
I recently read an article about the implementation principles of PHP arrays. Because it is implemented in the form of a hashTable linked list, I want to use the code to check the impact on performance and test the two or three groups of data, which is not satisfactory, I don't need to post the analysis. I need to post the code first, and then I need to modify the test code to draw a graph to analyze the purpose of the test. An example is to illustrate the PHP number through

I recently read an article about the implementation principles of PHP arrays. Because it is implemented in the form of a hashTable linked list, I want to use the code to check the impact on performance and test the two or three groups of data, which is not satisfactory, I don't need to post the analysis. I need to post the code first, and then I need to modify the test code to draw a graph to analyze the purpose of the test. An example is to illustrate the PHP number through

I recently read an article about PHP array-related implementation principles. Because it is implemented in the form of hashTable + linked list, I would like to use the code to look at the impact on performance,

I tested 2 or 3 groups of data. If I am not satisfied, I will not post it for analysis. I will post the code first, and then I will need to modify the test code to draw a graph for analysis.

The purpose of the test is to use values to demonstrate that PHP arrays are randomly accessed through linked list traversal. The performance is similar to that of foreach (the fact shows that the former is much faster, the second example shows that foreach is linked list traversal, and for is random access traversal. The former has higher performance than the latter, and the test result is consistent with the guess.

 
Function getValueTest () {// generate the test sample array require_once '.. /.. /include/Timer. cls. php '; $ arr = array (); $ n = 10000; for ($ I = 1; $ I <= $ n; $ I ++) {$ value = 'A '. $ I; $ arr [$ value] = $ value;} $ Timer = new Timer (); $ tmp = ''; // compare the performance differences of values // use the index value $ Timer-> start (); for ($ I = $ n; $ I> 0; $ I --) {$ value = 'A '. $ I; $ tmp = $ arr [$ value] ;}$ Timer-> stop (); $ time1 = $ Timer-> getTime (); // compare values by traversing $ Timer-> start (); for ($ I = $ n; $ I> 0; $ I --) {$ value = 'A '. $ I; foreach ($ arr as $ key =>&$ val) {if ($ val ==$ value) {$ tmp = $ val; break ;}}} $ Timer-> stop (); $ time2 = $ Timer-> getTime (); unset ($ val); echo "time1 $ time1 time2 $ time2";} function compareWhile () {// generate the test sample array require_once '.. /.. /include/Timer. cls. php '; $ arr = array (); $ n = 1000000; for ($ I = $ n; $ I> 0; $ I --) {$ arr [$ I] = $ I;} $ tmp = ''; // traversal performance comparison $ Timer = new Timer (); $ Timer-> start (); for ($ I = 1; $ I <= $ n; $ I ++) {$ tmp = $ arr [$ I] ;}$ Timer-> stop (); $ time1 = $ Timer-> getTime (); $ Timer-> start (); foreach ($ arr as $ key =>&$ val) {$ tmp = $ val ;} $ Timer-> stop (); $ time2 = $ Timer-> getTime (); unset ($ val); echo "time1 $ time1 time2 $ time2 ";}

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.