HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Is code optimization for branch predictor necessary now?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
necessaryoptimizationnowforcodebranchpredictor

Problem

I would like to share a part of some code written several years ago (I have changed from mysql to mysqli); this code works perfectly. I am new to my company, and our senior programmers wrote some extra lines to optimize branch predictions. My question is: is it necessary to follow this code? Does it improve performance?

We are hosting our files on Linux VPS CentOS 64Bit, 4GM RAM

$gq = array();   //to hold global ques
 $gq = array_merge($m1_array,$m2_array,$m3_array,$m4_array,$m5_array,$mp5_array) ; 
        unset ($m1_array,$m2_array,$m3_array,$m4_array,$m5_array,$mp5_array);
        shuffle($gq);
    // collect answers
    //preparing list of question ids to get the answer 
    $qnolist ='';
    foreach ($gq as $qm)
            {$qnolist = $qnolist.','.$qm['qid'];}
    $qnolist=substr_replace($qnolist ,'',0,1); 
    //sorted list qry always good performance (branch predictor)
    $qnoarray =explode(",",$qnolist);
    sort($qnoarray);
    $qnolist=implode(",",$qnoarray);
    $sql = "SELECT some fields where `qid` in ({$qnolist}) order by `qid`,`aid`;";
    $result = mysqli_query ($dbcon, $sql) or die("Server connection error..."); 
      $ga = array();
        while ($row = mysqli_fetch_assoc($result)) {
              array_push($ga, $row);
              } 
        mysqli_free_result($result);

Solution

@rolfl's answer covers the major points very well. In addition, I would recommend that you change the comment, removing the phrase "branch predictor." A branch predictor is a part of the CPU which attempts to predict the result of a branch so that the CPU can speculatively execute the predicted branch. A programmer can write code that optimizes branch prediction in assembly, or C, or other compiled languages for which the programmer can reliably predict the machine code that the compiler will emit. In an interpreted language such as PHP or SQL, there is so much code between your source and the machine that optimizing for the branch predictor at the level of your PHP or SQL source no longer makes any sense.

Context

StackExchange Code Review Q#39049, answer score: 4

Revisions (0)

No revisions yet.