Advertisement
Guest User

Untitled

a guest
Jan 31st, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | Software | 0 0
  1. <?php
  2.  
  3. use Illuminate\Support\Collection;
  4.  
  5. // Include Laravel's autoload if you're running this outside a Laravel application
  6. require 'vendor/autoload.php';
  7.  
  8. // Function to measure execution time
  9. function measureTime(callable $callback)
  10. {
  11.     $start = microtime(true);
  12.     $callback();
  13.     return microtime(true) - $start;
  14. }
  15.  
  16. // Define dataset size
  17. $size = 1000000;
  18.  
  19. // Generate the dataset first (NOT included in timing)
  20. $rawData = range(1, $size);
  21. shuffle($rawData); // Mix the values randomly
  22.  
  23. // Measure time to create the PHP array
  24. $arrayCreationTime = measureTime(function () use ($rawData, &$data) {
  25.     $data = $rawData; // Simple assignment
  26. });
  27.  
  28. // Measure time to create the Laravel collection
  29. $collectionCreationTime = measureTime(function () use ($data, &$collection) {
  30.     $collection = collect($data);
  31. });
  32.  
  33. // Measure time for PHP array sum
  34. $arraySumTime = measureTime(function () use ($data) {
  35.     $sum = array_sum($data);
  36. });
  37.  
  38. // Measure time for Laravel Collection sum
  39. $collectionSumTime = measureTime(function () use ($collection) {
  40.     $sum = $collection->sum();
  41. });
  42.  
  43. // Measure time for iterating using foreach on PHP array
  44. $arrayIterationTime = measureTime(function () use ($data) {
  45.     foreach ($data as $value) {
  46.         $temp = $value * 2; // Some simple operation
  47.     }
  48. });
  49.  
  50. // Measure time for iterating using each() on Laravel Collection
  51. $collectionIterationTime = measureTime(function () use ($collection) {
  52.     $collection->each(function ($value) {
  53.         $temp = $value * 2; // Some simple operation
  54.     });
  55. });
  56.  
  57. // Display results with formatted output (no scientific notation)
  58. echo "PHP Array Creation Time: " . number_format($arrayCreationTime, 8) . " seconds\n";
  59. echo "Laravel Collection Creation Time: " . number_format($collectionCreationTime, 8) . " seconds\n";
  60. echo "PHP Array Sum Time: " . number_format($arraySumTime, 8) . " seconds\n";
  61. echo "Laravel Collection Sum Time: " . number_format($collectionSumTime, 8) . " seconds\n";
  62. echo "PHP Array Iteration Time (foreach): " . number_format($arrayIterationTime, 8) . " seconds\n";
  63. echo "Laravel Collection Iteration Time (each()): " . number_format($collectionIterationTime, 8) . " seconds\n";
  64.  
  65. // Compare performance
  66. echo "\nPerformance Summary:\n";
  67. echo "Faster data structure creation: " . ($arrayCreationTime < $collectionCreationTime ? "PHP Array" : "Laravel Collection") . "\n";
  68. echo "Faster sum operation: " . ($arraySumTime < $collectionSumTime ? "PHP Array" : "Laravel Collection") . "\n";
  69. echo "Faster iteration operation: " . ($arrayIterationTime < $collectionIterationTime ? "PHP Array" : "Laravel Collection") . "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement