Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branch prediction at php

Just read a great post about branch prediction. I was trying to reproduce it using php language.

<?php

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

$count = 300000;
$sum = 0;
for ($i = 0; $i <= $count; $i++) {
    $array[] = rand(0, $count);
}

sort($array);

for ($i = 0; $i <= $count; $i++) {
    if ($array[$i] <= 150000) {
        $sum += $array[$i];
    }
}

$time_end = microtime_float();
$time = $time_end - $time_start;

echo $sum . '<br />';
echo 'End:' . $time;
?>

But I always get the same results with sorting and without it. Maybe I'm doing something wrong? Or maybe php has built in optimization for branch predictor?

UPD:

I made code modifications according to comments and measure the time on my local machine.

Not sorted array: 1.108197927475

Sorted array: 1.6477839946747

Difference: 0.539586067.

I think this difference spent on sorting. It looks like true that branch predictor has no impact on speed.

like image 860
Viacheslav Kondratiuk Avatar asked Jul 02 '12 13:07

Viacheslav Kondratiuk


People also ask

What is meant by branch prediction?

Branch prediction is a technique used in CPU design that attempts to guess the outcome of a conditional operation and prepare for the most likely result. A digital circuit that performs this operation is known as a branch predictor. It is an important component of modern CPU architectures, such as the x86.

Why branch prediction is used?

The purpose of the branch predictor is to improve the flow in the instruction pipeline. Branch predictors play a critical role in achieving high performance in many modern pipelined microprocessor architectures such as x86.

What is branch prediction buffer and branch prediction?

Branch prediction buffers contain prediction about whether the next branch will be taken (T) or not (NT), but it does not supply the target PC value. A Branch Target Buffer (BTB) does this. The control unit looks up the branch target buffer during the “F” phase.

What is branch prediction list different types of branch prediction?

Types of Branch Prediction Technique – Branch prediction technique can be of two types: Static Branch Prediction Technique. Dynamic Branch Prediction Technique.


1 Answers

You won't replicate this in PHP. End of story. The reason is that the Java RTS uses JiT compilation techniques to compile the Java intermediate code down to the underlying X86 order code. This underlying order code will expose these branch prediction artefacts.

The PHP runtime system compiles PHP down to a bytecode which is a pseudo machine code that is interpreted. This interpreter will execute of the order of 0.5M opcodes /sec on a typical single core -- that is each PHP opcode takes perhaps 2-6K native instructions. Any subtleties of branching will be lost in this.

like image 173
TerryE Avatar answered Oct 03 '22 07:10

TerryE