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 ";}