哪个操作是快(>或> =),(小于;或< =)? [英] Which operator is faster (> or >=), (< or <=)?

查看:177
本文介绍了哪个操作是快(>或> =),(小于;或< =)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< 更便宜(速度)比< = ,同样,是> 更便宜(速度)比> =

Is < cheaper (faster) than <=, and similarly, is > cheaper (faster) than >=?

免责声明:我知道我可以衡量但这将只是我的机器上,我不知道答案可能是具体执行或者类似的东西。

Disclaimer: I know I could measure but that will be on my machine only and I am not sure if the answer could be "implementation specific" or something like that.

推荐答案

有异,在审查不同的指令集的第一个开始,如何如何的编译器使用这些指令集。就拿openrisc 32为例,这显然激发了MIPS,但确实条件句不同。为OR32有比较,并设置标志指示,比较这两个寄存器,如果小于或等于无符号则设置标志,如果相等集标志这两个寄存器进行比较。再就是在标志设置和分支机构的标志清晰两项有条件分支指令分支。编译器必须遵循这些路径中的一个,但少,比,小于或等于,大于,等等都将使用相同​​的数量的指令,相同的执行时间为条件分支和相同的执行时间不这样做的有条件的分支。

it varies, first start at examining different instruction sets and how how the compilers use those instruction sets. Take the openrisc 32 for example, which is clearly mips inspired but does conditionals differently. For the or32 there are compare and set flag instructions, compare these two registers if less than or equal unsigned then set the flag, compare these two registers if equal set the flag. Then there are two conditional branch instructions branch on flag set and branch on flag clear. The compiler has to follow one of these paths, but less, than, less than or equal, greater than, etc are all going to use the same number of instructions, same execution time for a conditional branch and same execution time for not doing the conditional branch.

现在它肯定会成为该执行分支需要比不是因为不必冲洗并重新填充管道的执行分支较长大多数架构如此。有些人这样做分支prediction等,以帮助这个问题。

Now it is definitely going to be true for most architectures that performing the branch takes longer than not performing the branch because of having to flush and re-fill the pipe. Some do branch prediction, etc to help with that problem.

现在某些架构指令的大小可能会有所不同,比较为gpr0和GPR1 VS比较为gpr0和眼前的数字1234,可能需要更大的指令时,你就会看到很多使用x86为例。所以虽然如果这两种情况下可能是一个分支不到你怎么带code少取决于什么寄存器发生持什么样的价值观可以使性能差异(确保86做了很多流水线,大量的缓存,等等,使为这些问题)。另一个类似的例子是MIPS和OR32,在r0始终为零,这是不是一个真正的通用寄存器,如果你写它,它并没有改变,这是硬连接到零,所以比较,如果等于0的费用可能你多比较,如果相等是否需要额外的指令或两个以填补GPR与眼前这样的比较可能发生,最坏的情况是有驱逐一个寄存器堆栈或内存中的一些其他数字,以释放登记将立即在那里,这样比较可能发生。

Now some architectures the size of the instruction may vary, compare gpr0 and gpr1 vs compare gpr0 and the immediate number 1234, may require a larger instruction, you will see this a lot with x86 for example. so although both cases may be a branch if less than how you encode the less depending on what registers happen to hold what values can make a performance difference (sure x86 does a lot of pipelining, lots of caching, etc to make up for these issues). Another similar example is mips and or32, where r0 is always a zero, it is not really a general purpose register, if you write to it it doesnt change, it is hardwired to a zero, so a compare if equal to 0 MIGHT cost you more than a compare if equal to some other number if an extra instruction or two is required to fill a gpr with that immediate so that the compare can happen, worst case is having to evict a register to the stack or memory, to free up the register to put the immediate in there so that the compare can happen.

某些架构,条件执行类似的手臂,为全面臂(不是拇指),你可以在每个指令的基础上执行指令,所以如果你有code

Some architectures have conditional execution like arm, for the full arm (not thumb) instructions you can on a per instruction basis execute, so if you had code

if(i==7) j=5; else j=9;

伪code的手臂会

the pseudo code for arm would be

cmp i,#7
moveq j,#5
movne j,#7

没有实际的分支,让你没有飞轮管道问题对​​上通过,速度非常快。

there is no actual branch, so no pipeline issues you flywheel right on through, very fast.

一个架构,以另一种,如果这是一个有趣的对比一些如前所述,MIPS,OR32,您必须专门执行一些指令的对比,有些人喜欢X86,MSP430和广大每次ALU操作更改标志,臂和样改变的标志,如果你告诉它改变如上图所示标志,否则不。因此

One architecture to another if that is an interesting comparison some as mentioned, mips, or32, you have to specifically perform some sort of instruction for the comparision, others like x86, msp430 and the vast majority each alu operation changes the flags, arm and the like change flags if you tell it to change flags otherwise dont as shown above. so a

while(--len)
{
  //do something
}

环1减法还设置了标志,如果在循环中的东西是很简单的,你可以使整个事情的条件,因此可以节省您在不同的比较和跳转指令,你在管道点球保存。 MIPS解决了这个有点通过比较和分支是一条指令,他们分支后执行一条指令到一点点保存在管道上。

loop the subtract of 1 also sets the flags, if the stuff in the loop was simple enough you could make the whole thing conditional, so you save on separate compare and branch instructions and you save in the pipeline penalty. Mips solves this a little by compare and branch are one instruction, and they execute one instruction after the branch to save a little in the pipe.

一般的答案是,你不会看到一个区别,指令数,execuition时间等都是相同的各种条件语句。特殊情况下,像小的立即VS大立即数等可能对边界情况的效果,或者编译器可以简单地选择做这一切的不同取决于你做什么比较。如果试图重新写你的算法有它给出相同的答案,但使用不足,而不是一个大于等于,你可能会改变code足以让不同的指令流。同样,如果执行过于简单性能测试中,编译器/将优化出了比较完整的,只是产生的结果,这可能取决于你的测试code导致不同的执行而有所不同。所有这一切的关键是拆开你要比较,看看说明什么不同的事情。这将告诉你,如果你应该会看到任何执行差异。

The general answer is that you will not see a difference, the number of instructions, execuition time, etc are the same for the various conditionals. special cases like small immediates vs big immediates, etc may have an effect for corner cases, or the compiler may simply choose to do it all differently depending on what comparison you do. If you try to re-write your algorithm to have it give the same answer but use a less than instead of a greater than and equal, you could be changing the code enough to get a different instruction stream. Likewise if you perform too simple of a performance test, the compiler can/will optimize out the comparison complete and just generate the results, which could vary depending on your test code causing different execution. The key to all of this is disassemble the things you want to compare and see how the instructions differ. That will tell you if you should expect to see any execution differences.

这篇关于哪个操作是快(&gt;或&GT; =),(小于;或&lt; =)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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