开发者

More efficient way of performing multiple IFs

This is just a simple check to see what letter grade to output. Is there any faster and more efficient way to achieve the objective?

if ( $grade >= 90 ) {
        echo "A";
    } elseif ( $grade >= 80 ) {
        echo "B";
    } elseif ( $grade >= 70 ) {
        echo "C";
    } else开发者_JS百科 {
        echo "Failed."
    }


This is not answering your actual question but I think you are making a mistake here:

You really shouldn't be thinking about efficiency when using PHP, it is not a language that was designed for speed, but one that was designed for ease of use. Even more so if your application is not yet finished and you haven't verified that this piece of code slows down your whole application (using the profiler of xdebug, for example).


Any such improvements would be micro-optimizations. I think you've got the best solution for both efficiency and clarity.


I agree with other posters that you're doing it right already. However, in situations like these you could could try converting the $grade to a value that could be used as an index in an associative array, not unlike what @ghostdog74 tried to do above.

$gradeindex = (int)$grade / 10; // 10 since we want 10-19 = 1, etc..
$gradenames = array('10' => 'A+', '9' => 'A', '8' => B, ..... );

However, since so many of them are identical, I'd probably use a switch()

$gradeindex = (int)$grade / 10; // 10 since we want 10-19 = 1, etc..
switch ($gradeindex) {
  case 10:
  case 9:
    $gradename = 'A';
    break;
  case 8:
    $gradename = 'B';
    break;
  case 7:
    $gradename = 'C';
    break;
  default:
    $gradename = 'Failed';
}
echo $gradename;

But as already said, you're basically best of with your current if statement.


I'm sure there are weird ninja ways to do what you're doing, but yours is certainly the best. It's the most clear to read, and performance wise it's too fast to matter.


Personally, I think I would use a function with multiple returns for this purpose:

function intGradeToStrGrade($grade) {
    if ($grade >= 90) return "A";
    if ($grade >= 80) return "B";
    ...
}

However, there has been some debate here on SO if multiple returns in one function are OK or not. Your choice. I think this is much cleaner than a drawn-out if statement.


I don't actually know what the efficiency of this is, but I saw this problem and wanted to solve it in a non-nested-if() style so more knowledgeable folk could compare it's relative efficiency. Soooooo... Here's and alternative way of going about it :)

function GetLetterForPercentGrade($grade)
{
    $letter= chr(($grade >59) ? (10-floor($grade /10))+64 : 'F');
    $sign = chr(abs((ceil(((($grade %10)*10)+1)/34)*34)-69)+10);
    return $letter.$sign;
}


$grade=87;
$grades=array("A"=>range(95,100),"B"=>range(80,94),"C"=>range(70,79),"Failed"=>range(0,69));
foreach($grades as $g=>$v){
    if ( in_array($grade,$v) ){
        print $g."\n";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜