在PHP分行prediction [英] Branch prediction at php

查看:107
本文介绍了在PHP分行prediction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚看完<一个一个伟大的职位href=\"http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array\">branch prediction 。我试图使用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;
?>

但我总是排序,没有它得到相同的结果。也许我做错了什么?或者,也许PHP内置的优化分支predictor?

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:

我根据意见和衡量我的本地机器上的时间作出code修改。

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

未排序数组:1.108197927475

Not sorted array: 1.108197927475

排序数组:1.6477839946747

Sorted array: 1.6477839946747

区别:0.539586067。

Difference: 0.539586067.

我觉得这种差异花在整理。它看起来像真的,分支predictor对速度没有影响。

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

推荐答案

您不会在PHP中复制此。故事结局。其原因是在Java RTS使用JIT编译技术来编译Java中间code到底层X86秩序code。这个基本规律code会暴露这些分支prediction文物。

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.

PHP运行时系统编译PHP下一个字节code这是一个伪机code这是间preTED。这间preTER将在一个典型的单核执行0.5M运codeS /秒的秩序 - 这是每个PHP运算code花费也许2-6K本地指令。分支的任何细微之处都将丢失在这。

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.

这篇关于在PHP分行prediction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆