mardi 24 mai 2016

Switch Case Vs. If.. Else (specific task)

I have 3 arrays that could be set or not. Their keys are strings.

Would it be more efficient this If... Else

if( isset( $arrayA[ $id ] ) )
{
    if( isset( $arrayB[ $id ] ) )
    {
        if( isset( $arrayC[ $id ] ) )
        {
        }
        else
        {
        }
    }
    else
    {
        if( isset( $arrayC[ $id ] ) )
        {
        }
        else
        {
        }
    }
}
else
{
    if( isset( $arrayB[ $id ] ) )
    {
        if( isset( $arrayC[ $id ] ) )
        {
        }
        else
        {
        }
    }
    else
    {
        if( isset( $arrayC[ $id ] ) )
        {
        }
    }
}

or a Switch... Case like this?

$ABC = ( isset( $arrayA[ $id ] ) ? "1" : "0" ) . ( isset( $arrayB[ $id ] ) ? "1" : "0" ) . ( isset( $arrayC[ $id ] ) ? "1" : "0" );

switch( $ABC )
{
    case "001":
    case "010":
    case "011":
    case "100":
    case "101":
    case "110":
    case "111":
}

Note that "000" case will never occur in my script.

Suppose I know combinations' frequency. More frequent -> "110" "111" "010" "100", "101", "001" "011" <- Less frequent

Anyway "110" and "111" occur almost always. "010" could occur 1 time per 10^4 or 10^5 times the cumulative occurrence "110"+"111".

Would this If... else a better solution?

if( isset( $arrayA[ $id ] ) and isset( $arrayB[ $id ] ) )
{
        if( isset( $arrayC[ $id ] ) )
        {
        }
        else
        {
        }
}
else
{
    $ABC = ( isset( $arrayA[ $id ] ) ? "1" : "0" ) . ( isset( $arrayB[ $id ] ) ? "1" : "0" ) . ( isset( $arrayC[ $id ] ) ? "1" : "0" );

    switch( $ABC )
    {
        case "001":
        case "010":
        case "011":
        case "100":
        case "101":
    }
}

Aucun commentaire:

Enregistrer un commentaire