Guest User

Untitled

a guest
Aug 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. PHP foreach performance when adding string to variable
  2. foreach($data as $r=>$d)
  3. {
  4. $return = $return. "<tr>
  5. <td>
  6. ".$d["client_id"]."
  7. </td>
  8. ......
  9. <td>
  10. ".$d["date_stamp"]."
  11.  
  12. </td>
  13. </tr>";
  14. }
  15. }
  16.  
  17. foreach($data as $r=>$d)
  18. {
  19. $now= "<tr>
  20. <td>
  21. ".$d["client_id"]."
  22. </td>
  23. ......
  24. <td>
  25. ".$d["date_stamp"]."
  26.  
  27. </td>
  28. </tr>";
  29. $return = $return.$now;
  30. }
  31.  
  32. //////////function to get time
  33. function parsemicrotime(){
  34. list($usec, $sec) = explode(" ",microtime());
  35. return ((float)$usec + (float)$sec);
  36. }
  37.  
  38. ////////////define test array
  39. $a = array();
  40. for($i = 0; $i < 5000; $i ++)//generate 5k rows
  41. {
  42. for($k=0; $k<5;$k++)//lets have just 6 columns
  43. {
  44. $a[$i]["column_".$k] = 'test string '.$i.' / '.$k.' - note that the size of the $output string makes a huge difference ';
  45. }
  46. }
  47.  
  48. ///////////////first test
  49. $time_start = parsemicrotime();
  50. $output = '';
  51. foreach($a as $row=>$columns)
  52. {
  53. $output = $output ."
  54. <tr>
  55. <td>".$columns["test_0"]. "</td>
  56. <td>" .$columns["test_1"]. "</td>
  57. <td>" .$columns["test_2"]. "</td>
  58. <td>" .$columns["test_3"]. "</td>
  59. <td>" .$columns["test_4"]. "</td>
  60. <td>" .$columns["test_5"]. "</td>
  61. </tr>";
  62. }
  63. $approach_1_result = parsemicrotime()-$time_start;
  64.  
  65.  
  66. /////////////second test
  67. $time_start2 = parsemicrotime();
  68. $output2 = '';
  69. foreach($a as $row2=>$columns2)
  70. {
  71. $now2= "
  72. <tr>
  73. <td>".$columns["test_0"]. "</td>
  74. <td>" .$columns["test_1"]. "</td>
  75. <td>" .$columns["test_2"]. "</td>
  76. <td>" .$columns["test_3"]. "</td>
  77. <td>" .$columns["test_4"]. "</td>
  78. <td>" .$columns["test_5"]. "</td>
  79. </tr>";
  80. $output2 = $output2 .$now2;
  81. }
  82. $approach_2_result = parsemicrotime()-$time_start2;
  83.  
  84.  
  85. /////////////third test
  86. $time_start3 = parsemicrotime();
  87. ob_start();
  88. $output3 = '';
  89. foreach($a as $row3=>$columns3)
  90. {
  91. echo "
  92. <tr>
  93. <td>".$columns["test_0"]. "</td>
  94. <td>" .$columns["test_1"]. "</td>
  95. <td>" .$columns["test_2"]. "</td>
  96. <td>" .$columns["test_3"]. "</td>
  97. <td>" .$columns["test_4"]. "</td>
  98. <td>" .$columns["test_5"]. "</td>
  99. </tr>";
  100. }
  101. $output3 = ob_get_clean();
  102. $approach_3_result = parsemicrotime()-$time_start3;
  103.  
  104.  
  105. die("first test:".$approach_1_result."<br>second test:".$approach_2_result."<br>third test:".$approach_3_result);
  106.  
  107. $a = [];
  108. for($i = 0; $i < 10000; $i ++)
  109. $a[] = $i;
  110.  
  111. $output = '';
  112. foreach($a as $k => $v)
  113. $output = $output . "some static text" . $v . "some other text";
  114.  
  115. $output .= 'some static text' . $v . 'some other text';
  116.  
  117. $output = '';
  118. ob_start();
  119. foreach($a as $k => $v)
  120. echo 'some static text' . $v . 'some other text';
  121. $output = ob_get_clean();
  122.  
  123. ob_start();
  124. foreach($data as $r=>$d)
  125. {
  126. echo "<tr>
  127. <td>
  128. ";
  129. echo $d["client_id"];
  130.  
  131. echo "</td>
  132. ......
  133. <td>
  134. ";
  135. echo $d["date_stamp"];
  136.  
  137. echo "</td>
  138. </tr>";
  139. }
  140.  
  141. $return .= ob_get_contents();
  142. ob_end_clean();
Add Comment
Please, Sign In to add comment