本文介绍了为什么分支预测比没有分支更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到这个问题的启发:为什么处理排序数组比处理排序数组更快一个未排序的数组?

我编写了自己的分支预测实验:

I wrote my own branch prediction experiment:

public class BranchPrediction {
    public static void main(final String[] args) {
        long start;
        long sum = 0;

        /* No branch */
        start = System.nanoTime();
        sum = 0;
        for (long i = 0; i < 10000000000L; ++i)
            sum += i;
        System.out.println(System.nanoTime() - start);
        System.out.println(sum);

        /* With branch */
        start = System.nanoTime();
        sum = 0;
        for (long i = 0; i < 10000000000L; ++i)
            if (i >= 0)
                sum += i;
        System.out.println(System.nanoTime() - start);
        System.out.println(sum);

        /* No branch (again) */
        start = System.nanoTime();
        sum = 0;
        for (long i = 0; i < 10000000000L; ++i)
            sum += i;
        System.out.println(System.nanoTime() - start);
        System.out.println(sum);

        /* With branch (again) */
        start = System.nanoTime();
        sum = 0;
        for (long i = 0; i < 10000000000L; ++i)
            if (i >= 0)
                sum += i;
        System.out.println(System.nanoTime() - start);
        System.out.println(sum);
    }
}

结果让我很困惑:根据程序输出,有分支的循环比没有分支的循环可靠得更快.

The result confuses me: according to program output, the loop with a branch is reliably faster than no branch loops.

示例输出:

7949691477
-5340232226128654848
6947699555
-5340232226128654848
7920972795
-5340232226128654848
7055459799
-5340232226128654848

为什么会这样?

推荐答案

在我的其他机器(Intel 服务器和工作站)上运行相同的实验后,我可能会得出结论,我遇到的现象是特定于这台笔记本电脑 CPU(Intel i7Q740M).

After running the same experiment on my other machines (Intel servers and workstations), I may conclude that the phenomenon I experienced is specific to this laptop CPU (Intel i7 Q740M).

==== 6 个月后编辑 ====

==== 6 months later edit ====

看看这个:http://eli.thegreenplace.net/2013/12/03/intel-i7-loop-performance-anomaly/

这篇关于为什么分支预测比没有分支更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 13:54