info('Starting the sort test...'); $count = 200000; $this->comment("Generating an array of $count random numbers..."); $array = []; for ($i = 0; $i < $count; $i++) { $array[] = rand(0, $count); } $memoryBefore = memory_get_usage(); $timeBefore = microtime(true); $this->comment('Before sorting:'); $this->comment('Memory usage: ' . round($memoryBefore / 1024 / 1024, 2) . ' MB'); $this->info('Sorting the array using PHP\'s built-in sort()...'); sort($array); $timeAfter = microtime(true); $memoryAfter = memory_get_usage(); $this->comment('After sorting:'); $this->comment('Memory usage: ' . round($memoryAfter / 1024 / 1024, 2) . ' MB'); $this->comment('Memory increase: ' . round(($memoryAfter - $memoryBefore) / 1024, 2) . ' KB'); $this->comment('Time taken: ' . round($timeAfter - $timeBefore, 4) . ' seconds'); $this->line(''); $this->info('First 12 elements:'); $this->line(implode(', ', array_slice($array, 0, 12))); $this->info('Last 12 elements:'); $this->line(implode(', ', array_slice($array, -12))); $this->info('Sort test finished.'); $this->line(' Для сортировки большого массива в PHP, особенно когда важны и скорость, и потребление памяти, встроенная функция `sort()` является лучшим выбором. Она реализована на C и использует алгоритм Quicksort, который является одним из самых быстрых для среднего случая (O(n log n)). `sort()` выполняет сортировку "на месте" (in-place), что означает, что она не требует выделения дополнительной памяти, пропорциональной размеру массива, что делает ее очень эффективной по памяти. Для большинства практических задач в PHP это оптимальное решение. '); } }