I found this to be quite an interesting question and did some crude benchmarks to get some numbers.
Dataset
I'm using a dataset of all 25 letters uppercased (A-Z), and I've implemented the switch in three ways:
- The normal way:
case $value: return $value;
- The loose comparison:
case $value == 'A': return $value;
- The strict comparison:
case $value === 'A': return $value;
Test
The time calculated is the time used for calling the switch with every single letter of the dataset once.
Results
The output was as follows on my system running PHP 8.0.18:
Iterations: 1000000
switch_with_loose_comparisons
Tot: 2.4946439266205 seconds
Avg: 2.4484488964081E-6 seconds
Max: 0.00097203254699707 seconds
Min: 9.5367431640625E-7 seconds
switch_with_strict_comparisons
Tot: 2.9073448181152 seconds
Avg: 2.8613615036011E-6 seconds
Max: 0.00032496452331543 seconds
Min: 1.9073486328125E-6 seconds
switch_with_values
Tot: 1.0886509418488 seconds
Avg: 1.0394973754883E-6 seconds
Max: 0.00041389465332031 seconds
Min: 0 seconds
The noteworthy figures of this is the total amount of time used for 1 million iterations of the dataset where the largest difference is.
Conclusion
The normal way is faster!
Keep in mind the total times are worth 25 million calls to each function, so in practice it shouldn't really matter what you use in production as the differences are negligible for single calls.
Code
function switch_with_loose_comparisons(string $input) : string
{
switch (true) {
case $input == 'A':
return $input;
case $input == 'B':
return $input;
case $input == 'C':
return $input;
case $input == 'D':
return $input;
case $input == 'E':
return $input;
case $input == 'F':
return $input;
case $input == 'G':
return $input;
case $input == 'H':
return $input;
case $input == 'I':
return $input;
case $input == 'J':
return $input;
case $input == 'K':
return $input;
case $input == 'L':
return $input;
case $input == 'M':
return $input;
case $input == 'N':
return $input;
case $input == 'O':
return $input;
case $input == 'P':
return $input;
case $input == 'Q':
return $input;
case $input == 'R':
return $input;
case $input == 'S':
return $input;
case $input == 'T':
return $input;
case $input == 'U':
return $input;
case $input == 'V':
return $input;
case $input == 'W':
return $input;
case $input == 'X':
return $input;
case $input == 'Y':
return $input;
case $input == 'Z':
return $input;
}
}
function switch_with_strict_comparisons(string $input) : string
{
switch (true) {
case $input === 'A':
return $input;
case $input === 'B':
return $input;
case $input === 'C':
return $input;
case $input === 'D':
return $input;
case $input === 'E':
return $input;
case $input === 'F':
return $input;
case $input === 'G':
return $input;
case $input === 'H':
return $input;
case $input === 'I':
return $input;
case $input === 'J':
return $input;
case $input === 'K':
return $input;
case $input === 'L':
return $input;
case $input === 'M':
return $input;
case $input === 'N':
return $input;
case $input === 'O':
return $input;
case $input === 'P':
return $input;
case $input === 'Q':
return $input;
case $input === 'R':
return $input;
case $input === 'S':
return $input;
case $input === 'T':
return $input;
case $input === 'U':
return $input;
case $input === 'V':
return $input;
case $input === 'W':
return $input;
case $input === 'X':
return $input;
case $input === 'Y':
return $input;
case $input === 'Z':
return $input;
}
}
function switch_with_values(string $input) : string
{
switch ($input) {
case 'A':
return $input;
case 'B':
return $input;
case 'C':
return $input;
case 'D':
return $input;
case 'E':
return $input;
case 'F':
return $input;
case 'G':
return $input;
case 'H':
return $input;
case 'I':
return $input;
case 'J':
return $input;
case 'K':
return $input;
case 'L':
return $input;
case 'M':
return $input;
case 'N':
return $input;
case 'O':
return $input;
case 'P':
return $input;
case 'Q':
return $input;
case 'R':
return $input;
case 'S':
return $input;
case 'T':
return $input;
case 'U':
return $input;
case 'V':
return $input;
case 'W':
return $input;
case 'X':
return $input;
case 'Y':
return $input;
case 'Z':
return $input;
}
}
$letters = range('A', 'Z');
$results = [];
$functions = [
'switch_with_loose_comparisons',
'switch_with_strict_comparisons',
'switch_with_values',
];
$iterations = 100000;
$totals = [];
foreach ($functions as $function) {
$total_start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$start = microtime(true);
foreach ($letters as $letter) {
$function($letter);
}
$results[$function][] = microtime(true) - $start;
}
$totals[$function] = microtime(true) - $total_start;
}
$averages = [];
$max = [];
$min = [];
foreach ($results as $function => $times) {
$averages[$function] = array_sum($times) / count($times);
$max[$function] = max($times);
$min[$function] = min($times);
}
echo "Iterations: $iterations\n";
foreach ($functions as $function) {
echo "$function\n";
echo "Tot: $totals[$function] seconds\n";
echo "Avg: $averages[$function] seconds\n";
echo "Max: $max[$function] seconds\n";
echo "Min: $min[$function] seconds\n";
}