Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. $objArr = Region::find()->all();
  4. $iterations = 250;
  5.  
  6. $i = 0;
  7. $result = [];
  8. $start1 = microtime(true);
  9. for ($i = 0; $i < $iterations; $i++) {
  10. foreach ($objArr as $obj) {
  11. $result[] = $obj->id;
  12. }
  13. }
  14. $end1 = microtime(true);
  15. unset($result);
  16.  
  17. $i = 0;
  18. $result = [];
  19. $start2 = microtime(true);
  20. for ($i = 0; $i < $iterations; $i++) {
  21. $result = ArrayHelper::getColumn($objArr, 'id');
  22. }
  23. $end2 = microtime(true);
  24. unset($result);
  25.  
  26. $i = 0;
  27. $result = [];
  28. $start3 = microtime(true);
  29. for ($i = 0; $i < $iterations; $i++) {
  30. $result = array_map(function ($x) {
  31. return $x->id;
  32. }, $objArr);
  33. }
  34. $end3 = microtime(true);
  35. unset($result);
  36.  
  37. $d1 = $end1 - $start1;
  38. $d2 = $end2 - $start2;
  39. $d3 = $end3 - $start3;
  40.  
  41. $result = [];
  42. $start1 = microtime(true);
  43. for ($i = 0; $i < $iterations; $i++) {
  44. $result = [];
  45. foreach ($objArr as $obj) {
  46. $result[] = $obj->id;
  47. }
  48. }
  49. $end1 = microtime(true);
  50. unset($result);
  51.  
  52. $i = 0;
  53. $result = [];
  54. $start2 = microtime(true);
  55. for ($i = 0; $i < $iterations; $i++) {
  56. $result = ArrayHelper::getColumn($objArr, 'id');
  57. }
  58. $end2 = microtime(true);
  59. unset($result);
  60.  
  61. $i = 0;
  62. $result = [];
  63. $start3 = microtime(true);
  64. for ($i = 0; $i < $iterations; $i++) {
  65. $result = array_map(function ($x) {
  66. return $x->id;
  67. }, $objArr);
  68. }
  69. $end3 = microtime(true);
  70. unset($result);
  71.  
  72. $d1 += $end1 - $start1;
  73. $d2 += $end2 - $start2;
  74. $d3 += $end3 - $start3;
  75. $arr = ['normal' => $d1, 'arrhelper' => $d2, 'map' => $d3];
  76. asort($arr);
  77. echo(json_encode($arr));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement