Home > Software design >  Which is the best/fastest practice? Create the same loop in each case of a switch statement, or to s
Which is the best/fastest practice? Create the same loop in each case of a switch statement, or to s

Time:05-11

So I was about to write something like following code

switch ($mode) {
    case 'all':
        foreach ($sortInfo as $info) $filter[] = array_merge([$info->maincat], $info->subcats);
        break;
    case 'sub':
        foreach ($sortInfo as $info) $filter[] = $info->subcats;
        break;
    default:
        foreach ($sortInfo as $info) $filter[] = [$info->maincat];
        break;
}

Then I told myself "hey, wouldn't it be more optimized if I wrapped the whole switch inside the for loop?"

foreach ($sortInfo as $info) {
    switch ($mode) {
        case 'all':
            $filter[] = array_merge([$info->maincat], $info->subcats);
            break;
        case 'sub':
            $filter[] = $info->subcats;
            break;
        default:
            $filter[] = [$info->maincat];
            break;
    }
}

But while it does technically save a bit (get it?) of filesize, the loop would confirm the same information in the switch statement for each iteration of the loop.

I figured there is no objective way to determine which is fastest because it depends on the length of $sortinfo, but since it's the first time I come across this dilemma, I'd like to know if there's a preferred method, or what's your take on it.

CodePudding user response:

Since you will only be running this once, any performance difference is entirely negligible. You could benchmark with a fixed dataset if you had to run it thousands of times, though still I doubt that you'd see more than a /- 10% difference. Choose whichever version you find more readable. If you want to save bits and are running PHP 8, you can do:

$stuff = match($mode) {
    'all' => array_merge(
        array_column($sortInfo, 'subcats'), array_column($sortInfo, 'maincat')
    ),
    'sub' => array_column($sortInfo, 'subcats'),
    default => array_column($sortInfo, 'maincat')
};

That's 227 bits vs. 338 bits in your "switch in foreach" vs. 346 bits in your "foreach in switch" or a 33% reduction. I find this approach very readable and free of avoidable verbosity.

If you're still runnning PHP 7, you can factor this approach into a switch:

switch ($mode) {
    case 'all': 
        $stuff = array_merge(
            array_column($sortInfo, 'subcats'), array_column($sortInfo, 'maincat')
        ),
    break;
    case 'sub': 
        $stuff = array_column($sortInfo, 'subcats');
        break;
    default: 
        $stuff = array_column($sortInfo, 'maincat');
        break;
};

That's still only 299 bytes. :) N.B. match was one of my major reasons for upgrading early to PHP 8. Premium sugar for a number of dishes, eliminates a lot of boilerplate code.

CodePudding user response:

Basicly, the code and outcome are the same, but i think the first one would be faster because it is more simple and straight-forward. The second one looks cleaner but it needs to switch-case and make decision everytime it loop and this would slow down the whole process.

  • Related