Performance comparison test between array_push and $arr []=$value in PHP

In the Gaomeng blog, I saw "the performance comparison test between array_push and $arr []=$value in php". This problem is really not noticed during normal use. After seeing this article again, I also tried it. The results are as follows.

Compare array_push and $arr []=$value performance

1. Use array_push to push in 1000000 elements.

  1. $arr  =  array ();   
  2. $starttime  = get_microtime();   
  3. for  ( $i =0;   $i  < 1000000;   $i ++) {   
  4.      array_push ( $arr  ,  $i );   
  5. }  
  6. $endtime  = get_microtime();   
  7. printf( "Time% f ms"  , ( $endtime - $starttime )*1000); //Time 3559.043169 ms   
  8. function  get_microtime(){  
  9.      //list($msec,$sec) = explode(' ' , microtime());   
  10.      //return (float)$msec+(float)$sec;   
  11.      return  microtime(true);   
  12. }  

Time 3559.043169 ms

 

2. Press 100000 elements with $arr []=$value.

  1. $arr  =  array ();   
  2. $starttime  = get_microtime();   
  3. for  ( $i =0;   $i  < 1000000;   $i ++) {   
  4.      $arr [] =  $i ;   
  5. }  
  6. $endtime  = get_microtime();   
  7. printf( "Time% f ms"  , ( $endtime - $starttime )*1000); //Time 371.806145 ms   
  8. function  get_microtime(){  
  9.      //list($msec,$sec) = explode(' ' , microtime());   
  10.      //return (float)$msec+(float)$sec;   
  11.      return  microtime(true);   
  12. }  

Time 371.806145 ms

result:

The execution time of array_push is 3559.043169 milliseconds, while the execution time of $arr []=$value is 371.806145 milliseconds. It can be seen that the execution time of $arr []=$value is far less than that of array_push when only one element is added.

3. Press multiple elements to compare at the same time.

Use the array_push method to press 100000 elements, 50 elements at a time

  1. $arr  =  array ();   
  2. $starttime  = get_microtime();   
  3. for  ( $i =0;   $i  < 1000000;   $i  =  $i +50) {   
  4.       array_push ( $arr $i , $i +1, $i +2, $i +3, $i +4, $i +5, $i +6, $i +7, $i +8, $i +9, $i +10,  
  5.          $i +11, $i +12, $i +13, $i +14, $i +15, $i +16, $i +17, $i +18, $i +19,  
  6.          $i +21, $i +22, $i +23, $i +24, $i +25, $i +26, $i +27, $i +28, $i +29,  
  7.          $i +31, $i +32, $i +33, $i +34, $i +35, $i +36, $i +37, $i +38, $i +39,  
  8.          $i +41, $i +42, $i +43, $i +44, $i +45, $i +46, $i +47, $i +48, $i +49);   
  9. }  
  10. $endtime  = get_microtime();   
  11. printf( "Time% f ms"  , ( $endtime - $starttime )*1000); //Time 237.401962ms   
  12.   
  13. function  get_microtime(){  
  14.      //list($msec,$sec) = explode(' ' , microtime());   
  15.      //return (float)$msec+(float)$sec;   
  16.      return  microtime(true);   
  17. }  

result:

The execution time of array_push: 237.401962 milliseconds, while $arr []=$value Just tested the execution time: 371.806145 milliseconds. This shows that the execution speed of array_push is fast.

To sum up, $arr []=$value is used when only one element is pushed in, and array_push is recommended when multiple elements can be pushed in at the same time.

(Note: If you are pushing an element, it is efficient to use $arr []=$value, because it can save the extra burden of calling the function. If you are pushing multiple elements at the same time, it is efficient to use array_push, because you do not need to repeatedly obtain the pointer at the end of the file.)

 Watson Blog
  • This article is written by Published on November 15, 2017 11:21:28
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/828.html

Comment