Are there any best practices for optimizing the performance of a PHP algorithm that generates permutations of numbers?

Generating permutations of numbers can be computationally expensive, especially for large sets of numbers. To optimize the performance of a PHP algorithm that generates permutations, one approach is to use efficient data structures and algorithms, such as recursion with memoization to avoid redundant calculations. Additionally, limiting the number of unnecessary operations and optimizing the code for speed can help improve the performance of the algorithm.

function generatePermutations($numbers, $perms = [], &$result = []) {
    if (empty($numbers)) {
        $result[] = $perms;
    } else {
        for ($i = count($numbers) - 1; $i >= 0; $i--) {
            $newNumbers = $numbers;
            $newPerms = $perms;
            list($picked) = array_splice($newNumbers, $i, 1);
            array_unshift($newPerms, $picked);
            generatePermutations($newNumbers, $newPerms, $result);
        }
    }
    return $result;
}

// Example usage
$numbers = [1, 2, 3];
$permutations = generatePermutations($numbers);
print_r($permutations);